hibernate 与联合主键

来源:互联网 发布:解放军艺术学院 知乎 编辑:程序博客网 时间:2024/06/09 14:01

第一种情况 表中有联合主键 假设表是bedRoom

hibernate逆向生成三个文件

第一个bedRoomId.java(主键所在的类

package com.pojos;public class BedroomId implements java.io.Serializable {// Fieldsprivate Integer id;private String classname;// Constructors/** default constructor */public BedroomId() {}/** full constructor */public BedroomId(Integer id, String classname) {this.id = id;this.classname = classname;}// Property accessorspublic Integer getId() {return this.id;}public void setId(Integer id) {this.id = id;}public String getClassname() {return this.classname;}public void setClassname(String classname) {this.classname = classname;}public boolean equals(Object other) {if ((this == other))return true;if ((other == null))return false;if (!(other instanceof BedroomId))return false;BedroomId castOther = (BedroomId) other;return ((this.getId() == castOther.getId()) || (this.getId() != null&& castOther.getId() != null && this.getId().equals(castOther.getId())))&& ((this.getClassname() == castOther.getClassname()) || (this.getClassname() != null&& castOther.getClassname() != null && this.getClassname().equals(castOther.getClassname())));}public int hashCode() {int result = 17;result = 37 * result + (getId() == null ? 0 : this.getId().hashCode());result = 37 * result+ (getClassname() == null ? 0 : this.getClassname().hashCode());return result;}}

 第二个文件  bedRoom.java

package com.pojos;public class Bedroom implements java.io.Serializable {// Fieldsprivate BedroomId id;private String name;private String bedroomnum;private String telephone;// Constructors/** default constructor */public Bedroom() {}/** minimal constructor */public Bedroom(BedroomId id) {this.id = id;}/** full constructor */public Bedroom(BedroomId id, String name, String bedroomnum,String telephone) {this.id = id;this.name = name;this.bedroomnum = bedroomnum;this.telephone = telephone;}// Property accessorspublic BedroomId getId() {return this.id;}public void setId(BedroomId id) {this.id = id;}public String getName() {return this.name;}public void setName(String name) {this.name = name;}public String getBedroomnum() {return this.bedroomnum;}public void setBedroomnum(String bedroomnum) {this.bedroomnum = bedroomnum;}public String getTelephone() {return this.telephone;}public void setTelephone(String telephone) {this.telephone = telephone;}}

第三个文件bedRoom.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"><!--     Mapping file autogenerated by MyEclipse Persistence Tools--><hibernate-mapping>    <class name="com.pojos.Bedroom" table="bedroom" catalog="test">        <composite-id name="id" class="com.pojos.BedroomId">            <key-property name="id" type="java.lang.Integer">                <column name="id" />            </key-property>            <key-property name="classname" type="java.lang.String">                <column name="classname" length="20" />            </key-property>        </composite-id>        <property name="name" type="java.lang.String">            <column name="name" length="20">                <comment>姓名</comment>            </column>        </property>        <property name="bedroomnum" type="java.lang.String">            <column name="bedroomnum" length="20">                <comment>寝室宿舍号</comment>            </column>        </property>        <property name="telephone" type="java.lang.String">            <column name="telephone" length="20">                <comment>联系方式</comment>            </column>        </property>    </class></hibernate-mapping>


第二种 是没有主键

bedRoomId.java

package com.pojos;/** * BedroomId entity. @author MyEclipse Persistence Tools */public class BedroomId implements java.io.Serializable {// Fieldsprivate Integer id;private String classname;private String name;private String bedroomnum;private String telephone;// Constructors/** default constructor */public BedroomId() {}/** minimal constructor */public BedroomId(Integer id, String classname) {this.id = id;this.classname = classname;}/** full constructor */public BedroomId(Integer id, String classname, String name,String bedroomnum, String telephone) {this.id = id;this.classname = classname;this.name = name;this.bedroomnum = bedroomnum;this.telephone = telephone;}// Property accessorspublic Integer getId() {return this.id;}public void setId(Integer id) {this.id = id;}public String getClassname() {return this.classname;}public void setClassname(String classname) {this.classname = classname;}public String getName() {return this.name;}public void setName(String name) {this.name = name;}public String getBedroomnum() {return this.bedroomnum;}public void setBedroomnum(String bedroomnum) {this.bedroomnum = bedroomnum;}public String getTelephone() {return this.telephone;}public void setTelephone(String telephone) {this.telephone = telephone;}public boolean equals(Object other) {if ((this == other))return true;if ((other == null))return false;if (!(other instanceof BedroomId))return false;BedroomId castOther = (BedroomId) other;return ((this.getId() == castOther.getId()) || (this.getId() != null&& castOther.getId() != null && this.getId().equals(castOther.getId())))&& ((this.getClassname() == castOther.getClassname()) || (this.getClassname() != null&& castOther.getClassname() != null && this.getClassname().equals(castOther.getClassname())))&& ((this.getName() == castOther.getName()) || (this.getName() != null&& castOther.getName() != null && this.getName().equals(castOther.getName())))&& ((this.getBedroomnum() == castOther.getBedroomnum()) || (this.getBedroomnum() != null&& castOther.getBedroomnum() != null && this.getBedroomnum().equals(castOther.getBedroomnum())))&& ((this.getTelephone() == castOther.getTelephone()) || (this.getTelephone() != null&& castOther.getTelephone() != null && this.getTelephone().equals(castOther.getTelephone())));}public int hashCode() {int result = 17;result = 37 * result + (getId() == null ? 0 : this.getId().hashCode());result = 37 * result+ (getClassname() == null ? 0 : this.getClassname().hashCode());result = 37 * result+ (getName() == null ? 0 : this.getName().hashCode());result = 37* result+ (getBedroomnum() == null ? 0 : this.getBedroomnum().hashCode());result = 37 * result+ (getTelephone() == null ? 0 : this.getTelephone().hashCode());return result;}}

BedRoom.java

package com.pojos;/** * Bedroom entity. @author MyEclipse Persistence Tools */public class Bedroom implements java.io.Serializable {// Fieldsprivate BedroomId id;// Constructors/** default constructor */public Bedroom() {}/** full constructor */public Bedroom(BedroomId id) {this.id = id;}// Property accessorspublic BedroomId getId() {return this.id;}public void setId(BedroomId id) {this.id = id;}}

bedRoom.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"><!--     Mapping file autogenerated by MyEclipse Persistence Tools--><hibernate-mapping>    <class name="com.pojos.Bedroom" table="bedroom" catalog="test">        <composite-id name="id" class="com.pojos.BedroomId">            <key-property name="id" type="java.lang.Integer">                <column name="id" />            </key-property>            <key-property name="classname" type="java.lang.String">                <column name="classname" length="20" />            </key-property>            <key-property name="name" type="java.lang.String">                <column name="name" length="20" />            </key-property>            <key-property name="bedroomnum" type="java.lang.String">                <column name="bedroomnum" length="20" />            </key-property>            <key-property name="telephone" type="java.lang.String">                <column name="telephone" length="20" />            </key-property>        </composite-id>    </class></hibernate-mapping>



原创粉丝点击