Hibernate中的one2many 单向关联

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);
}


}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值