Classes类
import java.util.Set;
public class Classes {
private int id;
private String name;
private Set students;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set getStudents() {
return students;
}
public void setStudents(Set students) {
this.students = students;
}
}
---------------------------------------------------------------------
Student 类
public class Student {
private int id;
private String name;
private Classes classes;
public Classes getClasses() {
return classes;
}
public void setClasses(Classes classes) {
this.classes = classes;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
--------------------------------------
Classes.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.soft.gread1">
<class name="Classes" table="t_classes">
<id name="id" >
<generator class="native"/>
</id>
<property name="name"/>
<set name="students" cascade="save-update">
<key column="classId"></key>
<one-to-many class="Student"/>
</set>
</class>
</hibernate-mapping>
------------------------------------------------------------------------------------------
Student.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.soft.gread1">
<class name="Student" table="t_student">
<id name="id" >
<generator class="native"/>
</id>
<property name="name"/>
<many-to-one name="classes" column="classid" cascade="save-update"/>
</class>
</hibernate-mapping>
----------------------------------------------
测试类
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import org.hibernate.Session;
import junit.framework.TestCase;
import com.soft.gread1.Classes;
import com.soft.gread1.Student;
import com.soft.gread1.comm.HibernateSessionFactory;
public class Test extends TestCase{
public void testSave2()
{
Session session=null;
try {
session=(Session) HibernateSessionFactory.getSession();
session.beginTransaction();
Classes classes=new Classes();
classes.setName("软开java1");
Student student1=new Student();
student1.setName("张三");
student1.setClasses(classes);
Student student2=new Student();
student2.setName("李四");
student2.setClasses(classes);
session.save(student1);
session.save(student2);
session.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally
{
HibernateSessionFactory.closeSession();
}
}
public void testLoad1()
{
Session session=null;
try {
session=(Session) HibernateSessionFactory.getSession();
session.beginTransaction();
Classes classes=(Classes) session.load(Classes.class, 1);
System.out.println("班级号:"+classes.getId());
System.out.println("班级名称:"+classes.getName());
Set students=classes.getStudents();
for(Iterator ite=students.iterator();ite.hasNext();)
{
Student student=(Student) ite.next();
System.out.print("学生Id:"+student.getId()+"\t");
System.out.print("学生姓名:"+student.getName()+"\t");
System.out.println("学生所在班级:"+student.getClasses().getName());
}
session.getTransaction().commit();
} catch (Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally
{
HibernateSessionFactory.closeSession();
}
}
}
---------------------------------------------------------
自动生成表 类
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.exception.Configurable;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class AutoCreateTab {
public static void main(String[] args) {
Configuration cfg=new Configuration().configure();
SchemaExport export=new SchemaExport(cfg);
export.create(true, true);
}
}
id | name |
1 | 软开java1 |
id | name | classid |
| 张三 | 1 |