班级类
package hibernate;
import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;
public class Grade implements Serializable {
private int gid;
private String sname;
private String gdesc;
private Set<student1> students=new HashSet<student1>();
public Grade(int gid, String sname, String gdesc, Set<student1> students) {
super();
this.gid = gid;
this.sname = sname;
this.gdesc = gdesc;
this.students = students;
}
public Grade() {
super();
}
public int getGid() {
return gid;
}
public void setGid(int gid) {
this.gid = gid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getGdesc() {
return gdesc;
}
public void setGdesc(String gdesc) {
this.gdesc = gdesc;
}
public Set<student1> getStudents() {
return students;
}
public void setStudents(Set<student1> students) {
this.students = students;
}
public Grade(String sname, String gdesc) {
super();
this.sname = sname;
this.gdesc = gdesc;
}
}
学生类
package hibernate;
import java.io.Serializable;
public class student1 implements Serializable{
private int sid;
private String sname;
private String sex;
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public student1(String sname, String sex) {
super();
this.sname = sname;
this.sex = sex;
}
}
班级hbm类
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2017-3-25 22:16:53 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="hibernate.Grade" table="grade1">
<id name="gid" type="int">
<column name="GID" />
<generator class="increment" />
</id>
<property name="sname" type="java.lang.String">
<column name="SNAME" />
</property>
<property name="gdesc" type="java.lang.String">
<column name="GDESC" />
</property>
<set name="students" table="student1" inverse="false" lazy="true">
<key>
<column name="gid" />
</key>
<one-to-many class="hibernate.student1" />
</set>
</class>
</hibernate-mapping>
学生hbm
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- Generated 2017-3-25 22:16:53 by Hibernate Tools 3.5.0.Final -->
<hibernate-mapping>
<class name="hibernate.student1" table="student1">
<id name="sid" type="int">
<column name="SID" />
<generator class="increment" />
</id>
<property name="sname" type="java.lang.String">
<column name="SNAME" />
</property>
<property name="sex" type="java.lang.String">
<column name="SEX" />
</property>
</class>
</hibernate-mapping>
配置文档
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="connection.username">root</property>
<property name="connection.password">123456</property>
<property name="connection.driver_name">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/student3?useUnicode=true&characterEncoding=UTF-8</property>
<property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect </property>
<property name="show_sql">true</property>
<property name="format_sql">true</property>
<property name="hbm2ddl.auto">create</property>
<mapping resource="student1.hbm.xml"/>
<mapping resource="Grade.hbm.xml"/>
</session-factory>
</hibernate-configuration>
测试类
package hibernate;
import java.util.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class StudentsTest {
private SessionFactory sessionFactory;
private Session session;
private Transaction transaction;
@Before
public void init(){
//创建配置对象
Configuration config=new Configuration().configure();
config.addClass(Grade.class);
config.addClass(student1.class);
//创建服务注册对象
ServiceRegistry serviceRegistry =new StandardServiceRegistryBuilder().applySettings(config.getProperties()).build();
//创建会话工厂对象
sessionFactory=config.buildSessionFactory(serviceRegistry);
//会话对象
session=sessionFactory.openSession();
//开启事物
transaction=session.beginTransaction();
}
@After
public void destroy(){
transaction.commit();
transaction.commit();//提交事物
session.close();//关闭会话
sessionFactory.close();//关闭会话工厂
}
@Test
public void testSaveStudent(){
Grade g=new Grade("java二班","java软件开发一班");
student1 stu1=new student1("王五","男");
student1 stu2=new student1("赵六","男");
g.getStudents().add(stu1);
g.getStudents().add(stu2);
session.save(g);
session.save(stu1);
session.save(stu2);
}
@Test
public void testsearchGrade(){
Grade grade=(Grade)session.get(Grade.class, 1);
System.out.println(grade);
}
只要测试 testsearchGrade() 方法 数据表就会把以前的数据清空
相关分类