Hibernate Gossip: 複合主鍵(二)
来源:互联网 发布:德云色淘宝店网址 编辑:程序博客网 时间:2024/04/28 21:57
可以將主鍵的資訊獨立為一個類別,例如:
現在User類別的主鍵資訊被分離出來了,例如:
在映射文件方面,需要指定主鍵類的資訊,例如:
在查詢資料時,必須指定主鍵資訊,例如:
<script type="text/javascript"><!--google_ad_client = "pub-9750319131714390";google_ad_width = 160;google_ad_height = 600;google_ad_format = "160x600_as";google_ad_type = "text_image";google_ad_channel = "";//--> </script> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"> </script> <iframe name="google_ads_frame" marginwidth="0" marginheight="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-9750319131714390&dt=1188805912109&lmt=1175337899&format=160x600_as&output=html&correlator=1188805912109&url=http%3A%2F%2Fcaterpillar.onlyfun.net%2FGossip%2FHibernateGossip%2FCompactPK2.html&ad_type=text_image&ref=http%3A%2F%2Fcaterpillar.onlyfun.net%2FGossip%2FHibernateGossip%2FHibernateGossip.html&cc=24&ga_vid=1129055167.1188554514&ga_sid=1188804451&ga_hid=608548633&ga_fc=true&flash=9&u_h=768&u_w=1024&u_ah=740&u_aw=1024&u_cd=32&u_tz=480&u_his=3&u_java=true&u_nplug=17&u_nmime=57" frameborder="0" width="160" scrolling="no" height="600" allowtransparency="allowtransparency"></iframe>
<script type="text/javascript"><!--google_ad_client = "pub-9750319131714390";google_ad_width = 160;google_ad_height = 600;google_ad_format = "160x600_as";google_ad_type = "text_image";google_ad_channel = "";//--> </script> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"> </script> <iframe name="google_ads_frame" marginwidth="0" marginheight="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-9750319131714390&dt=1188805912171&lmt=1175337899&prev_fmts=160x600_as&format=160x600_as&output=html&correlator=1188805912109&url=http%3A%2F%2Fcaterpillar.onlyfun.net%2FGossip%2FHibernateGossip%2FCompactPK2.html&ad_type=text_image&ref=http%3A%2F%2Fcaterpillar.onlyfun.net%2FGossip%2FHibernateGossip%2FHibernateGossip.html&cc=24&ga_vid=1129055167.1188554514&ga_sid=1188804451&ga_hid=608548633&ga_fc=true&flash=9&u_h=768&u_w=1024&u_ah=740&u_aw=1024&u_cd=32&u_tz=480&u_his=3&u_java=true&u_nplug=17&u_nmime=57" frameborder="0" width="160" scrolling="no" height="600" allowtransparency="allowtransparency"></iframe>
- UserPK.java
package onlyfun.caterpillar;
import java.io.Serializable;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
public class UserPK implements Serializable {
private String name;
private String phone;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public boolean equals(Object obj) {
if(obj == this) {
return true;
}
if(!(obj instanceof User)) {
return false;
}
UserPK pk = (UserPK) obj;
return new EqualsBuilder()
.append(this.name, pk.getName())
.append(this.phone, pk.getPhone())
.isEquals();
}
public int hashCode() {
return new HashCodeBuilder()
.append(this.name)
.append(this.phone)
.toHashCode();
}
}
現在User類別的主鍵資訊被分離出來了,例如:
- User.java
package onlyfun.caterpillar;
import java.io.Serializable;
public class User implements Serializable {
private UserPK userPK; // 主鍵
private Integer age;
public User() {}
public UserPK getUserPK() {
return userPK;
}
public void setUserPK(UserPK userPK) {
this.userPK = userPK;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
在映射文件方面,需要指定主鍵類的資訊,例如:
- User.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>
<class name="onlyfun.caterpillar.User" table="user">
<composite-id name="userPK"
class="onlyfun.caterpillar.UserPK"
unsaved-value="any">
<key-property name="name"
column="name"
type="java.lang.String"/>
<key-property name="phone"
column="phone"
type="java.lang.String"/>
</composite-id>
<property name="age" column="age" type="java.lang.Integer"/>
</class>
</hibernate-mapping>
在查詢資料時,必須指定主鍵資訊,例如:
UserPK pk = new UserPK();
pk.setName("bush");
pk.setPhone("0970123456");
Session session = sessionFactory.openSession();
// 以主鍵類實例設定複合主鍵並載入對應的資料
User user = (User) session.load(User.class, pk);
System.out.println(user.getAge() + "/t" +
user.getUserPK().getName() + "/t" +
user.getUserPK().getPhone());
session.close();
pk.setName("bush");
pk.setPhone("0970123456");
Session session = sessionFactory.openSession();
// 以主鍵類實例設定複合主鍵並載入對應的資料
User user = (User) session.load(User.class, pk);
System.out.println(user.getAge() + "/t" +
user.getUserPK().getName() + "/t" +
user.getUserPK().getPhone());
session.close();
<script type="text/javascript"><!--google_ad_client = "pub-9750319131714390";google_ad_width = 160;google_ad_height = 600;google_ad_format = "160x600_as";google_ad_type = "text_image";google_ad_channel = "";//--> </script> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"> </script> <iframe name="google_ads_frame" marginwidth="0" marginheight="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-9750319131714390&dt=1188805912109&lmt=1175337899&format=160x600_as&output=html&correlator=1188805912109&url=http%3A%2F%2Fcaterpillar.onlyfun.net%2FGossip%2FHibernateGossip%2FCompactPK2.html&ad_type=text_image&ref=http%3A%2F%2Fcaterpillar.onlyfun.net%2FGossip%2FHibernateGossip%2FHibernateGossip.html&cc=24&ga_vid=1129055167.1188554514&ga_sid=1188804451&ga_hid=608548633&ga_fc=true&flash=9&u_h=768&u_w=1024&u_ah=740&u_aw=1024&u_cd=32&u_tz=480&u_his=3&u_java=true&u_nplug=17&u_nmime=57" frameborder="0" width="160" scrolling="no" height="600" allowtransparency="allowtransparency"></iframe>
<script type="text/javascript"><!--google_ad_client = "pub-9750319131714390";google_ad_width = 160;google_ad_height = 600;google_ad_format = "160x600_as";google_ad_type = "text_image";google_ad_channel = "";//--> </script> <script src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript"> </script> <iframe name="google_ads_frame" marginwidth="0" marginheight="0" src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-9750319131714390&dt=1188805912171&lmt=1175337899&prev_fmts=160x600_as&format=160x600_as&output=html&correlator=1188805912109&url=http%3A%2F%2Fcaterpillar.onlyfun.net%2FGossip%2FHibernateGossip%2FCompactPK2.html&ad_type=text_image&ref=http%3A%2F%2Fcaterpillar.onlyfun.net%2FGossip%2FHibernateGossip%2FHibernateGossip.html&cc=24&ga_vid=1129055167.1188554514&ga_sid=1188804451&ga_hid=608548633&ga_fc=true&flash=9&u_h=768&u_w=1024&u_ah=740&u_aw=1024&u_cd=32&u_tz=480&u_his=3&u_java=true&u_nplug=17&u_nmime=57" frameborder="0" width="160" scrolling="no" height="600" allowtransparency="allowtransparency"></iframe>
- Hibernate Gossip: 複合主鍵(二)
- Hibernate Gossip: 複合主鍵(一)
- Hibernate Gossip: 二級快取(Second-level)
- Java Gossip学习笔记 (二)
- Algorithm Gossip: 老鼠走迷官(二)
- Algorithm Gossip:老鼠走迷宫(二)
- 6.Algorithm Gossip: 老鼠走迷官(二)
- Hibernate Gossip学习笔记
- Hibernate Gossip: Criteria 進階查詢
- 清除节点上的gossip状态(二)
- Hibernate Gossip: 多对一
- Hibernate Gossip: 基本查詢
- Hibernate Gossip: 樂觀鎖定(Optimistic locking)
- Hibernate Gossip: Criteria 基本查询
- gossip(转载)
- 节间通讯(gossip)
- Gossip
- Gossip
- 自己写的加密字符串、数字、日期的类(VB.net)
- css学习笔记(1)加入方法 文本属性 颜色和背景
- 《Pro Spring》学习笔记之声明式事务
- AOP的四种主要Java实现方式
- Struts原理与实践
- Hibernate Gossip: 複合主鍵(二)
- .Net 2.0 新功能:Parital Classes(分部类)
- 报名网络考试了
- G.711编码
- linux的c编程技巧
- 8月份开基飙升 股票基金最赚钱
- .Net 2.0 新功能:迭代器(Iterators)
- 不影响网络性能 充分满足VoIP带宽
- .Net 2.0 新功能:可空类型(Nullbale Types)