Hi everyone.
I am new to this forum and also to hibernate, but I already was able to answer a lot of my questions which came up, while I experimented with this framework.
One question I couldn't found the answer for is about a simple Map<Integer,CustomClass>.. I will post my code here:
Code:
@Entity
public class Parent implements Serializable{
@Id
@GeneratedValue
private Long id;
@OneToMany(cascade=CascadeType.ALL)
@Cascade ({org.hibernate.annotations.CascadeType.DELETE_ORPHAN})
@MapKey(name="parent_ref")
private Map<Integer, Child> fvalues = new HashMap<Integer, Child>();
public void setChild(int ref, Child child) {
fvalues.put(ref, child);
}
}
Code:
@Entity
public class Child implements Serializable {
@Id
@GeneratedValue
private Long id;
public Integer parent_ref;
}
So all I want to do is put Child in a map, which belongs to Parent. The code compiles and tomcat does not give any error, the database is being made without complaints. But when I do
Code:
Parent p = new Parent();
Child c = new Child();
p.setChild(5,c);
em.persist(p);
it does only put the value of the map in the database, not the key. I tried all kinds of combinations of annotations but so far no success. The childs show up in the database but the key-fields are always 0. In my case it should be 5 (from p.setChild(5,c);). Any ideas ? :S
Thanks to everyone with a suggestion in advance!
Greetz,
HellCode
EDIT: code corrections