您的位置:首页 > 编程语言 > Java开发

hibernate实现双向一对多的映射

2016-05-21 22:18 423 查看
首先先搭建好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!


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  hibernate orm javaweb