标签:
1.pom.xml文件参考http://my.oschina.net/u/555061/blog/506049
2.这里使用注解方法(其实注解也就注解了2个字段为主键),给出hibernate.cfg.xml配置跟http://my.oschina.net/u/555061/blog/506049相同,只是映射类改为<mapping class="EntityTest.Tour_Morph"/>
3.给出Tour_Morph.java,
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类的主键
4.1Tour_MorphID implements Serializable 这个是要求
4.2覆盖了hashCode和equals两个方法,
4.3public类注解增加了@IdClass,主键主键@Id有2个,
5.生成的表sql如下,
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.使用的话跟一个主键的用法没有任何区别
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(); }
标签:
原文地址:http://my.oschina.net/u/555061/blog/506131