Unexpected behavior with association which overrides a transient property in Hibernate

Solution Verified - Updated -

Issue

  • An entity hierarchy is defined as follows:
    @MappedSuperclass
    public class SuperClass implements Serializable {
        ...
        @Transient
        public RelatedEntity getRelatedEntity() {
            return relatedEntity;
        }
    }

    @Entity
    public class SubClass extends SuperClass {
        @ManyToOne(targetEntity = RelatedEntity.class, fetch = FetchType.LAZY)
        @JoinColumn(name = "AssociatedId")
        public RelatedEntity getRelatedEntity() {
            return super.getRelatedEntity();
        }
    }

    @Entity
    public class RelatedEntity {
        ...
    }
  • Sometimes the association is found to be null. Other times it is populated automatically (e.g. when loading instances of the subclass using a query).

Environment

  • Red Hat JBoss Enterprise Application Platform (EAP) 6
  • Hibernate 4

Subscriber exclusive content

A Red Hat subscription provides unlimited access to our knowledgebase of over 48,000 articles and solutions.

Current Customers and Partners

Log in for full access

Log In
Close

Welcome! Check out the Getting Started with Red Hat page for quick tours and guides for common tasks.