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, 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