I'm using spring jpa with Mysql.
I am trying to implement the folowing solution for bypassing the generated id field: Bypass GeneratedValue in Hibernate (merge data not in db?)
The problem is that when the id is null and should be generated I get : "field 'id' doesn't have a default value"
My entity:
@Entity
public class RegistrationDetails{
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY, generator = "IdOrGenerated")
@GenericGenerator(name = "IdOrGenerated", strategy = "com.xxx.common.UseIdOrGenerate")
@Column(name = "ID", nullable = false)
private Long id;
@Column(nullable = false)
private String firstName;
}
my generator:
public class UseIdOrGenerate extends IdentityGenerator {
@Override
public Serializable generate(SessionImplementor session, Object obj) throws HibernateException {
if (obj == null)
throw new HibernateException(
new NullPointerException());
if ((((RegistrationDetails) obj).getId()) == null) {
Serializable id = super.generate(
session,
obj);
return id;
}
else {
return ((RegistrationDetails) obj).getId();
}
}
}
From some conversations I think it something about the Auto increment filed but I cant add it in MySql.
EDIT: My tables are generated automatically when the tomcat server starts. the problem is that it doesn't set the id field as auto increment. Is there a hibernate annotation for this?