组件(Component)映射
Component(组件)是一个被包含的对象,它作为值类型被持久化,而非一个被引用的实体。“component(组件)”这一术语指的是面向对象的合成概念(而并不是系统构架层次上的组件的概念)。在hibernate中可借助Component节点的定义,完成对复杂数据表的细分。在hibernate语义中,将某个实例对象中的一个逻辑组成称为Component(组件)。Component(组件)与实体对象的根本差别就在于Componet(组件)没有标识符(identity),它作为一个逻辑组成完全从属于实体对象。
还是以我第一个hibernate实例为例来说明组件映射:
User实体类如下:
- package eg;
- public class User {
- private int id;
- private Name name;//Name是另一个类
- private Date birthday;
- //setter和getter方法省略
- }
package eg; public class User { private int id; private Name name;//Name是另一个类 private Date birthday; //setter和getter方法省略 }
Name类如下:
- package eg;
- public class Name {
- private String firstName;
- private String lastName;
- //省略setter和getter方法
- }
package eg; public class Name { private String firstName; private String lastName; //省略setter和getter方法 }
User.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">
- <hibernate-mapping>
- <class name="eg.User" >
- <id name="id" >
- <generator class="native" />
- </id>
- <component name="name">
- <property name="firstName" column="first_name" />
- <property name="lastName" column="last_name" />
- </component>
- <property name="birthday" />
- </class>
- </hibernate-mapping
<?xml version="1.0"?> <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> <hibernate-mapping> <class name="eg.User" > <id name="id" > <generator class="native" /> </id> <component name="name"> <property name="firstName" column="first_name" /> <property name="lastName" column="last_name" /> </component> <property name="birthday" /> </class> </hibernate-mapping
测试类如下:
- public class TestUser {
- @Test
- public void test1(){
- Configuration config = new Configuration();
- config.configure();
- SessionFactory factory = config.buildSessionFactory();
- Session session = factory.openSession();
- Transaction tran = session.beginTransaction();
- User user = new User();
- Name name = new Name();
- name.setFirstName("long");
- name.setLastName("xiao");
- user.setName(name);
- user.setBirthday(new Date());
- session.save(user);
- tran.commit();
- session.close();
- }
- }
public class TestUser { @Test public void test1(){ Configuration config = new Configuration(); config.configure(); SessionFactory factory = config.buildSessionFactory(); Session session = factory.openSession(); Transaction tran = session.beginTransaction(); User user = new User(); Name name = new Name(); name.setFirstName("long"); name.setLastName("xiao"); user.setName(name); user.setBirthday(new Date()); session.save(user); tran.commit(); session.close(); } }
测试程序运行后,数据库中生成了user表,创建语句如下:
- DROP TABLE IF EXISTS `test`.`user`;
- CREATE TABLE `test`.`user` (
- `id` int(11) NOT NULL AUTO_INCREMENT,
- `first_name` varchar(255) DEFAULT NULL,
- `last_name` varchar(255) DEFAULT NULL,
- `birthday` datetime DEFAULT NULL,
- PRIMARY KEY (`id`)
- ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
DROP TABLE IF EXISTS `test`.`user`; CREATE TABLE `test`.`user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `first_name` varchar(255) DEFAULT NULL, `last_name` varchar(255) DEFAULT NULL, `birthday` datetime DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
数据库中记录如下所示:
+----+------------+-----------+----------+
| id | first_name | last_name | birthday |
+----+------------+-----------+----------+
| 1 | long | xiao | NULL |
+----+------------+-----------+----------+
1 row in set (0.00 sec)
相关推荐
组件component映射(hibernate_component) 27 复合(联合)主键映射(hibernate_composite) 27 集合(collection)映像 (hibernate_collection) 28 Hibernate 对数据库的并发支持 30 悲观锁(hibernate_pessimistic...
022 component(组件)关联映射 023 复合主键 关联映射 024 其它 关联映射 025 hibernate 悲观锁、乐观锁 026 hibernate 操作树形结构 027 hibernate 查询语言(HQL) 028 hibernate 缓存(性能优化策略) 029 hibernate ...
8. 组件(Component)映射; 9. 继承映射(Inheritance Mappings); 10. 与对象共事; 11. 事务和并发; 12. 拦截器与事件(Interceptors and events); 13. 批量处理(Batch processing; 14. HQL: Hibernate查询语言; 15. ...
组件(Component)是一个被包含的对象,在持久化的过程中,它被当作值类型,而并非一个实体的引用。在这篇文档中,组件这一术语指的是面向对象的合成概念(而并不是系统构架层次上的组件的概念)。举个例子, 你对人...
8. 组件(Component)映射 8.1. 依赖对象(Dependent objects) 8.2. 在集合中出现的依赖对象 (Collections of dependent objects) 8.3. 组件作为Map的索引(Components as Map indices ) 8.4. 组件作为联合...
Hibernate 的注解学习 ExportDBAnnotation HibernateAnnotationUtils 【一对一】关系映射【一对多】关系映射 【多对多】关系映射 【composite复合主键】关系映射【component组件】关系映射
第 9 章 组件(Component)映射 第 10 章 继承映射(Inheritance Mapping) 第 11 章 与对象共事 第 12 章 Read-only entities 第 13 章 事务和并发 第 14 章 拦截器与事件(Interceptors and ...
8. 组件(Component)映射 8.1. 依赖对象(Dependent objects) 8.2. 在集合中出现的依赖对象 (Collections of dependent objects) 8.3. 组件作为Map的索引(Components as Map indices ) 8.4. 组件作为联合...
8. 组件(Component)映射 8.1. 依赖对象(Dependent objects) 8.2. 在集合中出现的依赖对象 (Collections of dependent objects) 8.3. 组件作为Map的索引(Components as Map indices ) 8.4. 组件作为联合...
组件(Component)映射 7.1. 依赖对象(Dependent objects) 7.2. 在集合中出现的依赖对象 7.3. 组件作为Map的索引(Components as Map indices ) 7.4. 组件作为联合标识符(Components as composite ...
8. 组件(Component)映射 8.1. 依赖对象(Dependent objects) 8.2. 在集合中出现的依赖对象 (Collections of dependent objects) 8.3. 组件作为Map的索引(Components as Map indices ) 8.4. 组件作为联合...
8. 组件(Component)映射 8.1. 依赖对象(Dependent objects) 8.2. 在集合中出现的依赖对象 (Collections of dependent objects) 8.3. 组件作为Map的索引(Components as Map indices ) 8.4. 组件作为联合标识符...
8. 组件(Component)映射 8.1. 依赖对象(Dependent objects) 8.2. 在集合中出现的依赖对象 8.3. 组件作为Map的索引(Components as Map indices ) 8.4. 组件作为联合标识符(Components as composite identifiers...
8. 组件(Component)映射 8.1. 依赖对象(Dependent objects) 8.2. 在集合中出现的依赖对象 (Collections of dependent objects) 8.3. 组件作为Map的索引(Components as Map indices ) 8.4. 组件作为联合...
9. 组件(Component)映射 9.1. 依赖对象(Dependent objects) 9.2. 在集合中出现的依赖对象 9.3. 组件作为Map的索引(Components as Map indices ) 9.4. 组件作为联合标识符(Components as composite ...
8. 组件(Component)映射 8.1. 依赖对象(Dependent objects) 8.2. 在集合中出现的依赖对象 (Collections of dependent objects) 8.3. 组件作为Map的索引(Components as Map indices ) 8.4. 组件作为联合...
9. 组件(Component)映射 9.1. 依赖对象(Dependent objects) 9.2. 在集合中出现的依赖对象 9.3. 组件作为Map的索引(Components as Map indices ) 9.4. 组件作为联合标识符(Components as composite ...
组件(Component)映射 7.1. 依赖对象(Dependent objects) 7.2. 在集合中出现的依赖对象 7.3. 组件作为Map的索引(Components as Map indices ) 7.4. 组件作为联合标识符(Components as composite ...
// Persistent component using defaults Address homeAddress; @Embedded @AttributeOverrides( { @AttributeOverride(name="iso2", column = @Column(name="bornIso2") ), @AttributeOverride(name="name", ...
9. 组件(Component)映射 9.1. 依赖对象(Dependent objects) 9.2. 在集合中出现的依赖对象 9.3. 组件作为Map的索引(Components as Map indices ) 9.4. 组件作为联合标识符(Components as composite ...