Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Do I have to set ID value in hibernate entity

Do I have to set the ID value in entity class in hibernate, When I don’t set the value for ID It shows "Column ‘ID’ doesn’t have default value". Datatype for the column is Decimal, but when I use same database in Oracle I didn’t get error like this. I was seeing this error when I’m using MySQL. Could you give me an suggestion.

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

A common approach is to define the ID with an autoIncrement type. This let’s the database assign the next available ID to that field. So then you don’t have to set a value from the code (which could potentially fail if another thread on the server attempts to create an instance of that entity at the same moment).

The resulting ID value will be present in the returned entity instance after creation.

For example you can define such an ID field using JPA annotations as follows:

@javax.persistence.Id @javax.persistence.GeneratedValue
private Long id;

See also: https://www.baeldung.com/hibernate-identifiers

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading