hibernate实现双向一对多的映射

首先先搭建好hebernate的开发环境,导入hibernate所需要的各种jar包

在src目录下导入一个 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>
		<!--配置数据库的基本信息-->
		<property name="connection.username">root</property>
		<property name="connection.password">root</property>
		<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
		<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
		
		<!-- 配置hibernate的基本信息 -->
		<!-- hibernate所使用的数据库的方言 -->
		<property name="dialect">org.hibernate.dialect.MySQLDialect</property>
		<!-- 执行操作时是否在控制台打印sql -->
		<property name="show_sql">true</property>
		<!-- 是否格式化sql语句 -->
		<property name="format_sql">true</property>
		<!-- 指定自动生成数据库的策略 -->
		<property name="hbm2ddl.auto">update</property>
		
		<!-- 集成c3p0连接池 -->
		<property name="hibernate.c3p0.min_size">5</property>
		<property name="hibernate.c3p0.max_size">10</property>
		<property name="hibernate.c3p0.timeout">2000</property>
		<property name="hibernate.c3p0.max_statements">10</property>
		<property name="hibernate.c3p0.idle_test_period">2000</property>
		<property name="hibernate.c3p0.acquire_increment">10</property>
		
		<!-- 设定jdbc的statement每次从数据库中读取的记录数 oracle数据库可用-->
		<property name="hibernate.jdbc.fetch_size">100</property>
		<!-- 设定对数据库进行批量删除,批量插入时的批次大小 -->
		<property name="hibernate.jdbc.batch_size">30</property>
		
		<!-- 指定关联的.hbm.xml  -->
		<mapping resource="cn/lfd/hibernate/n21/both/Customer.hbm.xml"/>
		<mapping resource="cn/lfd/hibernate/n21/both/Order.hbm.xml"/>
	</session-factory>
</hibernate-configuration>

Customer类:


package cn.lfd.hibernate.n21.both;
import java.util.HashSet;
import java.util.Set;
public class Customer {
	private Integer customerID;
	private String customerName;
	private Set<Order> orders = new HashSet<Order>();
	
	
	public Set<Order> getOrders() {
		return orders;
	}
	public void setOrders(Set<Order> orders) {
		this.orders = orders;
	}
	public Integer getCustomerID() {
		return customerID;
	}
	public void setCustomerID(Integer customerID) {
		this.customerID = customerID;
	}
	public String getCustomerName() {
		return customerName;
	}
	public void setCustomerName(String customerName) {
		this.customerName = customerName;
	}
	public Customer() {
		super();
		// TODO Auto-generated constructor stub
	}
}
Order类:

package cn.lfd.hibernate.n21.both;
public class Order {
	private Integer orderID;
	private String orderName;
	private Customer customer;
	public Integer getOrderID() {
		return orderID;
	}
	public void setOrderID(Integer orderID) {
		this.orderID = orderID;
	}
	public String getOrderName() {
		return orderName;
	}
	public void setOrderName(String orderName) {
		this.orderName = orderName;
	}
	public Customer getCustomer() {
		return customer;
	}
	public void setCustomer(Customer customer) {
		this.customer = customer;
	}
	public Order(Integer orderID, String orderName, Customer customer) {
		super();
		this.orderID = orderID;
		this.orderName = orderName;
		this.customer = customer;
	}
	public Order() {
		super();
		// TODO Auto-generated constructor stub
	}
	
}
为每一个持久化类生成一个className.hbm.xml文件

Customer.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">
<!-- Generated 2014-9-29 8:46:32 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping package="cn.lfd.hibernate.n21.both">
    <class name="Customer" table="CUSTOMER">
        <id name="customerID" type="java.lang.Integer">
            <column name="CUSTOMER_ID" />
            <generator class="native" />
        </id>
        <property name="customerName" type="java.lang.String">
            <column name="CUSTOMER_NAME" length="40"/>
        </property>
        
        <!-- inverse指定由哪一方维护关联关系 通常设置为true指定多的一方维护关联关系 -->
        <set name="orders" table="ORDERS" inverse="true">
        	<key column="CUSTOMER_ID"></key>
        	<one-to-many class="Order"/>
        </set>
    </class>
</hibernate-mapping>
Order.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">
<!-- Generated 2014-9-29 8:46:32 by Hibernate Tools 3.4.0.CR1 -->
<hibernate-mapping package="cn.lfd.hibernate.n21.both">
    <class name="Order" table="ORDERS">
        <id name="orderID" type="java.lang.Integer">
            <column name="ORDER_ID" />
            <generator class="native" />
        </id>
        <property name="orderName" type="java.lang.String">
            <column name="ORDER_NAME"/>
        </property>
        
        <!-- 映射多对一的关系 -->
        <many-to-one name="customer" column="CUSTOMER_ID" class="Customer"></many-to-one>
    </class>
</hibernate-mapping>
这样一个双向一对多的关系就完成了,ok!


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值