- 浏览: 320288 次
- 性别:
- 来自: 北京
文章分类
- 全部博客 (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
基于连接表,顾名思义就是需要建立中间表来保存两个表的关联关系。与基于外键的一对多关联关系不同的是多了一个保存外键关系的关联表,通过关联表维护两个实体之间的关系。
同时,既然是一对多,一端的Set集合肯定也是少不了的,那么在一端的hbm中,肯定也会有<set>标签出现。
一个Person有多个地址,建立3表,连接表jointable保存2个实体表的主键,jointable维护实体表的关联关系。
Person.java实体类中使用集合Set,保存多端的关系,并在Person.hbm.xml文件中配置关联关系。
创建数据库脚本如下:(参看附件)
-- 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 `address` -- DROP TABLE IF EXISTS `address`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `address` ( `addressId` int(11) NOT NULL AUTO_INCREMENT, `item` varchar(30) NOT NULL, PRIMARY KEY (`addressId`) ) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `address` -- LOCK TABLES `address` WRITE; /*!40000 ALTER TABLE `address` DISABLE KEYS */; INSERT INTO `address` VALUES (1,'address_no2'),(2,'address_no1'); /*!40000 ALTER TABLE `address` ENABLE KEYS */; UNLOCK TABLES; -- -- Table structure for table `person` -- DROP TABLE IF EXISTS `person`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `person` ( `personid` int(11) NOT NULL AUTO_INCREMENT, `age` int(11) NOT NULL, `sex` varchar(10) NOT NULL, PRIMARY KEY (`personid`) ) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=latin1; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `person` -- LOCK TABLES `person` WRITE; /*!40000 ALTER TABLE `person` DISABLE KEYS */; INSERT INTO `person` VALUES (1,10,'M'); /*!40000 ALTER TABLE `person` ENABLE KEYS */; UNLOCK TABLES; -- -- Table structure for table `personadd` -- DROP TABLE IF EXISTS `personadd`; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `personadd` ( `pid` int(11) NOT NULL, `aid` int(11) NOT NULL, KEY `pid` (`pid`), KEY `aid` (`aid`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1; /*!40101 SET character_set_client = @saved_cs_client */; -- -- Dumping data for table `personadd` -- LOCK TABLES `personadd` WRITE; /*!40000 ALTER TABLE `personadd` DISABLE KEYS */; INSERT INTO `personadd` VALUES (1,1),(1,2); /*!40000 ALTER TABLE `personadd` 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 23:04:56
//Address.java
public class Address implements java.io.Serializable { // Fields private Integer addressId; private String item;
//Person.java
public class Person implements java.io.Serializable { // Fields private Integer personid; private Integer age; private String sex; private Set<Address> address = new HashSet<Address>();
//Person.hbm.xml
<!-- 基于连接表的一对多单项关联 --> <!-- set name表示Person表中的Address集合,table是关联表名,cascade级联操作 --> <!-- key column是一端主键在关联表中的外键名 --> <!-- many-to-many column是多端主键在关联表中的外键名 --> <set name="address" table="personadd" cascade="save-update"> <key column="pid"/> <many-to-many column="aid" unique="true" class="com.v512.examples.Address"/> </set>
保存数据
//HibernateTest.java
public static void addInfo() { Address ad1 = new Address(); ad1.setItem("address_no1"); Address ad2 = new Address(); ad2.setItem("address_no2"); Person person = new Person(); person.setAge(10); person.setSex("M"); Set<Address> address = new HashSet<Address>(); address.add(ad1); address.add(ad2); person.setAddress(address); Session session = HibernateUtil.getSessionFactory().openSession(); Transaction tx = session.beginTransaction(); //启用级联操作 session.save(person); // session.save(ad1); // session.save(ad2); tx.commit(); session.close(); HibernateUtil.shutdown(); }
操作信息如下:
22:59:30,843 DEBUG ConnectionManager:444 - opening JDBC connection 22:59:30,843 DEBUG JDBCTransaction:87 - current autocommit status: false 22:59:30,843 DEBUG IncrementGenerator:104 - fetching initial value: select max(personid) from person 22:59:30,843 DEBUG AbstractBatcher:410 - about to open PreparedStatement (open PreparedStatements: 0, globally: 0) 22:59:30,859 DEBUG SQL:111 - select max(personid) from person Hibernate: select max(personid) from person 22:59:30,859 DEBUG IncrementGenerator:119 - first free id: 1 22:59:30,859 DEBUG AbstractBatcher:418 - about to close PreparedStatement (open PreparedStatements: 1, globally: 1) 22:59:30,875 DEBUG AbstractSaveEventListener:135 - generated identifier: 1, using strategy: org.hibernate.id.IncrementGenerator 22:59:30,890 DEBUG IncrementGenerator:104 - fetching initial value: select max(addressId) from address 22:59:30,890 DEBUG AbstractBatcher:410 - about to open PreparedStatement (open PreparedStatements: 0, globally: 0) 22:59:30,890 DEBUG SQL:111 - select max(addressId) from address Hibernate: select max(addressId) from address 22:59:30,890 DEBUG IncrementGenerator:119 - first free id: 1 22:59:30,890 DEBUG AbstractBatcher:418 - about to close PreparedStatement (open PreparedStatements: 1, globally: 1) 22:59:30,937 DEBUG AbstractSaveEventListener:135 - generated identifier: 1, using strategy: org.hibernate.id.IncrementGenerator 22:59:30,937 DEBUG AbstractSaveEventListener:135 - generated identifier: 2, using strategy: org.hibernate.id.IncrementGenerator 22:59:30,937 DEBUG JDBCTransaction:134 - commit 22:59:30,937 DEBUG AbstractFlushingEventListener:134 - processing flush-time cascades 22:59:30,937 DEBUG AbstractFlushingEventListener:177 - dirty checking collections 22:59:30,937 DEBUG Collections:199 - Collection found: [com.v512.examples.Person.address#1], was: [<unreferenced>] (initialized) 22:59:30,984 DEBUG AbstractFlushingEventListener:108 - Flushed: 3 insertions, 0 updates, 0 deletions to 3 objects 22:59:30,984 DEBUG AbstractFlushingEventListener:114 - Flushed: 1 (re)creations, 0 updates, 0 removals to 1 collections 22:59:30,984 DEBUG Printer:106 - listing entities: 22:59:30,984 DEBUG Printer:113 - com.v512.examples.Person{sex=M, address=[com.v512.examples.Address#1, com.v512.examples.Address#2], age=10, personid=1} 22:59:30,984 DEBUG Printer:113 - com.v512.examples.Address{item=address_no1, addressId=2} 22:59:30,984 DEBUG Printer:113 - com.v512.examples.Address{item=address_no2, addressId=1} 22:59:30,984 DEBUG AbstractBatcher:410 - about to open PreparedStatement (open PreparedStatements: 0, globally: 0) 22:59:30,984 DEBUG SQL:111 - insert into hibernate_demo.person (age, sex, personid) values (?, ?, ?) Hibernate: insert into hibernate_demo.person (age, sex, personid) values (?, ?, ?) 22:59:30,984 DEBUG AbstractBatcher:66 - Executing batch size: 1 22:59:30,984 DEBUG AbstractBatcher:418 - about to close PreparedStatement (open PreparedStatements: 1, globally: 1) 22:59:30,984 DEBUG AbstractBatcher:410 - about to open PreparedStatement (open PreparedStatements: 0, globally: 0) 22:59:30,984 DEBUG SQL:111 - insert into hibernate_demo.address (item, addressId) values (?, ?) Hibernate: insert into hibernate_demo.address (item, addressId) values (?, ?) 22:59:31,000 DEBUG AbstractBatcher:248 - reusing prepared statement 22:59:31,000 DEBUG SQL:111 - insert into hibernate_demo.address (item, addressId) values (?, ?) Hibernate: insert into hibernate_demo.address (item, addressId) values (?, ?) 22:59:31,000 DEBUG AbstractBatcher:66 - Executing batch size: 2 22:59:31,000 DEBUG AbstractBatcher:418 - about to close PreparedStatement (open PreparedStatements: 1, globally: 1) 22:59:31,000 DEBUG AbstractCollectionPersister:1112 - Inserting collection: [com.v512.examples.Person.address#1] 22:59:31,000 DEBUG AbstractBatcher:410 - about to open PreparedStatement (open PreparedStatements: 0, globally: 0) 22:59:31,000 DEBUG SQL:111 - insert into personadd (pid, aid) values (?, ?) Hibernate: insert into personadd (pid, aid) values (?, ?) 22:59:31,000 DEBUG AbstractBatcher:248 - reusing prepared statement 22:59:31,000 DEBUG SQL:111 - insert into personadd (pid, aid) values (?, ?) Hibernate: insert into personadd (pid, aid) values (?, ?) 22:59:31,000 DEBUG AbstractCollectionPersister:1194 - done inserting collection: 2 rows inserted 22:59:31,000 DEBUG AbstractBatcher:66 - Executing batch size: 2 22:59:31,000 DEBUG AbstractBatcher:418 - about to close PreparedStatement (open PreparedStatements: 1, globally: 1) 22:59:31,000 DEBUG JDBCTransaction:147 - committed JDBC Connection 22:59:31,000 DEBUG ConnectionManager:427 - aggressively releasing JDBC connection 22:59:31,000 DEBUG ConnectionManager:464 - releasing JDBC connection [ (open PreparedStatements: 0, globally: 0) (open ResultSets: 0, globally: 0)] 22:59:31,000 INFO SessionFactoryImpl:853 - closing 22:59:31,000 INFO DriverManagerConnectionProvider:170 - cleaning up connection pool: jdbc:mysql://localhost:3306/hibernate_demo
其他的代码都是通过myeclipse生成的,不是很麻烦。
刚开始大家可能会有疑惑,是呀,有3个表,为什么不建3个类,3个hbm文件呢?
事实是这样的,数据库之间的表关联关系通过外键实现,现在有了外键关系表。而实体呢?实体之间是通过集合类进行关联的,我们在Person.hbm.xml文件中配置了一对多的关联关系,自然也不同在建立什么关联关系表的映射文件了,对吧。况且,2个文件能做的事情,何必用3个文件呢。
- personAdd.rar (960 Bytes)
- 下载次数: 1
发表评论
-
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 3329The user must supply a JDBC con ... -
Hibernate Annotation小记
2012-08-22 23:43 864老实说,Hibernate Annotation用起来 ... -
Hibernate Annotation 基于外键的一对多双向关联
2012-08-22 23:44 4158纠结了好久呀,因为没有set,关联关系没有保存对,导致插入数据 ... -
【转】Hibernate中的"Repeated column in mapping for entity"异常
2012-08-21 10:31 943文章来源: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 5919写了这么几篇都是单向的关联,干脆后面的都写双向关联吧, ... -
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 1575废话不多说,直接上例子(附件) 数据库脚本: ... -
Hibernate 1+N问题的原理与解决
2012-08-13 20:46 1221什么时候会遇到1+N ... -
【转】Hibernate获取记录总数
2012-08-13 20:41 1116hql获取记录条数 Str ... -
Hibernate基于外键的一对多单向关联
2012-08-09 21:42 1983一对多,一个分类Category下有多个Product,从Ca ... -
Hibernate使用注解
2012-08-04 01:37 1042//类标志 @Entity @Table(name = & ...
相关推荐
Hibernate 多对多单向关联 Hibernate 一对一外键双向关联 Hibernate 一对一主键双向关联 Hibernate 一对一连接表双向关联 Hibernate 一对多外键双向关联 Hibernate 一对多连接表双向关联 Hibernate 多对多双向关联
NULL 博文链接:https://paladin1988.iteye.com/blog/1634669
Hibernate关联关系映射...│ └─ 多对多单向关联 └─双向关联 ├─ 一对一外键双向关联 ├─ 一对一主键双向关联 ├─ 一对一连接表双向关联 ├─ 一对多外键双向关联 ├─ 一对多连接表双向关联 └─ 多对多双向关联
hibernate一对多双向自连接关联映射 15 hibernate多对多关联映射(单向User---->Role) 19 hibernate多对多关联映射(双向User<---->Role) 20 Hibernate的继承关系 21 每棵继承树映射成一张表(hibernate_extends_1) 22 ...
通过在被拥有的实体端(owned entity)增加一个外键列来实现一对多单向关联是很少见的,也是不推荐的,建议通过一个联接表来实现这种关联(下面会讲到)。 @JoinColoumn批注来描述这种单向关联关系 @Entity Public class...
7.3. 使用连接表的单向关联(Unidirectional associations with join tables) 7.3.1. 一对多(one to many) 7.3.2. 多对一(many to one) 7.3.3. 一对一(one to one) 7.3.4. 多对多(many to many) 7.4. ...
这种策略支持双向的一对多关联,但不支持 IDENTIFY 生成器策略,因为ID必须在多个表间共享。一旦使用就不能使用AUTO和IDENTIFY生成器。 每个类层次结构一张表 @Entity @Inheritance(strategy=InheritanceType....
7.3. 使用连接表的单向关联(Unidirectional associations with join tables) 7.3.1. 一对多(one to many) 7.3.2. 多对一(many to one) 7.3.3. 一对一(one to one) 7.3.4. 多对多(many to many) 7.4. ...
7.3. 使用连接表的单向关联(Unidirectional associations with join tables) 7.3.1. 一对多(one to many) 7.3.2. 多对一(many to one) 7.3.3. 一对一(one to one) 7.3.4. 多对多(many to many) 7.4. ...
7.3. 使用连接表的单向关联(Unidirectional associations with join tables) 7.3.1. 一对多(one to many) 7.3.2. 多对一(many to one) 7.3.3. 一对一(one to one) 7.3.4. 多对多(many to many) 7.4. ...
8.3. 使用连接表的单向关联(Unidirectional associations with join tables) 8.3.1. 一对多(one to many) 8.3.2. 多对一(many to one) 8.3.3. 一对一(one to one) 8.3.4. 多对多(many to many) 8.4. ...
7.3. 使用连接表的单向关联(Unidirectional associations with join tables) 7.3.1. 一对多(one to many) 7.3.2. 多对一(many to one) 7.3.3. 一对一(one to one) 7.3.4. 多对多(many to many) 7.4. 双向...
7.3. 使用连接表的单向关联(Unidirectional associations with join tables) 7.3.1. 一对多(one to many) 7.3.2. 多对一(many to one) 7.3.3. 一对一(one to one) 7.3.4. 多对多(many to many) 7.4. 双向...
使用连接表的单向关联(Unidirectional associations with join tables) 7.3.1. 一对多(one to many) 7.3.2. 多对一(many to one) 7.3.3. 一对一(one to one) 7.3.4. 多对多(many to many) 7.4. 双向...
7.3. 使用连接表的单向关联(Unidirectional associations with join tables) 7.3.1. 一对多(one to many) 7.3.2. 多对一(many to one) 7.3.3. 一对一(one to one) 7.3.4. 多对多(many to many) 7.4. ...
8.3. 使用连接表的单向关联(Unidirectional associations with join tables) 8.3.1. 一对多(one to many) 8.3.2. 多对一(many to one) 8.3.3. 一对一(one to one) 8.3.4. 多对多(many to many) 8.4. ...
15.3.4 把多对多关联分解为两个一对多关联 15.4 小结 15.5 思考题 第16章 Hibernate的检索策略 16.1 Hibernate的检索策略简介 16.2 类级别的检索策略 16.2.1 立即检索 16.2.2 延迟检索 16.3 一对多...
8.3. 使用连接表的单向关联(Unidirectional associations with join tables) 8.3.1. 一对多(one to many) 8.3.2. 多对一(many to one) 8.3.3. 一对一(one to one) 8.3.4. 多对多(many to many) 8.4. ...
使用连接表的单向关联(Unidirectional associations with join tables) 8.3.1. 一对多(one to many) 8.3.2. 多对一(many to one) 8.3.3. 一对一(one to one) 8.3.4. 多对多(many to many) 8.4. 双向...