想飞就别怕摔

大爷的并TM骂人

Hibernate学习(五)---【一对多单项】

Classes和Student一对多。要站在classes这段来看。
classes.java
package com.zzn.hibernate.oneToMany;

import java.util.Set;

public class Classes {
    
private int id;
    
private String name;
    
private Set students;

    
public Set getStudents() {
        
return students;
    }
    
public void setStudents(Set students) {
        
this.students = 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;
    }
    
    
}

Student.java
package com.zzn.hibernate.oneToMany;

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

}


Class.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.zzn.hibernate.oneToMany">
    
<class name="Classes" table="CLASSES">
        
<id name="id" column="id">
            
<generator class="native" />
        
</id>
        
<property name="name" />
        
<!--cascade="all"才能操作classes时级联操作student  -->
        
<set name="students" cascade="all">
            
<!-- key 这里不能是not-null="true" -->
            
<key column="classesid"/>
            
<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>

    
<class name="com.zzn.hibernate.oneToMany.Student" table="STUDENT">
        
<id name="id" column="id">
            
<generator class="native" />
        
</id>
        
<property name="name" />
    
</class>

</hibernate-mapping>

test.java
package com.zzn.hibernate;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import junit.framework.TestCase;

import org.hibernate.Session;
import org.hibernate.Transaction;

import com.zzn.hibernate.hibernateUtil.HibernateUtils;
import com.zzn.hibernate.oneToMany.Classes;
import com.zzn.hibernate.oneToMany.Student;

public class OneToManyTest extends TestCase {
    
    @SuppressWarnings(
"unchecked")
    
public void OneToManySave() {
        Session session 
= null;
        Transaction tx 
= null;
        
try {
            session 
= HibernateUtils.getSession();
            tx 
= 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(
"中国");
            classes.setStudents(students);
            
            session.save(classes);
            
        } 
catch (Exception e) {
            e.printStackTrace();
        } 
finally {
            
if (session != null) {
                
if (session.isOpen()) {
                    tx.commit();
                    session.close();
                }
            }
        }
    }
    
    @SuppressWarnings(
"unchecked")
    
public void OneToManyLoad() {
        Session session 
= null;
        Transaction tx 
= null;
        
try {
            session 
= HibernateUtils.getSession();
            tx 
= session.beginTransaction();
            Classes classes 
= (Classes)session.load(Classes.class1);
            Set students 
= classes.getStudents();
            
for (Iterator iter=students.iterator(); iter.hasNext();){
                Student student 
= (Student)iter.next();
                System.out.println(
"student.name="+student.getName());
            }
            
        } 
catch (Exception e) {
            e.printStackTrace();
        } 
finally {
            
if (session != null) {
                
if (session.isOpen()) {
                    tx.commit();
                    session.close();
                }
            }
        }
    }
}

posted on 2009-11-01 16:01 生命的绽放 阅读(459) 评论(0)  编辑  收藏 所属分类: Hibernate


只有注册用户登录后才能发表评论。


网站导航:
 
<2009年11月>
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345

导航

统计

常用链接

留言簿(5)

随笔分类(94)

随笔档案(93)

文章分类(5)

文章档案(5)

相册

JAVA之桥

SQL之音

兄弟之窗

常用工具下载

积分与排名

最新评论

阅读排行榜