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

Solution Verified - Updated -

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.

Environment

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

Subscriber exclusive content

A Red Hat subscription provides unlimited access to our knowledgebase, tools, and much more.

Current Customers and Partners

Log in for full access

Log In

New to Red Hat?

Learn more about Red Hat subscriptions

Using a Red Hat product through a public cloud?

How to access this content