How to fetch @Basic fields in JPQL query

Viewed 23

The Entity definition is:

@Entity
@DynamicInsert
@DynamicUpdate
public class ProductInformation {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Version
    private Long version;

    private String url;
    private String price;
    private String bookName;
    private String isbn;

    @Lob
    @Basic(fetch = FetchType.LAZY)
    private String sourceFile;

    @Lob
    @Basic(fetch = FetchType.LAZY)
    private String information;

    ...
}

I load all the entries using the following JPQL query:

from ProductInformation

Afterwards the fields annotated with @Basic are accessed during a Export procedure. I can see the following output in the SQL logs (the lazy loading is visible):

select productinf0_.id as id1_0_, productinf0_.book_name as book_nam2_0_, productinf0_.isbn as isbn4_0_, productinf0_.price as price5_0_, productinf0_.url as url7_0_, productinf0_.version as version8_0_ from product_information productinf0_
select productinf_.information as informat3_0_, productinf_.source_file as source_f6_0_ from product_information productinf_ where productinf_.id=?
select productinf_.information as informat3_0_, productinf_.source_file as source_f6_0_ from product_information productinf_ where productinf_.id=?
select productinf_.information as informat3_0_, productinf_.source_file as source_f6_0_ from product_information productinf_ where productinf_.id=?
select productinf_.information as informat3_0_, productinf_.source_file as source_f6_0_ from product_information productinf_ where productinf_.id=?
select productinf_.information as informat3_0_, productinf_.source_file as source_f6_0_ from product_information productinf_ where productinf_.id=?
...

Is there a way to fetch the @Basic(fetch = FetchType.LAZY) annotated fields in the JPQL Query?

1 Answers

See entityGraph and LoadGraphs (javax.persistence.loadgraph) query hint which allows you to specify additional fields to be loaded in your entity, overriding the lazy mapping specification. See https://www.baeldung.com/jpa-entity-graph for examples

Related