many to one
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/hibernate</property>
<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
<property name="myeclipse.connection.profile">mysqlCon</property>
<property name="connection.password">123456</property>
<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="show_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping resource="com/yelang/hibernate/Class.hbm.xml"/>
<mapping resource="com/yelang/hibernate/Student.hbm.xml"/>
</session-factory>
</hibernate-configuration>
HibernateUtils.java package com.yelang.hibernate;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
public class hibernateUtils {
/**
* sessionFactory
*/
private static SessionFactory sf ;
/**
* sessionFactory
*/
static {
try {
sf = new Configuration().configure().buildSessionFactory();
} catch (HibernateException e) {
e.printStackTrace();
}
}
/**
* session
*/
public static Session getSession() {
return sf.openSession();
}
/**
* Session
*/
public static void closeSession(Session session) {
if(session != null) {
if(session.isOpen()) {
session.close();
}
}
}
}
class.java package com.yelang.hibernate;
public class Class {
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;
}
}
Student.java package com.yelang.hibernate;
public class Student {
private int id;
private String stuName;
private Class classer;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getStuName() {
return stuName;
}
public void setStuName(String stuName) {
this.stuName = stuName;
}
public Class getClasser() {
return classer;
}
public void setClasser(Class classer) {
this.classer = classer;
}
}
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.yelang.hibernate">
<class name="Class" table="tb_class">
<id name="id">
<generator class="native"/>
</id>
<property name="name"/>
</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.yelang.hibernate">
<class name="Student" table="tb_student">
<id name="id">
<generator class="native"/>
</id>
<property name="stuName"/>
<many-to-one name="classer" column="classId" cascade="save-update"/>
</class>
</hibernate
TestUser.java package com.yelang.hibernate;
import org.hibernate.Session;
import junit.framework.TestCase;
public class StudentTest extends TestCase {
public void testSave() {
Session session = null;
try{
session = hibernateUtils.getSession();
session.beginTransaction();
Class class1 = new Class();
class1.setName("07 ");
Student student = new Student();
student.setClasser(class1);
student.setStuName("###");
session.save(student);
Student student2 = new Student();
student2.setClasser(class1);
student2.setStuName("gg");
session.save(student2);
session.getTransaction().commit();
}catch(Exception e) {
e.printStackTrace();
session.getTransaction().rollback();
}finally {
hibernateUtils.closeSession(session);
}
}
}