Hi,
I use Hibernate 3.6.4 with JPA 2 . I have OneToMany similar to the below code:
1. Parent Class:
@Entity @Table(name = "PARENT_TABLE") public class Parent implements Serializable{
@Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SEQ_IC_PARENT") @SequenceGenerator(name = "SEQ_IC_PARENT", sequenceName = "SEQ_IC_PARENT", allocationSize = 1) @Column(name = "PARENT_ID") private Integer parentId;
@Column(name = "PARENT_NAME") private String parentName;
@OneToMany(mappedBy="parent", targetEntity = Child.class, cascade = { CascadeType.PERSIST, CascadeType.MERGE,CascadeType.REMOVE,CascadeType.DETACH,CascadeType.REMOVE,CascadeType.REFRESH}, orphanRemoval=true) @Cascade( { org.hibernate.annotations.CascadeType.SAVE_UPDATE, org.hibernate.annotations.CascadeType.MERGE, org.hibernate.annotations.CascadeType.DELETE, org.hibernate.annotations.CascadeType.PERSIST, org.hibernate.annotations.CascadeType.DELETE, org.hibernate.annotations.CascadeType.DELETE_ORPHAN }) private List<Child> ChildList = null; }
2. Child Class
@Entity @Table(name = "CHILD") public class Child implements Serializable{ @Id @Column(name="CHILD_ID") private String childId; }
3. Parent Child Class:
@Entity @Table(name = "PARENT_CHILD") public class ParentChild implements Serializable{
@Id @GeneratedValue(strategy=GenerationType.SEQUENCE, generator ="SEQ_IC_BD_SUPPORT") @SequenceGenerator(name="SEQ_PARENT_CHILD", sequenceName="SEQ_PARENT_CHILD", allocationSize=1 ) @Column(name = "PARENT_CHILD_ID") private Integer parentChildId;
@ManyToOne @JoinColumn(name = "parent_id",insertable=false,updatable=false) private Parent parent;
@OneToOne @JoinColumn(name = "USER_ID") private Child child; }
Basically I have 3 tables. Parent, Child and Parent_Child.
When saving the list in parent class, the child records are not deleted when i remove them from the collection.
Can you let me know If I am missing any in the mapping?
On Save I do this:
transaction = session.beginTransaction(); session.save(parent); or session.update(parent); session.flush(); transaction.commit();
|