Working with JPA 1 (version hibernate-core 3.3.0.SP1 and hibernate-entitymanager version 3.4.0.GA): I have some entities similar to those defined below, where ChildOne and ChildTwo extend from the Father object.
@Entity @Table(name = "TABLE_FATHER") @Inheritance(strategy = InheritanceType.JOINED) @DiscriminatorColumn(discriminatorType = DiscriminatorType.INTEGER, name = Father.C_ID_CTG) public class Father { @Id @GeneratedValue(strategy = GenerationType.AUTO, generator = "sq") @Column(name = "ID_PK", nullable = false) @BusinessId private Long id; ... } @Entity @Table(name = "TABLE_CHILD_ONE") @Inheritance(strategy = InheritanceType.JOINED) @DiscriminatorValue(Categories.ID_CTG_ONE) public class ChildOne extends Father { ... } @Entity @Table(name = "TABLE_CHILD_TWO") @Inheritance(strategy = InheritanceType.JOINED) @DiscriminatorValue(Categories.ID_CTG_TWO) public class ChildTwo extends Element { ... }
Let's say I have one entity that has an element of the Father, and the other is a collection of elements of the father. In both cases, child objects should appear.
@Entity @Table(name = "TABLE_ONE") public class OneTable { @JoinColumn(name = "ID_PK", referencedColumnName = "ID_PK", nullable = false) @ManyToOne(optional = false, fetch = FetchType.LAZY) private Father element; ... } @Entity @Table(name = "TABLE_ANOTHER") public class Another { @Fetch(FetchMode.JOIN) @OneToMany(cascade = CascadeType.ALL, mappedBy = "id", fetch = FetchType.LAZY) private Collection<Father> elementCollection; ... }
I expect to always get children, but when I get the getElement() element, it returns the father element and, on the other hand, when I get the getElementCollection() collection, the children appear.
Apparently, the reason for this behavior is @JoinColumn , making a join to the paternal table and forgetting the children elements. The collection works as expected.
How can I get the children element with getElement() call? Any ideas or desktop? Thanks in advance.
source share