2

I am working on project where I am using hibernate to perform CRUD operations. I have user model and I am trying to insert the information but keeping getting this error

Hibernate: insert into APPUSER (dob, email, firstName, lastName, password) values (?, ?, ?, ?, ?)
Jun 21, 2016 2:17:07 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 1400, SQLState: 23000
Jun 21, 2016 2:17:07 AM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: ORA-01400: cannot insert NULL into ("MYAPP8785"."APPUSER"."ID")

The user model looks like

@Entity
@Table(name="APPUSER")
public class AppUser {


    @Id 
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @Email
    @Size(max = 50)
    private String email;

    @Column
    private String dob;

    @Column
    private String firstName;

    @Column
    private String lastName;

    @Column(name = "password", nullable = false)
    private String password;
}

Hibernate properties like

properties.put("hibernate.dialect","org.hibernate.dialect.MySQLDialect");
//properties.put("hibernate.current_session_context_class","thread");
properties.put("hibernate.hbm2ddl.auto","update");
properties.put("hibernate.show_sql","true");

I am under the impression that hibernate would auto generate id for me and insert them using sequence

user3342812
  • 343
  • 8
  • 25

3 Answers3

6

Some errors :

  • Use the right dialect : org.hibernate.dialect.Oracle10gDialect (no specific dialect for 11g).
  • do not use primitive type for hibernate generated ID (use Integer, not int)

IDENTITY for ID generation is supported by oracle only since version 12c

You should use another strategy, like using sequence for example.

The mapping would look like :

@Id
@SequenceGenerator(name = "generator", sequenceName = "ID_SEQUENCE", allocationSize = 1)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "generator")
@Column(name = "ID")
private Integer id;
Vlad Mihalcea
  • 142,745
  • 71
  • 566
  • 911
Thierry
  • 5,270
  • 33
  • 39
  • Oracle version installed on a server I'm using is 11g. So as mentioned in the answer i used the Sequence generation strategy. I tried using TABLE and AUTO strategies as well. However, when i try to insert a record onto the table, it tells me that ID cannot be null. Shouldn't the ID be auto generated? Is there anything I'm missing? – Dilini Peiris Apr 05 '22 at 10:26
1

I see that you found a usable answer, but since I had this same problem and came across this post while searching for a solution, I will include what worked for me. I am using Oracle 12c - and this will ONLY work with 12c - the solution was to change the dialect to Oracle12cDialect. Otherwise, simply using

@GeneratedValue(strategy = GenerationType.IDENTITY)

worked.

James
  • 517
  • 1
  • 4
  • 17
0

You need to create the table APPUSER such that its primary key ID has auto increment feature. Please follow the link How to create id with AUTO_INCREMENT on Oracle? on how to do it. Only then you will see that hibernate will insert the value in the primary key field for you.

Community
  • 1
  • 1
Sanjeev Saha
  • 2,632
  • 1
  • 12
  • 19