org.hibernate.NonUniqueObjectException: a different object with the same identifier value was already associated with the session: [hibernate.student1#0]這是報(bào)錯(cuò)的信息這是測試類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)建服務(wù)注冊對象 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();//關(guān)閉會話 ? ?sessionFactory.close();//關(guān)閉會話工廠 } @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); }}
HIbernate 一對多關(guān)系只能保存一個(gè)
好好認(rèn)真學(xué)習(xí)
2017-08-10 22:23:39