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.