0

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?

Community
  • 1
  • 1
lior
  • 1,127
  • 3
  • 24
  • 43

1 Answers1

0

Refer the link Hibernate Auto Increment ID

You can use GenerationType.AUTO to create . For details refer link above

Community
  • 1
  • 1
muthukumar
  • 2,233
  • 3
  • 23
  • 30
  • maybe I cant get it to work because I;m using hibernate as a JPA provider and not pure hibernate? – lior Apr 16 '14 at 11:39