`
燮羽天翔
  • 浏览: 110443 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

hibernate_component_mapping

阅读更多

component映射

在hibernate中,component是某个实体的逻辑组成部分,它与实体的根本区别是没有oid,
component可以成为是值对象(DDD)

采用component映射的好处:它实现了对象模型的细粒度划分,层次会更分明,复用率会更高

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="com.bjsxt.hibernate.User" table="t_user">
        <id name="id">
            <generator class="native"/>
        </id>
        <property name="name"/>
        <component name="contact">
            <property name="email"/>
            <property name="address"/>
            <property name="zipCode"/>
            <property name="contactTel"/>
        </component>
    </class>
</hibernate-mapping>

 
User.java

package com.bjsxt.hibernate;

public class User {
    
    private int id;
    
    private String name;
    
    private Contact contact; 
    
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Contact getContact() {
        return contact;
    }

    public void setContact(Contact contact) {
        this.contact = contact;
    }
    
}

Contact.java
package com.bjsxt.hibernate;

public class Contact {
    
    private String email;
    
    private String address;
    
    private String zipCode;
    
    private String contactTel;

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getZipCode() {
        return zipCode;
    }

    public void setZipCode(String zipCode) {
        this.zipCode = zipCode;
    }

    public String getContactTel() {
        return contactTel;
    }

    public void setContactTel(String contactTel) {
        this.contactTel = contactTel;
    }
}

 
ComponentMappingTest.java

package com.bjsxt.hibernate;

import org.hibernate.Session;

import junit.framework.TestCase;

public class ComponentMappingTest extends TestCase {

    public void testSave1() {
        Session session = null;
        try {
            session = HibernateUtils.getSession();
            session.beginTransaction();
            
            User user = new User();
            user.setName("张三");
            
            Contact contact = new Contact();
            contact.setAddress("xxxxx");
            contact.setEmail("xxx@rrr.com");
            contact.setZipCode("1111111");
            contact.setContactTel("9999999999");
            
            user.setContact(contact);
            
            session.save(user);
            session.getTransaction().commit();
        }catch(Exception e) {
            e.printStackTrace();
            session.getTransaction().rollback();
        }finally {
            HibernateUtils.closeSession(session);
        }
    }        
}

 

分享到:
评论

相关推荐

    Hibernate3.1_学习源码

    03 03Hibernate_Component : 实体细粒度的划分,数据库中的一张表在程序中划分为几个部分的实体,配置文件和增、删、改、查的使用。 04 04Hibernate_Composite : 复合主键的使用,在开发中很少用到,一般良好的设计...

    04-hibernate-Component_and_Inheritance_Mapping

    Component and Inheritance Mapping Originals of Slides and Source Code for Examples: http://courses.coreservlets.com/Course-Materials/hibernate.html

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

    5.1.2. hibernate-mapping 5.1.3. class 5.1.4. id 5.1.5. composite-id 5.1.6. 鉴别器(discriminator) 5.1.7. 版本(version)(可选) 5.1.8. timestamp (可选) 5.1.9. property 5.1.10. 多对一(many-to-...

    hibernate_reference中文文档.pdf

    前言 ............................................................................ xi 1. 教程 ................................................5.1.2. Hibernate-mapping ........................................

    Hibernate Recipes A Problem-Solution Approach

    How to do various mappings, including one-to-one mapping, many-to-one mapping, collection mapping, component mapping, and inheritance mapping How to use Hibernate Query Language (HQL) How to perform ...

    Hibernate中文API大全

    (A mapping like this allows you to map extra columns of a many-to-many association table to the composite element class.) 接下来的的例子是从Order到Item的一个多对多的关联关系, 关联属性是 purchaseDate, ...

    Hibernate+中文文档

    5.1.2. hibernate-mapping 5.1.3. class 5.1.4. id 5.1.5. composite-id 5.1.6. 鉴别器(discriminator) 5.1.7. 版本(version)(可选) 5.1.8. timestamp (可选) 5.1.9. property 5.1.10. 多对一(many-to-...

    hibernate 教程

    hibernate-mapping 5.1.3. class 5.1.4. id 5.1.4.1. generator 5.1.4.2. 高/低位算法(Hi/Lo Algorithm) 5.1.4.3. UUID算法(UUID Algorithm ) 5.1.4.4. 标识字段和序列(Identity columns ...

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

    5.1.2. hibernate-mapping 5.1.3. class 5.1.4. id 5.1.5. composite-id 5.1.6. 鉴别器(discriminator) 5.1.7. 版本(version)(可选) 5.1.8. timestamp (可选) 5.1.9. property 5.1.10. 多对一(many-to-...

    HibernateAPI中文版.chm

    5.1.2. hibernate-mapping 5.1.3. class 5.1.4. id 5.1.5. composite-id 5.1.6. 鉴别器(discriminator) 5.1.7. 版本(version)(可选) 5.1.8. timestamp (可选) 5.1.9. property 5.1.10. 多对一(many-to-...

    Hibernate Reference Documentation3.1

    8. Component Mapping 8.1. Dependent objects 8.2. Collections of dependent objects 8.3. Components as Map indices 8.4. Components as composite identifiers 8.5. Dynamic components 9. Inheritance Mapping...

    Hibernate 3.6.0.Final Reference PDF 手册

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

    Hibernate框架参考文档

    5. 对象/关系数据库映射基础(Basic O/R Mapping); 6. 集合类(Collections)映射; 7. 关联关系映射; 8. 组件(Component)映射; 9. 继承映射(Inheritance Mappings); 10. 与对象共事; 11. 事务和并发; 12. 拦截器与...

    Hibernate中文详细学习文档

    5.1.2. hibernate-mapping 5.1.3. class 5.1.4. id 5.1.5. composite-id 5.1.6. 鉴别器(discriminator) 5.1.7. 版本(version)(可选) 5.1.8. timestamp (可选) 5.1.9. property 5.1.10. 多对一(many-to-...

    Hibernate 中文 html 帮助文档

    5.1.2. hibernate-mapping 5.1.3. class 5.1.4. id 5.1.4.1. Generator 5.1.4.2. 高/低位算法(Hi/Lo Algorithm) 5.1.4.3. UUID算法(UUID Algorithm ) 5.1.4.4. 标识字段和序列(Identity columns and Sequences...

    最全Hibernate 参考文档

    5.1.2. hibernate-mapping 5.1.3. class 5.1.4. id 5.1.4.1. Generator 5.1.4.2. 高/低位算法(Hi/Lo Algorithm) 5.1.4.3. UUID算法(UUID Algorithm ) 5.1.4.4. 标识字段和序列(Identity columns and Sequences...

    SpringMVC+Hibernate全注解整合

    &lt;context:component-scan base-package="com.org.*" /&gt; &lt;property name="suffix" value=".jsp" /&gt; &lt;!-- 配置jdbc --&gt; &lt;value&gt;classpath:properties/jdbc.properties &lt;!-- 配置數...

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

    hibernate-mapping 5.1.3. class 5.1.4. id 5.1.4.1. Generator 5.1.4.2. 高/低位算法(Hi/Lo Algorithm) 5.1.4.3. UUID算法(UUID Algorithm ) 5.1.4.4. 标识字段和序列(Identity columns and Sequences...

    Hibernate教程

    6.1.2. hibernate-mapping 6.1.3. class 6.1.4. id 6.1.4.1. Generator 6.1.4.2. 高/低位算法(Hi/Lo Algorithm) 6.1.4.3. UUID算法(UUID Algorithm ) 6.1.4.4. 标识字段和序列(Identity columns and ...

    hibernate

    hibernate-mapping 5.1.3. class 5.1.4. id 5.1.4.1. generator 5.1.4.2. 高/低位算法(Hi/Lo Algorithm) 5.1.4.3. UUID算法(UUID Algorithm ) 5.1.4.4. 标识字段和序列(Identity columns ...

Global site tag (gtag.js) - Google Analytics