您的位置:首页 > 编程语言 > Go语言

Hibernate Gossip: 複合主鍵(二)

2007-09-03 15:50 127 查看
可以將主鍵的資訊獨立為一個類別,例如:

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();

<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
4000
= "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>
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息