-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Error: Default value for @id (pk) of derived objects
PostPosted: Tue Oct 31, 2006 11:47 am 
Newbie

Joined: Tue Oct 31, 2006 10:42 am
Posts: 2
Hello,

I am using EM for object persistence of the following structure: Class X and Y are derived from Z. Therefore Z defines the primary key (@id) for X and Y. V uses a Map of Y objects as an attribute.

When setting up the Map of Y objects in V the database (MySQL) is complaining that the primary key of Y (= @id of Z) has not a default value. I tried it with the @GeneratedValue and also manually. Obviously the value is set, but that field will be ignored. In the specs I found no other way to define additional default values.

When destroying the inheritence between X and Z, it works.
Can somebody help me out?

Thanks,
Andreas




Caused by: java.sql.BatchUpdateException: Field 'YMap_Id' doesn't have a default value
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)


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 02, 2006 10:22 am 
Newbie

Joined: Tue Oct 31, 2006 10:42 am
Posts: 2
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)



Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.