-->
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: util.JDBCExceptionReporter Field 'ID' doesn't have a
PostPosted: Sat Oct 10, 2009 5:37 pm 
Newbie

Joined: Thu Mar 11, 2004 11:20 pm
Posts: 9
I searched the internet and found this thread viewtopic.php?p=2391598. However, I don't get the point on how it is solved.

I have the same error message saying

Code:
javax.persistence.PersistenceException: org.hibernate.exception.GenericJDBCException: could not insert:
   at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:637)
   at org.hibernate.ejb.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:244)
...
Caused by: java.sql.SQLException: Field 'ID' doesn't have a default value
   at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2928)
   at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1571)
   at com.mysql.jdbc.ServerPreparedStatement.serverExecute(ServerPreparedStatement.java:1124)
   at com.mysql.jdbc.ServerPreparedStatement.executeInternal(ServerPreparedStatement.java:676)
   at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1166)
   at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1082)
   at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1067)
   at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:73)
   at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:33)


My model classes contain
User -> Group (Many to Many)

Group -> Permission (one to Many)

The User class Id field is

Code:
   @Id
   @Column(name="ID", nullable=false)
   //@GeneratedValue(strategy = GenerationType.IDENTITY)
   @GeneratedValue(strategy = GenerationType.AUTO)
   private String id;


The id field is assigned manually (application will generate primary key) and application doesn't planed to let user set id other than constructor. So the id field must be provided whilst the class is initiated.

Code:
   public User(String id, String account, String name, String password){
      this.id = id;
      this.account = account;
      this.name = name;
      this.password = password;
   }
...
   public String getId(){
      return this.id;
   }

   //public void setId(String id){
   //   this.id = id
   //}



The persistence.xml transaction-type is set to RESOURCE_LOCAL

and the way to persist class is
Code:
public String createDefaultUser(String account, String name, String password, Gender gender){

      manager.getTransaction().begin();
      String id = KeyGenerator.generate();
      User user = new User(id, account, name, password);
      user.setGender(gender);
      user.addGroup(Group.GROUP_USER);
      merge(user);
      manager.flush();
      manager.getTransaction().commit();   
      return id;
}


What place is incorrectly set? Or how can I fix this error?

I appreciate any suggestion.


Top
 Profile  
 
 Post subject: Re: util.JDBCExceptionReporter Field 'ID' doesn't have a
PostPosted: Sat Oct 10, 2009 9:45 pm 
Newbie

Joined: Thu Mar 11, 2004 11:20 pm
Posts: 9
I think might find out the way to solve this issue.

This problem seems can be solved by remvoing GeneratedValue which is for value generated by database.

Code:
   @Id
   @Column(name="ID", nullable=false)
   //@GeneratedValue(strategy = GenerationType.IDENTITY)
   //@GeneratedValue(strategy = GenerationType.AUTO)
   private String id;


Thanks for help.


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.