composite-id用法

<composite-id name="id" class="SysPopedomId" >
			<key-property name="popedomModule" type="string">
				<column name="popedomModule" sql-type="varchar(30)"></column>
			</key-property>
			<key-property name="popedomPrivilege" type="string">
				<column name="popedomPrivilege" sql-type="varchar(30)"></column>
			</key-property>
		</composite-id>
public class SysPopedom {
private SysPopedomId id;
private Integer sort;
private String title;
private String popedomName;
public String getPopedomName() {
	return popedomName;
}
public void setPopedomName(String popedomName) {
	this.popedomName = popedomName;
}
private String remark;
public SysPopedomId getId() {
	return id;
}
public void setId(SysPopedomId id) {
	this.id = id;
}
public Integer getSort() {
	return sort;
}
public void setSort(Integer sort) {
	this.sort = sort;
}
public String getTitle() {
	return title;
}
public void setTitle(String title) {
	this.title = title;
}
public String getRemark() {
	return remark;
}
public void setRemark(String remark) {
	this.remark = remark;
}

}
package cn.itcast.crm.domain;

import java.io.Serializable;

public class SysPopedomId implements Serializable{
private String popedomModule;
private String popedomPrivilege;
public String getPopedomModule() {
	return popedomModule;
}
@Override
public int hashCode() {
	final int prime = 31;
	int result = 1;
	result = prime * result
			+ ((popedomModule == null) ? 0 : popedomModule.hashCode());
	result = prime * result
			+ ((popedomPrivilege == null) ? 0 : popedomPrivilege.hashCode());
	return result;
}
@Override
public boolean equals(Object obj) {
	if (this == obj)
		return true;
	if (obj == null)
		return false;
	if (getClass() != obj.getClass())
		return false;
	SysPopedomId other = (SysPopedomId) obj;
	if (popedomModule == null) {
		if (other.popedomModule != null)
			return false;
	} else if (!popedomModule.equals(other.popedomModule))
		return false;
	if (popedomPrivilege == null) {
		if (other.popedomPrivilege != null)
			return false;
	} else if (!popedomPrivilege.equals(other.popedomPrivilege))
		return false;
	return true;
}
public void setPopedomModule(String popedomModule) {
	this.popedomModule = popedomModule;
}
public String getPopedomPrivilege() {
	return popedomPrivilege;
}
public void setPopedomPrivilege(String popedomPrivilege) {
	this.popedomPrivilege = popedomPrivilege;
}
}



此时不能用mapped=true这个属性,因为已经有name="id",会提示Caused by: org.hibernate.MappingException: cannot combine mapped="true" with specified name

或者

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<!--

  This mapping demonstrates how to use an id class not
  embedded in the entity. Only the property values are
  shared between the 2. This mimic the EJB2.1 and EJB3
  semantic.

-->

<hibernate-mapping package="org.hibernate.test.idclass">
	
	<class name="Customer">
		<composite-id class="CustomerId" mapped="true">
			<key-property name="orgName" column="org_name"/>
			<key-property name="customerName" column="cust_name"/>
		</composite-id>
        <discriminator column="disc"/>
        <property name="address"/>
		<subclass name="FavoriteCustomer"/>
	</class>

</hibernate-mapping>


 

//$Id: Customer.java 7858 2005-08-11 21:46:58Z epbernard $

package org.hibernate.test.idclass;





public class Customer {



	public Customer() {

		super();

	}



	public Customer(String orgName, String custName, String add) {

		this.orgName = orgName;

		this.customerName = custName;

		this.address = add;

	}



	private String orgName;

	private String customerName;

	private String address;



	public String getAddress() {

		return address;

	}



	public void setAddress(String address) {

		this.address = address;

	}



	public String getCustomerName() {

		return customerName;

	}



	public void setCustomerName(String customerName) {

		this.customerName = customerName;

	}



	public String getOrgName() {

		return orgName;

	}



	public void setOrgName(String orgName) {

		this.orgName = orgName;

	}



}


 

//$Id: CustomerId.java 7239 2005-06-20 09:44:54Z oneovthafew $
package org.hibernate.test.idclass;

import java.io.Serializable;

public class CustomerId implements Serializable {
	
	private String orgName;
	private String customerName;

	public CustomerId() {
		super();
	}

	public CustomerId(String orgName, String custName) {
		this.orgName = orgName;
		this.customerName = custName;
	}

	public String getCustomerName() {
		return customerName;
	}

	public void setCustomerName(String customerName) {
		this.customerName = customerName;
	}

	public String getOrgName() {
		return orgName;
	}

	public void setOrgName(String orgName) {
		this.orgName = orgName;
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值