Hi,
I am new with Hibernate and encountered an issue, I will describe it through a short example because the involved classes and tables are parts of a very complex project from where is a little difficult to take out the code. In the project Hibernate 3.3.1 is used.
We have 2 tables: ParentTable and ChildTable. ParentTable has: Parent_Id (primary key) and some other fields (which are not interesting for us) ChildTable has: Parent_Id (foreign key from ParentTable), Field1, Field2. (FK_Parent_id and Field1) are the primary key for this table, so in the DB we have a Unique index for these fields.
Currently we have the following mappings in Java: @Entity @Table(name = "ParentTable") @AttributeOverride(name = "id", column = @Column(name = “Parent_Id", nullable = false)) @SequenceGenerator(name = "SEQ", sequenceName = "S_PARENT_ID") public class Parent extends PersistentObject (in this class we have defined id, getId(), setId() ) { ......................... @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER, mappedBy = "key") @JoinColumn(name = "Parent_Id") @MapKey(name = "key") private Map<Child.Key, Child> childrens = new HashMap(); .......................... public Map<Child.Key, Child> getChildrens() return childrens; public void setChildrens(Map<Child.Key, Child> childrens) this.childrens = childrens; }
@Entity @Table(name = "ChildTable") public class Child { private static final long serialVersionUID = 2795603999169345952L; @Column(name = "Field2", nullable = false) private Integer field2;
@EmbeddedId private Key key;
.........................
public Key getKey() return key; public Integer getField2() return field2; public void setField2(Integer field2) this.field2 = field2;
public Long getId() return key.parent.getId(); public void setId(Long id) key.parent.setId(id); public Field1 getField1() return key.getField1(); public void setField1(Field1 Field1) this.key.setField1(Field1); public Parent getParent() return key.getParent(); public void setParent(final Parent parent) key.setParent(parent);
@Embeddable public static class Key implements Serializable { private static final long serialVersionUID = 9102624253686497061L;
@ManyToOne(optional = false, fetch = FetchType.LAZY) @JoinColumn(name = "Parent_Id", insertable = false, updatable = false, nullable = false) private Parent parent;
@Type(type = "com.Field1") @Column(name = "Field1", insertable = false, updatable = false, nullable = false, unique = true) private Field1 field1;
public Key(Parent parent, Field1 field1) { this.parent = parent; this.field1 = field1; }
public Key() { this.parent = null; this.field1 = null; }
public Parent getParent() return parent; public Field1 getField1() return field1;
public boolean equals(Object obj) { ………….. }
public int hashCode() { ………. }
public void setParent(Parent parent) this.parent = parent; public void setField1(Field1 field1) this.field1 = field1; } }
Basically on the GUI I have a form with many fields (for Parent) and have a grid (which has 2 columns: Field1 and Field2) and can contain several rows (for child).
The problem is that if I remove a row from this grid the delete doesn’t work from the DB, the value is put back and it is showed again on the GUI. Only inserts and update works as expected. I read a couple of articles, forums, a few chapters from hibernate books but none of the solutions worked, does somebody has any idea of what can I change or what is wrong?
Thanks
|