hibernate 六 组合映射



我们会把一个类中的其他有关的信息封装到一个实体bean中,通过组合的关系进行映射

如下 clients表

id

name

linkname

phone

province

city

street

zipcode



Clients 与 Address是组合关系


Clients        ------------------------>                        Address(把以下几个字段抽象为一个bean)

id                                                                        province                                                     

name                                                                 city

linkname                                                            street

phone                                                                 zipcode

address   Address                                           clients Client


Address.java


package com.xiu.hibernate.other;
import java.io.Serializable;
public class Address implements Serializable{
private static final long serialVersionUID = 1L;

private String province;

private String city;

private  String street;

private String zipcode;

private Clients clients;

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 String getZipcode() {
return zipcode;
}

public void setZipcode(String zipcode) {
this.zipcode = zipcode;
}

public Clients getClients() {
return clients;
}


public void setClients(Clients clients) {
this.clients = clients;
}

}



Clients.java

package com.xiu.hibernate.other;
import java.io.Serializable;
public class Clients implements Serializable{
private Integer id;
private String name;
private String linkname;
private String phone;
private Address address;
public Integer getId() {
return id;
}

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

public String getName() {
return name;
}


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

public String getLinkname() {
return linkname;
}

public void setLinkname(String linkname) {
this.linkname = linkname;
}

public String getPhone() {
return phone;
}

public void setPhone(String phone) {
this.phone = phone;
}

public Address getAddress() {
return address;
}

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

}


Clients.hbm.xml


<?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" >
<hibernate-mapping package="com.xiu.hibernate.other">
<class name="Clients" table="clients">
<id name="id" column="ID" type="integer">
<generator class="identity" />
</id>
<property name="name" column="name" type="string" />
<property name="linkname" column="linkname" type="string" />
<property name="phone" column="phone" type="string" />
<component name="address"
class="com.xiu.hibernate.other.Address">
<parent name="clients" /><!--Address引用了clients  -->


<!-- 以下是进行组件类属性与表字段之间的持久化映射 -->
<property name="province" column="province" type="string" />
<property name="city" column="city" type="string" />
<property name="street" column="street" type="string" />
<property name="zipcode" column="zipcode" type="string" />
</component>
</class>

</hibernate-mapping>


测试


package com.xiu.hibernate.other;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
public class TestCompents {
public static void main(String[] args) {
SessionFactory sessionFactory = HibernateSessionFactory
.getSessionFactory();
Session session = sessionFactory.openSession();
Transaction tx = session.beginTransaction();
tx.begin();


Address address = new Address();
address.setCity("武汉");
address.setProvince("湖北");
address.setStreet("武昌区");
address.setZipcode("430205");

Clients clients = new Clients();
clients.setAddress(address);
clients.setLinkname("朱先生");
clients.setName("hello");
clients.setPhone("119");

session.save(clients);
tx.commit();


}


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值