这里错了个英文单词,我想说班级,却写了grade,这里就把grade当成班级来看吧
^_^ ^_^
^_^
数据表字段:
表grade:id,name
表student:id,name,grade_id
关系:
one -to-many :班级表 -- 对-- 学生表
many-to-one :学生表-- 对 -- 班级表
Grade.java
public class Grade implements java.io.Serializable {
private Integer id;
private String name;
private Set<Student> students;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Set<Student> getStudents() {
return students;
}
public void setStudents(Set<Student> students) {
this.students = students;
}
}
Student.java
public class Student implements java.io.Serializable {
private Integer id;
private String name;
private Grade grade;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Grade getGrade() {
return grade;
}
public void setGrade(Grade grade) {
this.grade = grade;
}
}
Grade.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="Grade" table="grade" catalog="testdb">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="name" length="20" />
</property>
<set name="students">
<key column="grade_id"></key>
<one-to-many class="Student"/>
</set>
</class>
</hibernate-mapping>
Student.hbm.xml
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="Student" table="student" catalog="testdb">
<id name="id" type="java.lang.Integer">
<column name="id" />
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="name" length="20" />
</property>
<many-to-one name="grade" class="Grade">
<column name="grade_id"></column>
</many-to-one>
</class>
</hibernate-mapping>
hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>
<session-factory>
<property name="connection.username">root</property>
<property name="connection.url">
jdbc:mysql://localhost:3306
</property>
<property name="dialect">
org.hibernate.dialect.MySQLDialect
</property>
<property name="myeclipse.connection.profile">
com.mysql.jdbc.Driver
</property>
<property name="connection.password">pass</property>
<property name="connection.driver_class">
com.mysql.jdbc.Driver
</property>
<mapping resource="./Grade.hbm.xml" />
<mapping resource="./Student.hbm.xml" />
</session-factory>
</hibernate-configuration>
TestMapping.java
import java.util.Iterator;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class TestMapping {
private SessionFactory factory;
public TestMapping() {
Configuration config = new Configuration().configure();
factory = config.buildSessionFactory();
}
public void testOne2Many() {
Session session = factory.openSession();
Grade grade = (Grade) session.get(Grade.class, 1);
if (grade == null) {
System.out.println("No suce grade id, Break.");
return;
}
System.out.println("Grade Name: " + grade.getName());
System.out.println("Student in Grade : [" + grade.getId() + "]");
Iterator<Student> iter = grade.getStudents().iterator();
Student student = null;
while (iter.hasNext()) {
student = iter.next();
System.out.print("Student ID: " + student.getId() + " ");
System.out.println("Student Name: " + student.getName());
}
session.close();
}
public void testMany2One() {
Session session = factory.openSession();
Student student = (Student) session.get(Student.class, 3);
if (student == null) {
System.out.println("No suce student id, Break.");
return;
}
System.out.print("Student ID: " + student.getId() + " ");
System.out.println("Student Name: " + student.getName());
System.out.println("The student is belong to Grade : [ "
+ student.getGrade().getName() + " ]");
session.close();
}
public static void main(String[] args) {
TestMapping tm = new TestMapping();
tm.testOne2Many();
tm.testMany2One();
}
}
=================================END=============================