How to prevent Hibernate from using 0 as an identifier?

I use

@TableGenerator(name="tab",initialValue=2,allocationSize=50) 

on objects and define an identifier using

 @Id @GeneratedValue(generator="tab",strategy=GenerationType.TABLE) private int id; 

but Hibernate still uses 0 as an identifier.

I cannot use @GenericGenerator because annotations do not come with Hibernate4, which comes with Jboss AS7.

Is there a simple solution or do I need to write my own generator?

+4
source share
1 answer

Hibernate creates identifiers with id 0 because you have a primitive type. Try using Integer id instead of int id . Remember that primitives cannot contain a null value.

If you want to generate a user identifier generator, you can use SEQUENCE in the database to generate an identifier if an object.

 <id ....> <generator class="sequence"> <param name="sequence">YOUR_SEQUENCE _NAME</param> </generator> </id> 

Read the API about generator classes here .

+2
source

Source: https://habr.com/ru/post/1381594/


All Articles