`

Hibernate组件(Component)映射

 
阅读更多

组件(Component)映射

Component(组件)是一个被包含的对象,它作为值类型被持久化,而非一个被引用的实体。“component(组件)”这一术语指的是面向对象的合成概念(而并不是系统构架层次上的组件的概念)。在hibernate中可借助Component节点的定义,完成对复杂数据表的细分。在hibernate语义中,将某个实例对象中的一个逻辑组成称为Component(组件)。Component(组件)与实体对象的根本差别就在于Componet(组件)没有标识符(identity),它作为一个逻辑组成完全从属于实体对象。

还是以我第一个hibernate实例为例来说明组件映射:

User实体类如下:

Java代码 复制代码 收藏代码
  1. package eg;   
  2. public class User {   
  3.        
  4.     private int id;   
  5.     private Name name;//Name是另一个类   
  6.     private Date birthday;   
  7.        //setter和getter方法省略   
  8. }  
package eg;
public class User {
	
	private int id;
	private Name name;//Name是另一个类
	private Date birthday;
       //setter和getter方法省略
}

 Name类如下:

Java代码 复制代码 收藏代码
  1. package eg;   
  2. public class Name {   
  3.   
  4.     private String firstName;   
  5.     private String lastName;   
  6.   
  7.   //省略setter和getter方法   
  8. }  
package eg;
public class Name {

    private String firstName;
    private String lastName;

  //省略setter和getter方法
}

 User.hbm.xml映射文件如下:

Xml代码 复制代码 收藏代码
  1. <?xml version="1.0"?>  
  2. <!DOCTYPE hibernate-mapping PUBLIC   
  3.     "-//Hibernate/Hibernate Mapping DTD 3.0//EN"   
  4.     "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">  
  5. <hibernate-mapping>  
  6.     <class name="eg.User" >  
  7.         <id name="id" >  
  8.             <generator class="native" />  
  9.         </id>  
  10.         <component name="name">  
  11.             <property name="firstName" column="first_name" />  
  12.             <property name="lastName" column="last_name" />  
  13.         </component>  
  14.         <property name="birthday" />  
  15.     </class>  
  16. </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

 测试类如下:

Java代码 复制代码 收藏代码
  1. public class TestUser {   
  2.   
  3.     @Test  
  4.     public void test1(){   
  5.         Configuration config = new Configuration();   
  6.         config.configure();   
  7.         SessionFactory factory = config.buildSessionFactory();   
  8.        
  9.         Session session = factory.openSession();   
  10.         Transaction tran = session.beginTransaction();   
  11.         User user = new User();   
  12.         Name name = new Name();   
  13.         name.setFirstName("long");   
  14.         name.setLastName("xiao");   
  15.         user.setName(name);   
  16.         user.setBirthday(new Date());   
  17.         session.save(user);   
  18.         tran.commit();   
  19.         session.close();   
  20.     }   
  21. }  
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表,创建语句如下:

Sql代码 复制代码 收藏代码
  1. DROP TABLE IF EXISTS `test`.`user`;   
  2. CREATE TABLE  `test`.`user` (   
  3.   `id` int(11) NOT NULL AUTO_INCREMENT,   
  4.   `first_name` varchar(255) DEFAULT NULL,   
  5.   `last_name` varchar(255) DEFAULT NULL,   
  6.   `birthday` datetime DEFAULT NULL,   
  7.   PRIMARY KEY (`id`)   
  8. ) 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)

分享到:
评论
1 楼 kxhu 2014-04-21  
请教component配置对应的批注是?谢谢!

相关推荐

    hibernate学习笔记

    组件component映射(hibernate_component) 27 复合(联合)主键映射(hibernate_composite) 27 集合(collection)映像 (hibernate_collection) 28 Hibernate 对数据库的并发支持 30 悲观锁(hibernate_pessimistic...

    Hibernate学习笔记

    022 component(组件)关联映射 023 复合主键 关联映射 024 其它 关联映射 025 hibernate 悲观锁、乐观锁 026 hibernate 操作树形结构 027 hibernate 查询语言(HQL) 028 hibernate 缓存(性能优化策略) 029 hibernate ...

    Hibernate框架参考文档

    8. 组件(Component)映射; 9. 继承映射(Inheritance Mappings); 10. 与对象共事; 11. 事务和并发; 12. 拦截器与事件(Interceptors and events); 13. 批量处理(Batch processing; 14. HQL: Hibernate查询语言; 15. ...

    Hibernate中文API大全

    组件(Component)是一个被包含的对象,在持久化的过程中,它被当作值类型,而并非一个实体的引用。在这篇文档中,组件这一术语指的是面向对象的合成概念(而并不是系统构架层次上的组件的概念)。举个例子, 你对人...

    Hibernate+中文文档

    8. 组件(Component)映射 8.1. 依赖对象(Dependent objects) 8.2. 在集合中出现的依赖对象 (Collections of dependent objects) 8.3. 组件作为Map的索引(Components as Map indices ) 8.4. 组件作为联合...

    hibernate注解案例

    Hibernate 的注解学习 ExportDBAnnotation HibernateAnnotationUtils 【一对一】关系映射【一对多】关系映射 【多对多】关系映射 【composite复合主键】关系映射【component组件】关系映射

    Hibernate 3.6.0.Final Reference PDF 手册

    第 9 章 组件(Component)映射 第 10 章 继承映射(Inheritance Mapping) 第 11 章 与对象共事 第 12 章 Read-only entities 第 13 章 事务和并发 第 14 章 拦截器与事件(Interceptors and ...

    hibernate3.2中文文档(chm格式)

    8. 组件(Component)映射 8.1. 依赖对象(Dependent objects) 8.2. 在集合中出现的依赖对象 (Collections of dependent objects) 8.3. 组件作为Map的索引(Components as Map indices ) 8.4. 组件作为联合...

    HibernateAPI中文版.chm

    8. 组件(Component)映射 8.1. 依赖对象(Dependent objects) 8.2. 在集合中出现的依赖对象 (Collections of dependent objects) 8.3. 组件作为Map的索引(Components as Map indices ) 8.4. 组件作为联合...

    hibernate 教程

    组件(Component)映射 7.1. 依赖对象(Dependent objects) 7.2. 在集合中出现的依赖对象 7.3. 组件作为Map的索引(Components as Map indices ) 7.4. 组件作为联合标识符(Components as composite ...

    Hibernate中文详细学习文档

    8. 组件(Component)映射 8.1. 依赖对象(Dependent objects) 8.2. 在集合中出现的依赖对象 (Collections of dependent objects) 8.3. 组件作为Map的索引(Components as Map indices ) 8.4. 组件作为联合...

    Hibernate 中文 html 帮助文档

    8. 组件(Component)映射 8.1. 依赖对象(Dependent objects) 8.2. 在集合中出现的依赖对象 (Collections of dependent objects) 8.3. 组件作为Map的索引(Components as Map indices ) 8.4. 组件作为联合标识符...

    最全Hibernate 参考文档

    8. 组件(Component)映射 8.1. 依赖对象(Dependent objects) 8.2. 在集合中出现的依赖对象 8.3. 组件作为Map的索引(Components as Map indices ) 8.4. 组件作为联合标识符(Components as composite identifiers...

    hibernate 体系结构与配置 参考文档(html)

    8. 组件(Component)映射 8.1. 依赖对象(Dependent objects) 8.2. 在集合中出现的依赖对象 (Collections of dependent objects) 8.3. 组件作为Map的索引(Components as Map indices ) 8.4. 组件作为联合...

    Hibernate教程

    9. 组件(Component)映射 9.1. 依赖对象(Dependent objects) 9.2. 在集合中出现的依赖对象 9.3. 组件作为Map的索引(Components as Map indices ) 9.4. 组件作为联合标识符(Components as composite ...

    Hibernate_3.2.0_符合Java习惯的关系数据库持久化

    8. 组件(Component)映射 8.1. 依赖对象(Dependent objects) 8.2. 在集合中出现的依赖对象 (Collections of dependent objects) 8.3. 组件作为Map的索引(Components as Map indices ) 8.4. 组件作为联合...

    Hibernate3的帮助文档

    9. 组件(Component)映射 9.1. 依赖对象(Dependent objects) 9.2. 在集合中出现的依赖对象 9.3. 组件作为Map的索引(Components as Map indices ) 9.4. 组件作为联合标识符(Components as composite ...

    hibernate

    组件(Component)映射 7.1. 依赖对象(Dependent objects) 7.2. 在集合中出现的依赖对象 7.3. 组件作为Map的索引(Components as Map indices ) 7.4. 组件作为联合标识符(Components as composite ...

    Hibernate注释大全收藏

    // Persistent component using defaults Address homeAddress; @Embedded @AttributeOverrides( { @AttributeOverride(name="iso2", column = @Column(name="bornIso2") ), @AttributeOverride(name="name", ...

    hibernate3.04中文文档.chm

    9. 组件(Component)映射 9.1. 依赖对象(Dependent objects) 9.2. 在集合中出现的依赖对象 9.3. 组件作为Map的索引(Components as Map indices ) 9.4. 组件作为联合标识符(Components as composite ...

Global site tag (gtag.js) - Google Analytics