org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [hibernate.student1#0]這是報錯的信息這是測試類public class GSTest {private SessionFactory sessionFactory; ? private Session session; ? private Transaction transaction; @Before public void init(){ //創(chuàng)建配置對象 Configuration config=new Configuration().configure(); //config.addClass(Student.class); //創(chuàng)建服務注冊對象 ServiceRegistry serviceRegistry =new ServiceRegistryBuilder().applySettings(config.getProperties()).buildServiceRegistry(); //創(chuàng)建會話工廠對象 sessionFactory=config.buildSessionFactory(serviceRegistry); //會話對象 session=sessionFactory.openSession(); //開啟事物 transaction=session.beginTransaction(); } @After public void destroy(){ transaction.commit();//提交事物 session.close();//關閉會話 ? ?sessionFactory.close();//關閉會話工廠 } @Test public void testSaveStudent(){ ?Grade g1=new Grade("java一班","java軟件開發(fā)一班"); ? ? student1 stu1=new student1("王五","男"); ?student1 stu2=new student1("李四","男"); ?g1.getStudents().add(stu1); ?g1.getStudents().add(stu2);?session.save(g1); ?session.save(stu1); ?session.save(stu2); }}
添加回答
舉報
0/150
提交
取消