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>();
}
Notice @CollectionOfElements and IndexColumn is Hibernate specific annotations, not JPA 1.0 specification. 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 works fine
Parent parent = new Parent();
parent.getChildList().add(new Child());
parent.getChildList().add(new Child()); // other child
session.save(parent); // A parent and two children will be saved
The drawback with this issue is that @CollectionOfElements annotation only applies to @Embeddadble class, not a Entity class. If you want a Child class as a Entity class, i would like to see the solution. It is not possible applies at the same time @Entity and @Embeddable annotations to a class.
Regards
Arthur Ronald F D Garcia (Java programmer)
Natal/RN - Brazil