这是一个多对一单向关联的一个例子,目的是为了总结一下Hibernate的基础知识。
生成 Classes 和student类
package com.test.hibernate;
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;
}
}
package com.test.hibernate;
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和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.test.hibernate"> <class name="Classes" table="t_classes"> <id name="id"> <generator class="native"/> </id> <property name="name"/> <set name="students"> <key column="classesid"/> <one-to-many class="Student"/> </set> </class> </hibernate-mapping>
XML配置最主要的为:
<set name="students">
<key column="classesid"/>
<one-to-many class="Student"/>
</set>
将Student对应的表添加一个 classesid属性。
package com.test.hibernate; 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; } }
生成hibernate.cfg.xml文件
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.url">jdbc:mysql://localhost/mytest</property> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property> <property name="hibernate.connection.username">root</property> <property name="hibernate.connection.password">123456</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect</property> <property name="hbm2ddl.auto">create</property> <property name="hibernate.show_sql">true</property> <mapping resource="com/test/hibernate/Student.hbm.xml"/> <mapping resource="com/test/hibernate/Classes.hbm.xml"/> </session-factory> </hibernate-configuration>
写JAVA代码进行OR转换
为了避免重复写代码,可以把一些初始化的东西写到一个类中!其它类调用这个类的方法和属性。
HibernateUtils.java如下:
package com.test.hibernate;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class HibernateUtils {
private static SessionFactory factory;
static {
try {
Configuration cfg = new Configuration().configure();
factory = cfg.buildSessionFactory();
}catch(Exception e) {
e.printStackTrace();
}
}
public static SessionFactory getSessionFactory() {
return factory;
}
public static Session getSession() {
return factory.openSession();
}
public static void closeSession(Session session) {
if (session != null) {
if (session.isOpen()) {
session.close();
}
}
}
}
生成主类 TestOne2Many.java
package com.test;
import java.util.HashSet;
import java.util.Set;
import org.hibernate.Session;
import com.test.hibernate.Classes;
import com.test.hibernate.HibernateUtils;
import com.test.hibernate.Student;
public class TestOne2Many {
public static void main(String[] args) {
testSave1();
}
public static void testSave1() {
Session session = null;
try {
session = HibernateUtils.getSession();
session.beginTransaction();
Student student1 = new Student();
student1.setName("10");
session.save(student1);
Student student2 = new Student();
student2.setName("祖儿");
session.save(student2);
Set students = new HashSet();
students.add(student1);
students.add(student2);
Classes classes = new Classes();
classes.setName("尚学堂");
classes.setStudents(students);
session.save(classes);
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
HibernateUtils.closeSession(session);
}
}
}