Home > database >  Using JPA to retrieve particular many-valued association columns
Using JPA to retrieve particular many-valued association columns

Time:03-08

There is a column that's so huge, it slows the entire query speed. So i need to ommit the one, only give when actualy needed. I've tried this solution:

@Query("SELECT new Account (a.name) From Account a Where a.id = :id)

The above code will retrive the id column data.When I try to use other getter,obviously the rest property are all null. but when it comes with other entity relation for example:

//The account entity code
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "user_id")
    @NotFound(action = NotFoundAction.IGNORE)
    private User user;
/**
The user side code is ommitted
**/
@Query("SELECT new Account (a.name,a.user) From Account a)

it will generarte these sql query:

inner join
        user user3_ 
            on account0_.user_id=user3_.id 

However, when we using the normal jpa method like

@Query("SELECT a FROM Account a WHERE a.id = :id")
Account getById(UUID id)

we can easily get the user entity with the getter method:

Account acc = accountRepository.getById(id);
User user = acc.getUser();

Not the inner join sql query;

How can I retrieve the paritcular association entity columns with getter?

Can it be possible to achieve with jpa?

CodePudding user response:

This post should help : How to lazy load an entity attribute using JPA

Prior I strongly recommend you to understand what lazy loading is in JPA and how it works. These one could help : What is lazy loading in Hibernate? and Difference between FetchType LAZY and EAGER in Java Persistence API?

  • Related