27

I have 2 tables: Order [OrderId(PK), OrderShipmentCode, ...] and Shipment[ShipmentId(PK), ShipmentCode, ...].

In Order class, I declared shipment field as follows:

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "OrderShipmentCode", referencedColumnName = "ShipmentCode", insertable = false, updatable = false, nullable = false)
private Shipment shipment;

When I get the list of Order, the Shipment is also loaded (I saw many separate SELECT queries). But I want the Shipment to be lazy loaded, not to be fetched together with Order.

For other tables, if the referenced column is primary key then the results are as expected (Lazy-loading is used). In my case, ShipmentCode is not Primary Key of Shipment table, and lazy-loading is not used by Hibernate.

Could you show me how to accomplish that goal?

EDIT: The Query code is as bellow:

Criteria criteria = HibernateUtil.getSessionFactory().getCurrentSession().createCriteria(Order.class);
List result = criteria.list();

SQL queries are: 1 SELECT statement for Order table and a bunch of SELECT statement for Shipment

3
  • 1
    Can you show the code for criteria or query being fired? Commented Jan 9, 2015 at 6:44
  • 1
    you can try using a native query like select * from order where condition. Commented Apr 5, 2018 at 5:37
  • If you know that the joined object is never null you can use optional=false. Hibernate then can lazily load the relation. See this super helpful post: stackoverflow.com/questions/3331907/… Commented Feb 16, 2022 at 0:41

5 Answers 5

2

Thd problem is caused by the HHH-13024 issue.

In the true spirit of OSS, you might want to investigate the problem and send a Pull Request with a fix proposal. That's the fastest way of getting an issue fixed.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. I already tried your solution but the result is the same
I also tested with your updated answer but the results are still the same.
I agree with that. Check this post for more detail (stackoverflow.com/questions/17987638/…)
1

If you worry about multiple select queries while loading, you can overcome this by using Entity Graphs. please refer to below link for more details https://www.baeldung.com/spring-data-jpa-named-entity-graphs

Comments

0

Try this:

Criteria criteria = HibernateUtil.getSessionFactory()
                                 .getCurrentSession()
                                 .createCriteria(Order.class)
                                 .setFetchMode("shipment", FetchMode.LAZY);

1 Comment

Please add explanation to your solution
-1

You can use @JsonIgnore over the shipment field of order. If you are giving using MappedBy over shipment field then removing it might solve your issue.

Comments

-1

add to your entity field with shipmentCode and set your code for relation, then it`s fork fine

@Column(name = "shipmentCode")
private Long shipmentCode;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "OrderShipmentCode", referencedColumnName = "shipmentCode", insertable = false, updatable = false, nullable = false)
private Shipment shipment;

If you use Lazy-init you entity extracting without fild with relation value, then when you trying extract lazy entity Hibernate can`t find any values becouse it havent relation value

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.