比如如下代码:
@Test
public void testSavePerson() {
try (Session session = sessionFactory.openSession()) {
Transaction tx = session.beginTransaction();
Person person = new Person();
Person person1 = new Person();
person.setName("222");
person1.setName("111");
session.save(person);
session.save(person1);
tx.commit();
}
}
始终报错,后来才发现是忘记主键自增了!!然后导致两个相同的对象在hibernate中,hibernate不能分辨这连个对象,就报错了!
添加
@GeneratedValue(strategy = GenerationType.AUTO)