Yes, you should use the following mapping
@Entity public class Parent {
@Id private Integer id; @CollectionOfElements @JoinTable( name="Child", joinColumn=@JoinColumn (name="PARENT_ID")) @IndexColumn("childIndex") private List<Child> childList = new ArrayList<Child>();
}
Note. @CollectionOfElements and IndexColumn are Hibernate annotations, not JPA 1.0. JPA 2.0 will introduce them.
@Embeddable public class Child {
// @Embeddable class does not contains identifiers // child class specific property's
}
So the following code will work fine
Parent parent = new parent ();
parent.getChildList (). add (new Child ()); parent.getChildList (). add (new Child ()); // another child
session.save (parent); // Parent And Two Children Will Be Saved
The disadvantage of this problem is that the @CollectionOfElements annotation applies only to the @Embeddadble class, not to the Entity class. If you want the Child class as an Entity class, I would like to see a solution. At the same time, this is not possible. @Entity and @Embeddable class annotations.
Relations Arthur Ronald F. D. Garcia (Java Programmer) Natal / RN - Brazil
Arthur Ronald FD Garcia
source share