hibernate 一对多 关系关联映射
2021/6/2 10:24:02
本文主要是介绍hibernate 一对多 关系关联映射,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
首先在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
这篇关于hibernate 一对多 关系关联映射的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-26消息中间件源码剖析教程
- 2024-11-26JAVA语音识别项目资料的收集与应用
- 2024-11-26Java语音识别项目资料:入门级教程与实战指南
- 2024-11-26SpringAI:Java 开发的智能新利器
- 2024-11-26Java云原生资料:新手入门教程与实战指南
- 2024-11-26JAVA云原生资料入门教程
- 2024-11-26Mybatis官方生成器资料详解与应用教程
- 2024-11-26Mybatis一级缓存资料详解与实战教程
- 2024-11-26Mybatis一级缓存资料详解:新手快速入门
- 2024-11-26SpringBoot3+JDK17搭建后端资料详尽教程