`

第七章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+中文文档

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

    hibernate_reference中文文档.pdf

    - **5.1.15 组件 (component) 和动态组件 (dynamic-component)**:说明如何映射复杂的数据类型。 - **5.1.16 属性 (Properties)**:提供关于映射集合属性的信息。 - **5.1.17 子类 (subclass)**:介绍如何使用...

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

    Hibernate支持三种持久化模型:实体(Entity)、值对象(Value Object)和组件(Component)。实体是与数据库表对应的Java对象;值对象用于封装业务逻辑中的数据;组件则用来表示一个对象的组成部分。 5. **级联...

    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学习笔记

    - **组件component映射**:将实体类中的复杂属性作为一个组件映射到表中。 - **复合(联合)主键映射**:使用多个字段作为主键。 - **集合(collection)映射**:将实体类中的集合属性映射到数据库表中。 --- #...

    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. 组件作为联合...

    Spring MVC3和Hibernate3整合的源码

    4. **注解驱动的开发**:描述中提到使用了更多的注解来简化配置,这可能指的是使用@Controller、@Service、@Repository和@Component等Spring的组件注解,以及Hibernate的@Entity、@Id、@GeneratedValue等数据持久化...

    Hibernate3的帮助文档

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

    Hibernate面试题

    Hibernate支持多种映射类型,如基本类型(Integer、String等)、集合类型(List、Set等)、组件类型(Component)、一对一(OneToOne)、一对多(OneToMany)、多对一(ManyToOne)和多对多(ManyToMany)。...

    hibernate

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

Global site tag (gtag.js) - Google Analytics