'Hibernate entities auto-increment id's across tables rather than per table

Currently I have three tables: Company, Group, Person.

The three tables are connected by Company is one-to-many with Group and Group is one-to-many with Person.

In all three classes (Company.java, Group.java, Person.java), I coded the auto-increment unique id this way:

@Id
@Column(name = "id")
@GeneratedValue
private int id;

Then I instantiated all three classes in this order: Company, Group, Person.

After saving those objects to my database, I noticed that there is something wrong with the id's in the tables.

Apparently, rather than increasing the Company's id in the order 1, 2, 3, 4, so on; it is increasing in 1, 4, 7, 10...

In this same logic, Group's id goes 2, 5, 8, 11...

What should I do to prevent the @GeneratedValue counter being shared among the three different tables? Is there an additional or different tag I should use?

Thanks.



Solution 1:[1]

That is probably because hibernate generate one table for the sequence of ids. Which DB are you working with? MySQL? or another database that do not use sequences?. You can try using:

 @GeneratedValue(strategy = GenerationType.IDENTITY)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 carpinchosaurio