hibernate详解(十四)组件映射

组件映射

  • 组建映射是指在一个实体类中有一个属性为自定义的类,但是自定义的类中并没有oid,在数据库中也没有对应这个类的表。
    表结构:
/* 组件映射 */ CREATE TABLE t_customer( id NUMBER(10) PRIMARY KEY, name VARCHAR2(20), province VARCHAR2(20), city VARCHAR2(20), street VARCHAR2(20) );

Customner类
package com.iotek.basic.component.pojo; import java.io.Serializable; public class Customer implements Serializable {private static final long serialVersionUID = 5660598920023039135L; private Long id; private String name; private Address address; public Customer() {}public Customer(Long id, String name, Address address) { super(); this.id = id; this.name = name; this.address = address; }public Long getId() { return id; }public void setId(Long id) { this.id = id; }public String getName() { return name; }public void setName(String name) { this.name = name; }public Address getAddress() { return address; }public void setAddress(Address address) { this.address = address; }@Override public String toString() { return "Customer [id=" + id + ", name=" + name + ", address=" + address + "]"; }}

组件类 Address.java
package com.iotek.basic.component.pojo; import java.io.Serializable; public class Address implements Serializable {private static final long serialVersionUID = 1921592338080264702L; private String province; private String city; private String street; private Customer customer; public Address() {}public Address(String province, String city, String street, Customer customer) { super(); this.province = province; this.city = city; this.street = street; this.customer = customer; }public String getProvince() { return province; }public void setProvince(String province) { this.province = province; }public String getCity() { return city; }public void setCity(String city) { this.city = city; }public String getStreet() { return street; }public void setStreet(String street) { this.street = street; }public Customer getCustomer() { return customer; }public void setCustomer(Customer customer) { this.customer = customer; }@Override public String toString() { return "Address [province=" + province + ", city=" + city + ", street=" + street + ", customer=" + customer + "]"; }}

映射文件:
t_customer_seq

hibernate.cfg.xml文件:
oracle.jdbc.driver.OracleDriverjdbc:oracle:thin:@localhost:1521:XEguestguesttrueorg.hibernate.dialect.Oracle10gDialect2025000!1001502false

【hibernate详解(十四)组件映射】看代码就可以理解了。
总结:

hibernate详解(十四)组件映射
文章图片
1.png

    推荐阅读