1.pom.xml文件參考http://my.oschina.net/u/555061/blog/506049java
2.這裏使用註解方法(其實註解也就註解了2個字段爲主鍵),給出hibernate.cfg.xml配置跟http://my.oschina.net/u/555061/blog/506049相同,只是映射類改成<mapping class="EntityTest.Tour_Morph"/>sql
3.給出Tour_Morph.java,session
package EntityTest; import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.IdClass; import javax.persistence.Table; @Entity @IdClass(Tour_MorphID.class) @Table(name = "Tour_Morph") public class Tour_Morph { @Id @Column(name = "Tour_id") private Integer Tour_id; @Id @Column(name = "Morph_id") private Integer Morph_id; private String otherField; public Tour_Morph() { } public Tour_Morph(Integer Tour_id, Integer Morph_id, String otherField) { this.Tour_id = Tour_id; this.Morph_id = Morph_id; this.otherField = otherField; } public Integer getTour_id() { return Tour_id; } public void setTour_id(Integer tour_id) { Tour_id = tour_id; } public Integer getMorph_id() { return Morph_id; } public void setMorph_id(Integer morph_id) { Morph_id = morph_id; } public String getOtherField() { return otherField; } public void setOtherField(String otherField) { this.otherField = otherField; } } class Tour_MorphID implements Serializable { private static final long serialVersionUID = 155771990L; private Integer Tour_id; private Integer Morph_id; public int hashCode() { int result = 1; result = Tour_id.hashCode() + Morph_id.hashCode(); return result; } public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; final Tour_MorphID other = (Tour_MorphID) obj; if (Tour_id == other.Tour_id && Morph_id == other.Morph_id) return true; return false; } public Integer getTour_id() { return Tour_id; } public void setTour_id(Integer tour_id) { Tour_id = tour_id; } public Integer getMorph_id() { return Morph_id; } public void setMorph_id(Integer morph_id) { Morph_id = morph_id; } }
4.解釋下,這裏的關鍵點在於如何讓hibernate編譯經過2個字段作主鍵的狀況,默認都是一個字段作主鍵,因此關鍵點就是Tour_MorphID.java這個類,Tour_MorphID類的全部字段做爲Tour_Morph類的主鍵app
4.1Tour_MorphID implements Serializable 這個是要求this
4.2覆蓋了hashCode和equals兩個方法,spa
4.3public類註解增長了@IdClass,主鍵主鍵@Id有2個,.net
5.生成的表sql以下,
hibernate
CREATE TABLE `tour_morph` ( `Morph_id` int(11) NOT NULL, `Tour_id` int(11) NOT NULL, `otherField` varchar(255) DEFAULT NULL, PRIMARY KEY (`Morph_id`,`Tour_id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
6.使用的話跟一個主鍵的用法沒有任何區別code
public void testBasicUsage() { // create a couple of events... Session session = sessionFactory.openSession(); session.beginTransaction(); session.save(new Tour_Morph(1, 11, "otherField1")); session.save(new Tour_Morph(2, 22, "otherField2")); session.getTransaction().commit(); session.close(); // now lets pull events from the database and list them session = sessionFactory.openSession(); session.beginTransaction(); List<Tour_Morph> result = session.createQuery("from Tour_Morph").list(); for (Tour_Morph tour_morph : result) { System.out.println(tour_morph.getTour_id() + "|" + tour_morph.getMorph_id() + "|" + tour_morph.getOtherField()); } session.getTransaction().commit(); session.close(); }