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> |
相关文章推荐
- Hibernate Gossip学习笔记
- Hibernate Gossip: 基本查詢
- Hibernate Gossip: Criteria 基本查询
- Hibernate Gossip: 多对一
- [转]Hibernate Gossip: 簡介快取(Session Level)
- Hibernate Gossip: 複合主鍵(一)
- Hibernate Gossip: Criteria 基本查詢
- Hibernate Gossip: 樂觀鎖定(Optimistic locking)
- [main] ERROR org.hibernate.event.def.AbstractFlushingEventListener - Could not synchronize database
- hibernate延迟加载(get和load的区别)
- Hibernate_映射_继承结构映射1_使用一张表的方式
- Hibernate的二级缓存
- Eclipse快速上手Hibernate之入门实例(二)
- 基本的Hibernate配置文件
- Hibernate的getTransaction()和beginTransaction()
- 解决Hibernate:could not initialize proxy - no Session
- hibernate session.flush()的使用
- Hibernate——搭建
- Hibernate的乐观锁与悲观锁
- Hibernate框架3