ManyToOne associations in embeddable collection elements are always eagerly loaded in Hibernate 5

Solution Verified - Updated

Environment

  • Red Hat JBoss Enterprise Application Platform (EAP) 7
  • Hibernate 5

Issue

  • An entity includes an element collection of embeddable types with a ManyToOne association to another entity

    	@Entity(name = "Parent")
    	public static class Parent {
    		@Id
    		@GeneratedValue
    		private int id;
    
    		@ElementCollection
    		private Set<ContainedChild> containedChildren = new HashSet<ContainedChild>();
    	}
    
    	@Entity(name = "Child")
    	public static class Child {
    		@Id
    		@GeneratedValue
    		private int id;
    
    	}
    
    	@Embeddable
    	public static class ContainedChild {
    		@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
    		private Child child;
    
    		ContainedChild() {
    		}
    
    		ContainedChild(Child child) {
    			this.child = child;
    		}
    	}
    
  • When the element collection is initialized, the associated ManyToOne entities will be eagerly loaded.

Resolution

This issue1 is resolved in This content is not included.EAP 7.1 cumulative patch (C4) 4 and later releases.

Root Cause

This is a known defect (Content from hibernate.atlassian.net is not included.HHH-12687).

Components
Category

This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.