Hibernate: @ManyToOne(fetch = FetchType.LAZY) does not work on non-primary key referenced column

Hibernate: @ManyToOne(fetch = FetchType.LAZY) does not work on non-primary key referenced column

hibernate one-to-one lazy loading not working
hibernate one to many lazy loading not working
jpa onetomany not fetching
hibernate lazy loading is not working
spring data jpa lazy loading not working
manytoone(fetch = fetchtype lazy joincolumn)
one to many lazy not working
fetch = fetchtype lazy optional = false

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


You need to specify optional = false:

@OneToOne(optional = false, fetch = FetchType.LAZY)

or simply turn it into a @ManyToOne:

@ManyToOne(fetch = FetchType.LAZY)

ManyToOne(fetch = FetchType.LAZY) doesn't work on non-primary , FetchType.LAZY) does not work on non-primary key referenced column But I want the Shipment to be lazy loaded, not to be fetched together with Order . By default, the @ManyToOne association assumes that the parent-side entity identifier is to be used to join with the client-side entity Foreign Key column. However, when using a non-Primary Key association, the referencedColumnName should be used to instruct Hibernate which column should be used on the parent side to establish the many-to-one database relationship.


Try this:

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

How to map a @ManyToOne association using a non-Primary Key , If it's not, lazy loading breaks and Hibernate eagerly fetches all make the system work exactly as expected if the join columns are the primary keys. LAZY ) @JoinColumn(name = "dummya_name", referencedColumnName� 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?


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.

Hibernate Tip: How to lazily load one-to-one associations, In this article, you are going to see how to use the @JoinColumn annotation in order to accommodate non-Primary Key many-to-one associations. @ ManyToOne (fetch = FetchType.LAZY). @JoinColumn ( Key association, the referencedColumnName should be used to instruct Hibernate which column� The @ManyToOne annotation allows you to map the Foreign Key column in the child entity mapping so that the child has an entity object reference to its parent entity. This is the most natural way of mapping a database one-to-many database association, and, usually, the most efficient alternative too .


@JoinColumn Annotation Explained, Learn what JPA's @JoinColumn annotation is all about. THE unique Spring Security education if you're working with Java today. Hibernate � JPA column in the owner entity refers to a primary key in the reference entity: @ManyToOne (fetch = FetchType.LAZY). @JoinColumn(name = "employee_id"). @Entity public class Office { @OneToOne(fetch = FetchType.LAZY) @JoinColumn(name = "addressId") private Address address; } The above code example will create a foreign key linking the Office entity with the primary key from the Address entity. The name of the foreign key column in the Office entity is specified by name property. 3.


ManyToOne lazy fetching on non primary key , Code: class Foo @ManyToOne(fetch=FetchType.LAZY) I didn't realize that it was limited to the non-primary-key associations as you and it doesn't work (we have eager fetching) when referencedColumnName refers to� The way this works at the database level is we have cart_id as a primary key in the cart table and also a cart_id as a foreign key in items. And, the way we do it in code is with @OneToMany . Let's map the Cart class to the Items object it a way that reflects the relationship in the database:


Hibernate 5.1.10 - lazy loading doesn't work, Lazy loading doesn't work for me, it always makes eager loading. This is because Hibernate has no other way of knowing whether to assign a null or a Serializable{ private DeviceEntity device; @OneToOne(fetch = FetchType. As far as I know, “find” function of EntityManager works by primary key. Even if the FK is NOT NULL and the parent-side is aware about its non-nullability through the optional attribute (e.g. @OneToOne(mappedBy = "post", fetch = FetchType.LAZY, optional = false)), Hibernate still generates a secondary select statement. For every managed entity, the Persistence Context requires both the entity type and the identifier,