Search code examples
hibernatesql-server-2005annotations

Hibernate auto increment id sql server 2005


I'm trying to learn hibernate with annotations. Now I've set up a successful database connection with SQL SERVER 2005. I've made a class event, but the following exception comes up:

Hibernate: insert into EVENTS (EVENT_DATE, title) values (?, ?)
1235 [main] WARN org.hibernate.util.JDBCExceptionReporter - SQL Error: 515, SQLState:23000
1235 [main] ERROR org.hibernate.util.JDBCExceptionReporter 
- Cannot insert the value NULL 
into column 'id', table 'hibernate_db.dbo.EVENTS'; column does not allow nulls. INSERT fails.

the event class:

@Entity
@Table( name = "EVENTS" )
public class Event {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name = "EVENT_ID", unique = true, nullable = false)
    private int event_id;

If you need more information please say so. Can someone help me out? I absolutely have no idea what is wrong with my auto incrementing event_id...


Solution

  • It seems i figured it out thanks to related question: Hibernate Auto Increment ID

    @GeneratedValue(strategy = javax.persistence.GenerationType.IDENTITY)
    

    Is now doing the trick for me to auto-increment the EVENT_ID.

    So my new code for event_id is:

    @Id
    @GeneratedValue(strategy = javax.persistence.GenerationType.IDENTITY)
    @Column(name="EVENT_ID", unique = true, nullable = false)
    private int event_id;