Problem with JPA / Hibernate / PG card

I have two objects: Entity1 and Entity2, from one to many relationships. On Entity1, I have a map containing entries.

The code that I use to save the new Entity1 with some Entity2 on the map is as follows:

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 an entity expression:

 @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 because it inserts both objects into the pg database, but the catch is that it does not create-save MapKey e2 in the database (theoretically JPA generates this key), so when I get e1 back, and I'm trying to get an e2s card from him, I have:

javax.ejb.EJBException: org.hibernate.HibernateException: null index column for collection:

How can I save this mapkey ??

Note. I use JavaEE with JPA / Hibernate while working on JBoss 4.2, with PGSQL DB.

+4
source share
1 answer

There are some oddities in the code ... But it seems like you should add the @MapKey annotation in Entity1:

 (snippet) @OneToMany(mappedBy = "e1", fetch = FetchType.EAGER) @MapKey(name="iCantFigureTheRightPropertyName") public Map<String, Entity2> getE2s() { return e2s; } 
+2
source

All Articles