hibernate 与联合主键

本文介绍Hibernate框架下两种复合主键的配置方式:一种为独立的联合主键类,另一种为无主键类。详细展示了Java实体类及映射文件的实现细节。

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

hibernate逆向生成三个文件

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

package com.pojos;

public class BedroomId implements java.io.Serializable {

	// Fields

	private 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 accessors

	public 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 {

	// Fields

	private 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 accessors

	public 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 {

	// Fields

	private 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 accessors

	public 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 {

	// Fields

	private BedroomId id;

	// Constructors

	/** default constructor */
	public Bedroom() {
	}

	/** full constructor */
	public Bedroom(BedroomId id) {
		this.id = id;
	}

	// Property accessors

	public 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>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值