`

第七章Hibernate 组件(component)映射

阅读更多

第七章 Hibernate组件(component)映射

  • 组件(Component)映射的单向关联
  • 组件映射的双向关联
  • 组件集合映射

   *  组件(Component)

  1.   除了粗粒度的对象模型设计(一个表映射成一个持久化类)之外,还可以采用细粒度的 对象模型,把一个表映射成两个或者多个类。
  2.   被细化出来的类,可以成为组件(Component)

   User类:

public class User implements java.io.Serializable {
	private Integer id;
	private String username;
	private String password;
	private Profile profile;
	public User(){}
	
	public Profile getProfile() {
		return profile;
	}
	public void setProfile(Profile profile) {
		this.profile = profile;
	}
	public Integer getId() {
		return this.id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getUsername() {
		return this.username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return this.password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
}

 Profile类:

 

public class Profile {
	private String email;
	private String address;
	private String postcode;
	private String mobile;
	private String phone;
	//加上该属性,修改配置文件,即双向关联
	private User user;
	
	public Profile(){}
	
	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 getPostcode() {
		return postcode;
	}
	public void setPostcode(String postcode) {
		this.postcode = postcode;
	}
	public String getMobile() {
		return mobile;
	}
	public void setMobile(String mobile) {
		this.mobile = mobile;
	}
	public String getPhone() {
		return phone;
	}
	public void setPhone(String phone) {
		this.phone = phone;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
}

 Hibernate配置文件:

<hibernate-mapping>
    <class name="com.crazy.User" table="USERS2" schema="SCOTT">
        <id name="id" type="java.lang.Integer">
            <column name="ID" precision="8" scale="0" />
            <generator class="increment" />
        </id>
        <property name="username" type="java.lang.String">
            <column name="USERNAME" length="20" />
        </property>
        <property name="password" type="java.lang.String">
            <column name="PASSWORD" length="20" />
        </property>
        
        <component name="profile" class="com.crazy.Profile">
			<parent name="user"/>
	        <property name="email" type="java.lang.String">
	        	<column name="EMAIL" length="200" />
	        </property>
	        <property name="address" type="java.lang.String">
	        	<column name="ADDRESS" length="200" />
	        </property>
	        <property name="postcode" type="java.lang.String">
	        	<column name="POSTCODE" length="10" />
	        </property>
	        <property name="mobile" type="java.lang.String">
	        	<column name="MOBILE" length="11" />
	        </property>
	        <property name="phone" type="java.lang.String">
	        	<column name="PHONE" length="20" />
	        </property>
        </component>
    </class>
</hibernate-mapping>

   双向关联配置:

			<parent name="user"/>
  • 组件集合映射

 使用组件集合映射,可以让组件对象的集合依附于一个持久化对象。

Product 类:

public class Product implements java.io.Serializable {
	private Integer id;
	private String name;
	private Double price;
	private String description;
	private Date createTime;
	private Set images = new HashSet();
	public Product() {
	}

	public Product(String name, Double price, String description,
			Date createTime, Set images) {
		this.name = name;
		this.price = price;
		this.description = description;
		this.createTime = createTime;
		this.images = images;
	}

	public Integer getId() {
		return this.id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return this.name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public Double getPrice() {
		return this.price;
	}
	public void setPrice(Double price) {
		this.price = price;
	}
	public String getDescription() {
		return this.description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public Date getCreateTime() {
		return this.createTime;
	}
	public void setCreateTime(Date createTime) {
		this.createTime = createTime;
	}
	public Set getImages() {
		return this.images;
	}
	public void setImages(Set images) {
		this.images = images;
	}
}

  Image 类:

 

public class Image {
	private String fileName;
	private String path;
	private  Integer imageSize;
	public Image(){}
	public Image(String fileName,String path, Integer imageSize){
		this.fileName = fileName;
		this.path = path;
		this.imageSize = imageSize;
	}
	public String getFileName() {
		return fileName;
	}
	public void setFileName(String fileName) {
		this.fileName = fileName;
	}
	public String getPath() {
		return path;
	}
	public void setPath(String path) {
		this.path = path;
	}
	public Integer getImageSize() {
		return imageSize;
	}
	public void setImageSize(Integer imageSize) {
		this.imageSize = imageSize;
	}
}

  映射配置文件:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!-- 
    Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
    <class name="com.crazy.Product" table="PRODUCT" schema="SCOTT">
        <id name="id" type="java.lang.Integer">
            <column name="ID" precision="8" scale="0" />
            <generator class="increment" />
        </id>
        <property name="name" type="java.lang.String">
            <column name="NAME" length="200" />
        </property>
        <property name="price" type="java.lang.Double">
            <column name="PRICE" precision="6" />
        </property>
        <property name="description" type="java.lang.String">
            <column name="DESCRIPTION" length="2000" />
        </property>
        <property name="createTime" type="java.util.Date">
            <column name="CREATE_TIME" length="7" />
        </property>
        <set name="images" table="image">
                <key column="PRODUCT_ID" foreign-key="id" />
                <composite-element class="com.crazy.Image">
                	<property name="fileName" column="filename"></property>
                	<property name="path" column="path"></property>
                	<property name="imageSize" column="image_Size"></property>
                </composite-element>
        </set>
    </class>
</hibernate-mapping>

  与Set映射比较相似,在第六章仅仅映射了一个字段,这里映射了一个组件。

<set name="images" table="image">
                <key column="PRODUCT_ID" foreign-key="id" />
                <composite-element class="com.crazy.Image">
                	<property name="fileName" column="filename"></property>
                	<property name="path" column="path"></property>
                	<property name="imageSize" column="image_Size"></property>
                </composite-element>
</set>

  测试类:

public class HibernateTest {
	public static void main(String[] args) {
		HibernateTest test = new HibernateTest();
		Product p = new Product();
		p.setName("洗面奶");
		p.setCreateTime(new Date());
		p.setDescription("洗脸很干净!");
		p.setPrice(999.99);
		Set<Image> images = new HashSet();
		images.add(new Image("filename1","filePath1",1000));
		images.add(new Image("filename2","filePath2",2000));
		images.add(new Image("filename3","filePath3",3000));
		p.setImages(images);
		test.addProduct(p);
	}

	public void addProduct(Product p){
		Session session = new Configuration().configure().buildSessionFactory().openSession();
		session.beginTransaction();
		session.save(p);
		session.getTransaction().commit();
	}
	
	public void printProduct(Product p){
		System.out.println("商品编号:" + p.getId());
		System.out.println("商品价格:" + p.getPrice());
		System.out.println("商品名称:" + p.getName());
		
		Set<Image> images = p.getImages();
		for(Image image:images){
			System.out.println("image名称:" + image.getFileName());
			System.out.println("image路径:" + image.getPath());
			System.out.println("image大小:" + image.getImageSize());
		}
	}
}
 

 

 

分享到:
评论

相关推荐

    Hibernate 3.6.0.Final Reference PDF 手册

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

    hibernate学习笔记

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

    Hibernate+中文文档

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

    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 ...

    hibernate3.04中文文档.chm

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

    hibernate 框架详解

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

    Hibernate3+中文参考文档

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

    Hibernate参考文档

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

    JBoss Seam 工作原理、seam和hibernate的范例、RESTFul的seam、seam-gen起步、seam组件、配置组件、jsf,jboss、标签、PDF、注解等等

    Seam - 语境相关的组件[满江红20071230]............................................................................................................................ 1 Java EE 框架...........................

    hibernate_reference中文文档.pdf

    1.1. 第一部分 - 第一个 Hibernate 应用程序 ................................. 1 1.1.1. 设置 ............................................................ 1 1.1.2. 第一个 class .............................

Global site tag (gtag.js) - Google Analytics