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;
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"/>
</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 testSave1()
{
Session session=null;
try {
session=(Session) HibernateSessionFactory.getSession();
session.beginTransaction();
Student student1=new Student();
student1.setName("张三");
Student student2=new Student();
student2.setName("李四");
Set students=new HashSet();
students.add(student1);
students.add(student2);
Classes classes=new Classes();
classes.setName("软开java1");
classes.setStudents(students);
session.save(classes);
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();
SessionFactory sf=cfg.buildSessionFactory();
SchemaExport export=new SchemaExport(cfg);
export.create(true, true);
}
}