Hello,
I did some more tests and I have to apologize. You need two hashmaps to replicate this error. Please have a look to the sample code. When you remove the inheritance (e.g. between u and v or x and z or y and z) or one one of the two Maps, it works. Please have a look. I can send you the src also via mail.
Thanks,
Andreas
Code:
@Entity
public class Z {
@Id
@GeneratedValue
private int zId;
public int getZId() {
return zId;
}
public void setZId(int id) {
zId = id;
}
}
Code:
@Entity
public class Y extends Z {
}
Code:
@Entity
public class X extends Z {
}
Code:
@Entity
public class V {
@Id
@GeneratedValue
private int vId;
@OneToMany(cascade=CascadeType.ALL)
@MapKey
private Map<Integer, Y> myMap = new HashMap();
public Map<Integer, Y> getMyMap() {
return myMap;
}
public void setMyMap(Map<Integer, Y> myMap) {
this.myMap = myMap;
}
}
Code:
@Entity
public class U extends V{
@OneToMany(cascade=CascadeType.ALL)
@MapKey
private Map<Integer, X> myMapU = new HashMap();
public Map<Integer, X> getMyMapU() {
return myMapU;
}
public void setMyMapU(Map<Integer, X> myMap) {
this.myMapU = myMap;
}
}
Code:
EntityManagerFactory emf = Persistence.createEntityManagerFactory("MyModel");
EntityManager em = emf.createEntityManager();
EntityTransaction tx = em.getTransaction();
tx.begin();
try {
X x = new X();
Y y = new Y();
Z z = new Z();
V v = new V();
U u = new U();
HashMap<Integer, Y> hm = new HashMap();
hm.put((Integer) y.getZId(), y);
v.setMyMap(hm);
em.persist(v);
tx.commit();
System.out.println("Persistent object stored");
} catch (Exception ex) {
System.out.println(ex.getMessage());
ex.printStackTrace();
tx.rollback();
System.out.println("Rolling back transactions for persistent object... ");
}
em.close();
emf.close();
Error:
Code:
Error while commiting the transaction
javax.persistence.RollbackException: Error while commiting the transaction
at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:63)
at Main.start(Main.java:37)
at Main.main(Main.java:56)
Caused by: org.hibernate.exception.GenericJDBCException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:103)
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:91)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:249)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:143)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:297)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:988)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:337)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at org.hibernate.ejb.TransactionImpl.commit(TransactionImpl.java:53)
... 2 more
Caused by: java.sql.BatchUpdateException: [b]Field 'myMapU_zId' doesn't have a default value[/b]
at com.mysql.jdbc.ServerPreparedStatement.executeBatch(ServerPreparedStatement.java:648)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:242)
... 10 more
Exception in thread "main" java.lang.IllegalStateException: Transaction not active
at org.hibernate.ejb.TransactionImpl.rollback(TransactionImpl.java:74)
at Main.start(Main.java:42)
at Main.main(Main.java:56)