I am a newbie in hibernate and was playing around with it. For an unique field in an entity I am setting @GeneratedValue annotation to it. Suppose I have two entities , user and company and both having an unique id with @GeneratedValue annotation . When I save both the entities using hibernate session factory , the id generated are 1 and 2 respectively , where it should be 1 each for both entities. The next time when i save both , id generated are 3 and 4 . So my guess is that , @GeneratedValue is working in db level and not table level. How to make it table level ?
Adding some codes and configs.
User entity
@Entity
@Table(name="user")
public class User {
@Id
@GeneratedValue
@Column(name = "id")
private Integer id;
}
Company entity
@Entity
@Table(name="company")
public class Company {
@Id
@GeneratedValue
@Column(name = "id")
private Integer id;
}
Saving using hibernate session
Session session = sessionFactory.getCurrentSession();
User user = new User();
session.save(user);
Company company = new Company();
session.save(company);
Result
mysql> select * from user;
+-----+
| id |
+-----+
| 234 |
+-----+
mysql> select * from company;
+-----+
| id |
+-----+
| 235 |
+-----+
For the records , I am using hibernate-jpa-2.1-api ( 1.0.0.Draft-16 ) with hibernate 5.