JAR包导入
hibernate3.jar
\lib\required*.jar
\lib\jpa*.jar
数据库驱动包:mysql-connector-java-5.1.32-bin.jar
完整多对一配置详解代码
项目结构
代码
Father.java
package com.ning.dao;
import java.io.Serializable;
/**
* Father 实体类
* private int id;
* private String name;
* private String age;
* @author wangn
*
*/
public class Father implements Serializable{
private int id;
private String name;
private String age;
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 String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
Son.java
package com.ning.dao;
import java.io.Serializable;
/**
* Son 实体类
* private int sid;
* private String name;
* private Father father;
* @author wangn
*
*/
public class Son implements Serializable{
private int sid;
private String name;
private Father father;
public int getSid() {
return sid;
}
public void setSid(int sid) {
this.sid = sid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Father getFather() {
return father;
}
public void setFather(Father father) {
this.father = father;
}
}
Father.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.ning.dao">
<class name="Father" table="father">
<!-- 主键 ,映射-->
<id name="id" column="id" type="integer">
<generator class="native"/>
</id>
<!-- 非主键,映射 -->
<property name="name" column="name" type="string"></property>
<property name="age" column="age" type="string"></property>
</class>
</hibernate-mapping>
Son.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.ning.dao">
<class name="Son" table="son">
<!-- 主键 ,映射-->
<id name="sid" column="sid" type="integer">
<generator class="native"/>
</id>
<!-- 非主键,映射 -->
<property name="name" column="name" type="string"></property>
<!-- 多对一 -->
<many-to-one name="father" column="ids" class="Father"></many-to-one>
</class>
</hibernate-mapping>
hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory name="">
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.connection.url">jdbc:mysql:///ning</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<!-- 数据库方言 -->
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- 控制台打印SQL语句 -->
<property name="hibernate.show_sql">true</property>
<!-- <property name="hibernate.format_sql">true</property> -->
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping resource="com/ning/dao/Son.hbm.xml"/>
<mapping resource="com/ning/dao/Father.hbm.xml"/>
</session-factory>
</hibernate-configuration>
Test1.java
package com.ning.test;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import com.ning.dao.Father;
import com.ning.dao.Son;
public class Test1 {
public static void main(String[] args) {
// 获取加载配置文件的管理类对象
Configuration config = new Configuration();
config.configure(); // 默认加载src/hibenrate.cfg.xml文件
// 创建session的工厂对象
SessionFactory sf = config.buildSessionFactory();
// 创建session (代表一个会话,与数据库连接的会话)
Session session = sf.openSession();
// 开启事务
Transaction tx = session.beginTransaction();
Father father =new Father();
Son son1 = new Son();
Son son2 = new Son();
father.setName("父亲1");
father.setAge("40");
son1.setName("儿子1");
son1.setFather(father);
son2.setName("儿子2");
son2.setFather(father);
session.save(father);
session.save(son1);
session.save(son2);
// 提交事务
tx.commit();
// 关闭
session.close();
sf.close();
}
}
数据库结果
查询结果
Son son = (Son) session.get(Son.class, 1);
System.out.println(son.getSid());
System.out.println(son.getName());
System.out.println(son.getFather().getName());//懒加载
控制台打印结果:
Hibernate: select son0_.sid as sid0_0_, son0_.name as name0_0_, son0_.ids as ids0_0_ from son son0_ where son0_.sid=?
1
儿子1
Hibernate: select father0_.id as id1_0_, father0_.name as name1_0_, father0_.age as age1_0_ from father father0_ where father0_.id=?
父亲1