package cn.bdqn.studentInfo.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import cn.bdqn.studentInfo.entity.Student;
import cn.bdqn.studentInfo.entity.Teacher;
public class testStu {
private Configuration conf=null;
private SessionFactory sessionFac=null;
private Session session=null;
private Transaction tx=null;
@Test
public void createDB(){
Configuration conf=new Configuration()
.configure();
SchemaExport export =new SchemaExport(conf);
export.create(true, true);
}
@Test
public void testMethod(){
Session session=new Configuration().configure().buildSessionFactory().openSession();
Transaction tx=session.beginTransaction();
try{
Student student1=new Student();
student1.setName("张同学");
student1.setId(9);
Student student2=new Student();
student2.setName("王同学");
student2.setId(10);
Teacher teacher1=new Teacher();
teacher1.setId(9);
teacher1.setName("武老师");
Teacher teacher2=new Teacher();
teacher2.setId(10);
teacher2.setName("程老师");
student1.getTeachers().add(teacher1);
student1.getTeachers().add(teacher2);
student2.getTeachers().add(teacher1);
student2.getTeachers().add(teacher2);
teacher1.getStudents().add(student1);
teacher1.getStudents().add(student2);
teacher2.getStudents().add(student1);
teacher2.getStudents().add(student2);
session.save(student1);
session.save(student2);
tx.commit();
}catch (Exception e) {
tx.rollback();
e.printStackTrace();
}
}
@After
public void closeSession(){
if(session!=null){
session.close();
}
}
}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步