第七章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());
		}
	}
}
 

相关推荐