I have two entities, Entity1 and Entity2, on a one to many relationship. On Entity1 I have a map which contains entries.
The code I use to persist a new Entity1 with some Entity2 on the map is like this:
Entity1 e1 = new Entity1();
Entity2 e2 = null;
e1.setE2s(new HashMap<String, Entity2>());
for (String key : someKeySet()){
e2 = new Entity2();
e2.setCode(key);
e2.setSwhon(true);
e2.setE1(e1);
e1.getE2s(key, e2);
em.persist(e1.getE2s().get(key));
}
em.persist(e1);
em.flush();
and here is a exctract of the entities:
@Entity
@Table(name = "wm_Entities1")
public class Entity1 implements Serializable {
private static final long serialVersionUID = 6592708276573465599L;
private Map<String, Entity2> e2s;
private Long id;
@Id
@GeneratedValue(strategy=GenerationType.TABLE)
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public void setE2s(Map<String, Entity2> e2s){
this.e2s = e2s;
}
@OneToMany(mappedBy = "e1", fetch = FetchType.EAGER)
public Map<String, Entity2> getE2s() {
return e2s;
}
}
@Entity
@Table(name = "wm_Entities2")
public class Entity2 implements Serializable {
private static final long serialVersionUID = -6131765066573346790L;
private long id;
private Entity1 e1;
@Id
@GeneratedValue(strategy=GenerationType.TABLE)
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
@ManyToOne()
@JoinColumn(name="e1_id")
public Entity1 getE1() {
return this.e1;
}
public void setE1(Entity1 e1) {
this.e1 = e1;
}
}
This seems to work fine since it inserts both entities on the pg database, but the catch is that it is not creating-saving the MapKey of the e2 on the database (theoretically JPA generates this key), so when I get the e1 back, and I try to get the e2s map from it, I have a:
javax.ejb.EJBException: org.hibernate.HibernateException: null index column for collection:
How can I get this MapKey saved??
Note: I'm using JavaEE with JPA/Hibernate, running over JBoss 4.2, with PGSQL DB.