hibernate实现双向一对多的映射
2016-05-21 22:18
423 查看
首先先搭建好hebernate的开发环境,导入hibernate所需要的各种jar包
在src目录下导入一个 hibernate.cfg.xml配置文件
Customer类:
Customer.hbm.xml文件:
在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!
相关文章推荐
- ASP也使用ORM,给ASP上所有的SQL注入画上句号
- javaweb之web入门基础
- Hibernate Oracle sequence的使用技巧
- jsp Hibernate批量更新和批量删除处理代码
- jsp hibernate的分页代码第1/3页
- Struts2+Hibernate实现数据分页的方法
- Hibernate环境搭建与配置方法(Hello world配置文件版)
- 仿orm自动生成分页SQL分享
- JAVA+Hibernate 无限级分类
- SSH整合中 hibernate托管给Spring得到SessionFactory
- jsp hibernate 数据保存操作的原理
- hibernate中的增删改查实现代码
- 解决hibernate+mysql写入数据库乱码
- java优化hibernate性能的几点建议
- java Hibernate延迟加载
- hibernate 常用方法介绍
- 详解Java的Hibernate框架中的注解与缓存
- 浅析Java的Hibernate框架中的继承关系设计
- Hibernate实现批量添加数据的方法
- 深入理解:单一入口、MVC、ORM、CURD、ActiveRecord概念