<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;
}
}