首先在oracle‘【你可以是其他数据库】中建三张表
<!--a表-->
create table a(
id number primary key,
name varchar2(50));
<!--b表-->
creata table b(
id number primary key,
xx varchar2(50));
<!--ab表-->
create table ab(
id number primary key,
a_id number references a(id),
b_id number references b(id));
之后建立映射
A.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="com.model.A" table="A" schema="DRDG">
<id name="id" type="java.math.BigDecimal">
<column name="ID" precision="22" scale="0" />
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="NAME" length="50" />
</property>
<set name="abs" inverse="true">
<key>
<column name="A_ID" precision="22" scale="0" />
</key>
<one-to-many class="com.model.Ab" />
</set>
</class>
</hibernate-mapping>
B.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="com.model.B" table="B" schema="DRDG">
<id name="id" type="java.math.BigDecimal">
<column name="ID" precision="22" scale="0" />
<generator class="native" />
</id>
<property name="xx" type="java.lang.String">
<column name="XX" length="50" />
</property>
<set name="abs" inverse="true">
<key>
<column name="B_ID" precision="22" scale="0" />
</key>
<one-to-many class="com.model.Ab" />
</set>
</class>
</hibernate-mapping>
AB.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="com.model.Ab" table="AB" schema="DRDG">
<id name="id" type="java.math.BigDecimal">
<column name="ID" precision="22" scale="0" />
<generator class="native" />
</id>
<many-to-one name="b" class="com.model.B" fetch="select">
<column name="B_ID" precision="22" scale="0" />
</many-to-one>
<many-to-one name="a" class="com.model.A" fetch="select">
<column name="A_ID" precision="22" scale="0" />
</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="dialect">
org.hibernate.dialect.Oracle9Dialect
</property>
<property name="connection.url">
jdbc:oracle:thin:@127.0.0.1:1521:orcl
</property>
<property name="connection.username">drdg</property>
<property name="connection.password">zxcvbnm</property>
<property name="show_sql">true</property>
<property name="connection.driver_class">
oracle.jdbc.driver.OracleDriver
</property>
<property name="myeclipse.connection.profile">
oracle.jdbc.driver.OracleDriver
</property>
<mapping resource="com/model/Ab.hbm.xml" />
<mapping resource="com/model/B.hbm.xml" />
<mapping resource="com/model/A.hbm.xml" />
</session-factory>
</hibernate-configuration>
测试类
package com.junit;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;
import org.junit.Test;
import com.model.Ab;
import com.util.HibernateSessionFactory;
public class T {
@Test
public void t(){
Session session = HibernateSessionFactory.getSession();
session.beginTransaction();
List<Ab> ab = null;
Query q = session.createQuery("from Ab");
ab = q.list();
for(Ab a:ab){
System.out.println(a.getA().getName());
}
}
}
下载地址:http://download.csdn.net/detail/yao__shun__yu/5087574