- 浏览: 320420 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (211)
- Java IO (3)
- Struts (13)
- Hibernate (19)
- Spring (9)
- jQuery (2)
- Extjs (8)
- Flex (1)
- Oracle (4)
- ibatis (3)
- SQL (0)
- WorkFlow (0)
- Java Core (14)
- Ant&Maven (18)
- Java Thread (0)
- Java Socket (1)
- Java OO (2)
- Javascript (14)
- CSS&Div (2)
- MYSQL (3)
- MSSQL (0)
- Design Pattern (3)
- Data Structure&Algorithm (1)
- Source Analysis (0)
- OSGi (3)
- Linux (7)
- Network (1)
- 百无聊赖 (9)
- English (5)
- Japaness (0)
- French (0)
- Webservice (3)
- 考试认证 (0)
- C/C++ (1)
- 小题目 (1)
- Server (1)
- JSP&Servlet (18)
- JDBC (8)
- JMS (3)
- JNDI (0)
- 软件工程 (2)
- 项目管理 (2)
- UML (0)
- 文档翻译 (0)
- 架构之美 (1)
- EJB (0)
- DataBase Design (1)
- DataBase (1)
- Framework Integration (2)
- JPA (2)
- Daily Reading (8)
- 通用组件设计 (3)
- Spring DM (1)
- Spring MVC (0)
- Spring Security (0)
- 时间管理 (0)
- 成本管理 (1)
- 进度管理 (0)
- 质量管理 (0)
- 范围管理 (0)
- 环境配置 (5)
- 敏捷开发 (0)
- DB2 (2)
- 持续集成 (1)
- JVM性能调优 (2)
- Weblogic (0)
- PHP (1)
- Websphere (2)
最新评论
-
di1984HIT:
写的很好,谢谢。。
【转载】【权限控制】角色访问动态生成用户权限菜单树 -
paladin1988:
wangxuehui 写道我昨天也参加视频面试,视频面试2个人 ...
阿里巴巴面试感想 -
wangxuehui:
我昨天也参加视频面试,视频面试2个人,最后都听我说完啦,最后我 ...
阿里巴巴面试感想 -
paladin1988:
liwei113114 写道请问一下,你们最后是怎么解决tld ...
OSGi动态拔插,动态部署,动态权限菜单 -
liwei113114:
请问一下,你们最后是怎么解决tld那个问题的?
我现在也遇到这 ...
OSGi动态拔插,动态部署,动态权限菜单
Hibernate基于外键的一对多单向关联
- 博客分类:
- Hibernate
一对多,一个分类Category下有多个Product,从Cateogry角度去保存数据。
创建数据库脚本如下:(参看附件)
-- MySQL dump 10.13 Distrib 5.1.55, for Win32 (ia32) -- -- Host: localhost Database: hibernate_demo -- ------------------------------------------------------ -- Server version 5.1.55-community /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8 */; /*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */; /*!40103 SET TIME_ZONE='+00:00' */; /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; -- -- Table structure for table `category` -- DROP TABLE IF EXISTS `category`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `category` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(30) NOT NULL, `description` varchar(30) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=latin1; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `category` -- LOCK TABLES `category` WRITE; /*!40000 ALTER TABLE `category` DISABLE KEYS */; INSERT INTO `category` VALUES (1,'fruit','fruit category'); /*!40000 ALTER TABLE `category` ENABLE KEYS */; UNLOCK TABLES; -- -- Table structure for table `product` -- DROP TABLE IF EXISTS `product`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `product` ( `id` int(11) NOT NULL, `name` varchar(30) NOT NULL, `price` int(11) NOT NULL, `description` varchar(30) NOT NULL, `category_id` int(11) NOT NULL, PRIMARY KEY (`id`), KEY `product_fk` (`category_id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `product` -- LOCK TABLES `product` WRITE; /*!40000 ALTER TABLE `product` DISABLE KEYS */; INSERT INTO `product` VALUES (1,'orige',20,'orige',1),(2,'apple',10,'apple',1); /*!40000 ALTER TABLE `product` ENABLE KEYS */; UNLOCK TABLES; /*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */; /*!40101 SET SQL_MODE=@OLD_SQL_MODE */; /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; -- Dump completed on 2012-08-09 21:40:19
这是一个非常不错的例子,呵呵,至少整个过程大家可以很清楚的明白。。
//Category.java
public class Category implements java.io.Serializable { // Fields private Integer id; private String name; private String description; private Set<Product> products = new HashSet<Product>();
//Product.java
public class Product implements java.io.Serializable { // Fields private Integer id; private String name; private Integer price; private String description;
//Category.hbm.xml
<!-- 基于外键的单向一对多关联 --> <!-- 单向一对多在一端进行配置 --> <!-- set的name代表一端的set属性,table指向多端的关联表--> <!-- key的column属性代表外键 --> <!-- cascade="save-update" ,使用保存数据的级联操作--> <set name="products" table="product" cascade="save-update"> <key column="category_id" not-null="true" /> <one-to-many class="com.v512.examples.Product"/> </set>
//HibernateTest.java
//保存数据
public static void addProduct() { Product product = new Product(); product.setDescription("apple"); product.setName("apple"); product.setPrice(10); Product product1 = new Product(); product1.setDescription("orige"); product1.setName("orige"); product1.setPrice(20); Category category = new Category(); category.setDescription("fruit category"); category.setName("fruit"); Set<Product> products = new HashSet<Product>(); products.add(product); products.add(product1); category.setProducts(products); Session session = HibernateUtil.getSessionFactory().openSession(); Transaction tx = session.beginTransaction(); session.save(category); //启用级联操作 // session.save(product); // session.save(product1); tx.commit(); session.close(); HibernateUtil.shutdown(); }
输入信息如下:
21:15:48,906 DEBUG JDBCTransaction:87 - current autocommit status: false 21:15:48,906 DEBUG IncrementGenerator:104 - fetching initial value: select max(id) from category 21:15:48,906 DEBUG AbstractBatcher:410 - about to open PreparedStatement (open PreparedStatements: 0, globally: 0) 21:15:48,937 DEBUG SQL:111 - select max(id) from category Hibernate: select max(id) from category 21:15:48,953 DEBUG IncrementGenerator:119 - first free id: 1 21:15:48,953 DEBUG AbstractBatcher:418 - about to close PreparedStatement (open PreparedStatements: 1, globally: 1) 21:15:48,953 DEBUG AbstractSaveEventListener:135 - generated identifier: 1, using strategy: org.hibernate.id.IncrementGenerator 21:15:48,968 DEBUG IncrementGenerator:104 - fetching initial value: select max(id) from product 21:15:48,968 DEBUG AbstractBatcher:410 - about to open PreparedStatement (open PreparedStatements: 0, globally: 0) 21:15:48,968 DEBUG SQL:111 - select max(id) from product Hibernate: select max(id) from product 21:15:48,968 DEBUG IncrementGenerator:119 - first free id: 1 21:15:48,968 DEBUG AbstractBatcher:418 - about to close PreparedStatement (open PreparedStatements: 1, globally: 1) 21:15:48,968 DEBUG AbstractSaveEventListener:135 - generated identifier: 1, using strategy: org.hibernate.id.IncrementGenerator 21:15:48,968 DEBUG AbstractSaveEventListener:135 - generated identifier: 2, using strategy: org.hibernate.id.IncrementGenerator 21:15:48,968 DEBUG JDBCTransaction:134 - commit 21:15:48,968 DEBUG AbstractFlushingEventListener:134 - processing flush-time cascades 21:15:48,968 DEBUG AbstractFlushingEventListener:177 - dirty checking collections 21:15:48,968 DEBUG Collections:199 - Collection found: [com.v512.examples.Category.products#1], was: [<unreferenced>] (initialized) 21:15:48,984 DEBUG AbstractFlushingEventListener:108 - Flushed: 3 insertions, 0 updates, 0 deletions to 3 objects 21:15:48,984 DEBUG AbstractFlushingEventListener:114 - Flushed: 1 (re)creations, 0 updates, 0 removals to 1 collections 21:15:48,984 DEBUG Printer:106 - listing entities: 21:15:48,984 DEBUG Printer:113 - com.v512.examples.Product{id=1, price=10, description=apple, name=apple} 21:15:48,984 DEBUG Printer:113 - com.v512.examples.Category{id=1, description=fruit category, name=fruit, products=[com.v512.examples.Product#2, com.v512.examples.Product#1]} 21:15:48,984 DEBUG Printer:113 - com.v512.examples.Product{id=2, price=20, description=orige, name=orige} 21:15:48,984 DEBUG AbstractBatcher:410 - about to open PreparedStatement (open PreparedStatements: 0, globally: 0) 21:15:48,984 DEBUG SQL:111 - insert into hibernate_demo.category (name, description, id) values (?, ?, ?) Hibernate: insert into hibernate_demo.category (name, description, id) values (?, ?, ?) 21:15:48,984 DEBUG AbstractBatcher:66 - Executing batch size: 1 21:15:48,984 DEBUG AbstractBatcher:418 - about to close PreparedStatement (open PreparedStatements: 1, globally: 1) 21:15:48,984 DEBUG AbstractBatcher:410 - about to open PreparedStatement (open PreparedStatements: 0, globally: 0) 21:15:48,984 DEBUG SQL:111 - insert into hibernate_demo.product (name, price, description, category_id, id) values (?, ?, ?, ?, ?) Hibernate: insert into hibernate_demo.product (name, price, description, category_id, id) values (?, ?, ?, ?, ?) 21:15:48,984 DEBUG AbstractBatcher:248 - reusing prepared statement 21:15:48,984 DEBUG SQL:111 - insert into hibernate_demo.product (name, price, description, category_id, id) values (?, ?, ?, ?, ?) Hibernate: insert into hibernate_demo.product (name, price, description, category_id, id) values (?, ?, ?, ?, ?) 21:15:48,984 DEBUG AbstractBatcher:66 - Executing batch size: 2 21:15:48,984 DEBUG AbstractBatcher:418 - about to close PreparedStatement (open PreparedStatements: 1, globally: 1) 21:15:48,984 DEBUG AbstractCollectionPersister:1112 - Inserting collection: [com.v512.examples.Category.products#1] 21:15:48,984 DEBUG AbstractBatcher:410 - about to open PreparedStatement (open PreparedStatements: 0, globally: 0) 21:15:48,984 DEBUG SQL:111 - update hibernate_demo.product set category_id=? where id=? Hibernate: update hibernate_demo.product set category_id=? where id=? 21:15:48,984 DEBUG AbstractBatcher:248 - reusing prepared statement 21:15:48,984 DEBUG SQL:111 - update hibernate_demo.product set category_id=? where id=? Hibernate: update hibernate_demo.product set category_id=? where id=? 21:15:48,984 DEBUG AbstractCollectionPersister:1194 - done inserting collection: 2 rows inserted 21:15:48,984 DEBUG AbstractBatcher:66 - Executing batch size: 2 21:15:48,984 DEBUG AbstractBatcher:418 - about to close PreparedStatement (open PreparedStatements: 1, globally: 1) 21:15:48,984 DEBUG JDBCTransaction:147 - committed JDBC Connection 21:15:48,984 DEBUG ConnectionManager:427 - aggressively releasing JDBC connection 21:15:48,984 DEBUG ConnectionManager:464 - releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)] 21:15:49,000 INFO SessionFactoryImpl:853 - closing 21:15:49,000 INFO DriverManagerConnectionProvider:170 - cleaning up connection pool: jdbc:mysql://localhost:3306/hibernate_demo
如果我们不使用级联操作,即cascade=“save-update”,则需要先保存category,然后再保存product。
那么保存数据就是这样
session.save(category); //启用级联操作 session.save(product); session.save(product1); tx.commit();
而启用级联,则只需要保存category就行。
cascade取值如下:
all : 所有情况下均进行关联操作。
none:所有情况下均不进行关联操作。(默认值)
save-update:在执行save/update/saveOrUpdate时进行关联操作。
delete:在执行delete时进行关联操作。
请注意看一下sql,以Hibernate:开头的才是hibernate执行的sql,其他的代表发送给hibernate执行的sql语句。刚开始看错了,还以为hibernate执行了多条sql。
这种方法Hibernate reference称之为
基于外键关联的单向一对多关联是一种很少见的情况,并不推荐使用。
推荐使用:
基于连接表的单向一对多关联 应该优先被采用。请注意,通过指定unique="true",我们可以把多样性从多对多改变为一对多。
- product.rar (927 Bytes)
- 下载次数: 3
发表评论
-
Hibernate非空属性保存异常
2013-05-21 13:24 1210错误描述: Caused by: org.hi ... -
Hibernate读取错误
2013-05-21 13:22 780错误如下: Caused by: com.da ... -
【转】The user must supply a JDBC connection
2012-08-27 01:12 3330The user must supply a JDBC con ... -
Hibernate Annotation小记
2012-08-22 23:43 867老实说,Hibernate Annotation用起来 ... -
Hibernate Annotation 基于外键的一对多双向关联
2012-08-22 23:44 4158纠结了好久呀,因为没有set,关联关系没有保存对,导致插入数据 ... -
【转】Hibernate中的"Repeated column in mapping for entity"异常
2012-08-21 10:31 944文章来源:http://www.iteye.com/t ... -
Hibernate Annotation 唯一外键一对一双向关联
2012-08-20 22:38 1054继续上面的东西,修改下数据库脚本,把共享主键中的Pro ... -
【转】attempted to assign id from null one-to-one
2012-08-20 21:59 811one-to-one在hibernate中可以用来作为两张 ... -
Hibernate Annotation 共享主键一对一双向关联
2012-08-20 21:59 5921写了这么几篇都是单向的关联,干脆后面的都写双向关联吧, ... -
Hibernate Annotation 基于连接表的单向一对多关联
2012-08-17 00:29 2224趁着今晚把一对多一起搞了吧,呵呵。。 一对多的关 ... -
Hibernate的一对多关联方式
2012-08-17 00:29 824之前阅读Hibernate reference的时候看 ... -
Hibernate Annotation 基于外键的单向一对多关联
2012-08-17 00:30 1518基于外键关联的单向一对多关联是一种很少见的情况,并不推荐使用。 ... -
Hibernate Annotation 基于外键的单向多对一关联
2012-08-16 01:26 2878其实一对多和多对一是一样的,只是看问题的角度不同。 ... -
Hibernate Annotation入门
2012-08-15 19:40 1576废话不多说,直接上例子(附件) 数据库脚本: ... -
Hibernate 1+N问题的原理与解决
2012-08-13 20:46 1221什么时候会遇到1+N ... -
【转】Hibernate获取记录总数
2012-08-13 20:41 1117hql获取记录条数 Str ... -
Hibernate基于连接表的一对多单向关联
2012-08-09 23:13 1413基于连接表,顾名思义就是需要建立中间表来保存两个表的关联关系。 ... -
Hibernate使用注解
2012-08-04 01:37 1043//类标志 @Entity @Table(name = & ...
相关推荐
Hibernate 多对多单向关联 Hibernate 一对一外键双向关联 Hibernate 一对一主键双向关联 Hibernate 一对一连接表双向关联 Hibernate 一对多外键双向关联 Hibernate 一对多连接表双向关联 Hibernate 多对多双向关联
Hibernate 一对多 外键 单向关联,有例子。
NULL 博文链接:https://paladin1988.iteye.com/blog/1633417
Hibernate关联关系映射...│ └─ 多对多单向关联 └─双向关联 ├─ 一对一外键双向关联 ├─ 一对一主键双向关联 ├─ 一对一连接表双向关联 ├─ 一对多外键双向关联 ├─ 一对多连接表双向关联 └─ 多对多双向关联
hibernate一对多双向自连接关联映射 15 hibernate多对多关联映射(单向User---->Role) 19 hibernate多对多关联映射(双向User<---->Role) 20 Hibernate的继承关系 21 每棵继承树映射成一张表(hibernate_extends_1) 22 ...
014 一对多关联映射 单向 015 一对多关联映射 双向 016 多对多关联映射 单向 017 多对多关联映射 双向 018 关联映射文件中标签中的 lazy(懒加载)属性 019 关联映射文件中集合标签中的 lazy(懒加载)属性 020 、单端...
一对多的基础上来测试单向多对一的关联 hibernate多对一的关联关系定义: 和单向一对多不同的是:一对多是在意的一方的一方定义set集合,在映射文件中 :单向多对一,简单很多在多的一方定义一的一方类的...
通过在被拥有的实体端(owned entity)增加一个外键列来实现一对多单向关联是很少见的,也是不推荐的,建议通过一个联接表来实现这种关联(下面会讲到)。 @JoinColoumn批注来描述这种单向关联关系 @Entity Public class...
Hibernate 实体关联关系映射 学习总结 把一对一 一对多 单向 双向 主键 外键 链接表等讲的比较清楚
这种策略支持双向的一对多关联,但不支持 IDENTIFY 生成器策略,因为ID必须在多个表间共享。一旦使用就不能使用AUTO和IDENTIFY生成器。 每个类层次结构一张表 @Entity @Inheritance(strategy=InheritanceType....
7.4.1. 一对多(one to many) / 多对一(many to one) 7.4.2. 一对一(one to one) 7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many ...
7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 7.6. 更复杂的关联映射 8. 组件(Component)映射 8.1. 依赖对象(Dependent objects) ...
7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 7.6. 更复杂的关联映射 8. 组件(Component)映射 8.1. 依赖对象(Dependent objects) ...
7.4.1. 一对多(one to many) / 多对一(many to one) 7.4.2. 一对一(one to one) 7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many ...
7.5.1. 一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 7.6. 更复杂的关联映射 8. 组件(Component)映射 8.1. 依赖对象(Dependent objects) 8.2. 在...
8.4.1. 一对多(one to many) / 多对一(many to one) 8.4.2. 一对一(one to one) 8.5. 使用连接表的双向关联(Bidirectional associations with join tables) 8.5.1. 一对多(one to many) /多对一( many ...
7.4.1. 一对多(one to many) / 多对一(many to one) 7.4.2. 一对一(one to one) 7.5. 使用连接表的双向关联(Bidirectional associations with join tables) 7.5.1. 一对多(one to many) /多对一( many to ...
15.3.4 把多对多关联分解为两个一对多关联 15.4 小结 15.5 思考题 第16章 Hibernate的检索策略 16.1 Hibernate的检索策略简介 16.2 类级别的检索策略 16.2.1 立即检索 16.2.2 延迟检索 16.3 一对多...
一对多(one to many) /多对一( many to one) 7.5.2. 一对一(one to one) 7.5.3. 多对多(many to many) 7.6. 更复杂的关联映射 8. 组件(Component)映射 8.1. 依赖对象(Dependent objects) 8.2. 在...