Hibernate 使用復合主鍵
在數據庫ssh下建立一個表dog:
create table dog( id int(11) not null, dog_id int(11) not null, name varchar(20) default null, primary key(id,dog_id) )engine=innodb default charset=gbk;
建立一個主鍵類DogId.java:
package helloworld.compositeid; public class DogId implements java.io.Serializable { // Fields private Integer id; private Integer dogId; // Constructors /** default constructor */ public DogId() { } /** full constructor */ public DogId(Integer id, Integer dogId) { this.id = id; this.dogId = dogId; } // Property accessors public Integer getId() { return this.id; } public void setId(Integer id) { this.id = id; } public Integer getDogId() { return this.dogId; } public void setDogId(Integer dogId) { this.dogId = dogId; } public boolean equals(Object other) { if ((this == other)) return true; if ((other == null)) return false; if (!(other instanceof DogId)) return false; DogId castOther = (DogId) other; return ((this.getId() == castOther.getId()) || (this.getId() != null && castOther.getId() != null && this.getId().equals( castOther.getId()))) && ((this.getDogId() == castOther.getDogId()) || (this .getDogId() != null && castOther.getDogId() != null && this.getDogId() .equals(castOther.getDogId()))); } public int hashCode() { int result = 17; result = 37 * result + (getId() == null ? 0 : this.getId().hashCode()); result = 37 * result + (getDogId() == null ? 0 : this.getDogId().hashCode()); return result; } }
建立一個類Dog.java:
package helloworld.compositeid; public class Dog implements java.io.Serializable { private DogId id; private String name; private String color; public Dog() { } public Dog(DogId id) { this.id = id; } public Dog(DogId id, String name, String color) { this.id = id; this.name = name; this.color = color; } public DogId getId() { return this.id; } public void setId(DogId id) { this.id = id; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } public String getColor() { return this.color; } public void setColor(String color) { this.color = color; } }
建立映射文件Dog.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="helloworld.compositeid.Dog" table="dog" catalog="ssh"> <!--定義復合主鍵--> <composite-id name="id" class="helloworld.compositeid.DogId"> <key-property name="id" type="java.lang.Integer"> <column name="id" /> </key-property> <key-property name="dogId" type="java.lang.Integer"> <column name="dog_id" /> </key-property> </composite-id> <property name="name" type="java.lang.String"> <column name="name" length="20" /> </property> <property name="color" type="java.lang.String"> <column name="color" length="10" /> </property> </class> </hibernate-mapping>
編寫一個測試類,實現基本CRUD操作,Test.java:
package helloworld.compositeid; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.hibernate.Transaction; import org.hibernate.cfg.Configuration; public class Test { /** * @param args */ public static void main(String[] args) { // Configuration管理Hibernate配置 Configuration config = new Configuration().configure(); // 根據Configuration建立 SessionFactory // SessionFactory用來建立Session SessionFactory sessionFactory = config.buildSessionFactory(); // 增加記錄 // 建立主鍵類實例 DogId dogid = new DogId(); dogid.setId(1); dogid.setDogId(1); // 建立Dog對象 Dog dog = new Dog(); dog.setName("Tom"); dog.setColor("yellow"); dog.setId(dogid); Session session = sessionFactory.openSession(); Transaction tx = null; try { tx = session.beginTransaction(); session.save(dog); tx.commit(); } catch (RuntimeException e) { if (tx != null) tx.rollback(); throw e; } finally { session.close(); } // 查找數據 session = sessionFactory.openSession(); dog = (Dog) session.get(Dog.class, dogid); System.out.println(dog.getId().getId() + " " + dog.getId().getDogId() + " " + dog.getName()); session.close(); // 修改數據 dog.setName("Kitty"); session = sessionFactory.openSession(); tx = null; try { tx = session.beginTransaction(); session.update(dog); tx.commit(); } catch (RuntimeException e) { if (tx != null) tx.rollback(); throw e; } finally { session.close(); } // 刪除數據 session = sessionFactory.openSession(); tx = null; try { tx = session.beginTransaction(); session.delete(dog); tx.commit(); } catch (RuntimeException e) { if (tx != null) tx.rollback(); throw e; } finally { session.close(); } // 關閉sessionFactory sessionFactory.close(); } }
源程序解讀:
1.本實例使用了單獨的主鍵類DogId。
2.主鍵類DogId重寫了equals方法和hashCode方法,這一點非常重要。
3.Hibernate要求主鍵類必須實現Serializable接口。
4.復合主鍵的值是一個主鍵類,而不是一個普通的常見數值。
5.復合主鍵的映射文件使用composite-id表示。
來自:http://blog.csdn.net/itzyjr/article/details/8487675
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!