一:pojo文件
package com.ibaiqi.po;
import java.io.Serializable;
public class Person implements Serializable{
private static final long serialVersionUID = 1L;
private String firstname;
private String lastname;
private String address;
private Integer phone;
public Person(){}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Integer getPhone() {
return phone;
}
public void setPhone(Integer phone) {
this.phone = phone;
}
public int hashCode() {
return getFirstname().hashCode()*11 +
getLastname().hashCode();
}
public boolean equals(Object obj) {
if(null == obj){
return false;
}
if(this == obj){
return true;
}
if(obj.getClass() == Person.class){
Person p = (Person)obj;
if(p.getFirstname().equals(getFirstname()) &&
p.getLastname().equals(getLastname())){
return true;
}
}
return false;
}
}
<!-- ~ Hibernate, Relational Persistence for Idiomatic Java ~ ~ Copyright
(c) 2010, Red Hat Inc. or third-party contributors as ~ indicated by the
@author tags or express copyright attribution ~ statements applied by the
authors. All third-party contributions are ~ distributed under license by
Red Hat Inc. ~ ~ This copyrighted material is made available to anyone wishing
to use, modify, ~ copy, or redistribute it subject to the terms and conditions
of the GNU ~ Lesser General Public License, as published by the Free Software
Foundation. ~ ~ This program is distributed in the hope that it will be useful,
~ but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
~ or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License ~ for more details. ~ ~ You should have received a copy of the GNU
Lesser General Public License ~ along with this distribution; if not, write
to: ~ Free Software Foundation, Inc. ~ 51 Franklin Street, Fifth Floor ~
Boston, MA 02110-1301 USA -->
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.ibaiqi.po">
<class name="Person" table="test_person">
<composite-id>
<key-property name="firstname" column="firstname" type="java.lang.String" length="50"/>
<key-property name="lastname" column="lastname" type="java.lang.String" length="50"/>
</composite-id>
<property name="address" column="address" type="string"/>
<property name="phone" column="phone" type="integer"/>
</class>
</hibernate-mapping>
package com.ibaiqi.po;
import java.io.Serializable;
public class Person implements Serializable{
private static final long serialVersionUID = 1L;
private String firstname;
private String lastname;
private String address;
private Integer phone;
public Person(){}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Integer getPhone() {
return phone;
}
public void setPhone(Integer phone) {
this.phone = phone;
}
public int hashCode() {
return getFirstname().hashCode()*11 +
getLastname().hashCode();
}
public boolean equals(Object obj) {
if(null == obj){
return false;
}
if(this == obj){
return true;
}
if(obj.getClass() == Person.class){
Person p = (Person)obj;
if(p.getFirstname().equals(getFirstname()) &&
p.getLastname().equals(getLastname())){
return true;
}
}
return false;
}
}
二:映射文件配置
<!-- ~ Hibernate, Relational Persistence for Idiomatic Java ~ ~ Copyright
(c) 2010, Red Hat Inc. or third-party contributors as ~ indicated by the
@author tags or express copyright attribution ~ statements applied by the
authors. All third-party contributions are ~ distributed under license by
Red Hat Inc. ~ ~ This copyrighted material is made available to anyone wishing
to use, modify, ~ copy, or redistribute it subject to the terms and conditions
of the GNU ~ Lesser General Public License, as published by the Free Software
Foundation. ~ ~ This program is distributed in the hope that it will be useful,
~ but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
~ or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
License ~ for more details. ~ ~ You should have received a copy of the GNU
Lesser General Public License ~ along with this distribution; if not, write
to: ~ Free Software Foundation, Inc. ~ 51 Franklin Street, Fifth Floor ~
Boston, MA 02110-1301 USA -->
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.ibaiqi.po">
<class name="Person" table="test_person">
<composite-id>
<key-property name="firstname" column="firstname" type="java.lang.String" length="50"/>
<key-property name="lastname" column="lastname" type="java.lang.String" length="50"/>
</composite-id>
<property name="address" column="address" type="string"/>
<property name="phone" column="phone" type="integer"/>
</class>
</hibernate-mapping>