hibernate 在mysql中复合主键应用
2014-12-13 22:29
363 查看
一: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;
}
}
二:映射文件配置
<?xml version="1.0"?>
<!-- ~ 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;
}
}
二:映射文件配置
<?xml version="1.0"?>
<!-- ~ 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>
相关文章推荐
- 两种方式配置Hibernate复合主键(修正)
- Hibernate之---复合主键
- MySQL如何创建主键,外键和复合主键
- hibernate3 注释生成复合主键或者嵌入式主键的方法及实例
- mysql hibernate主键配置
- Hibernate 调用带有复合主键的stored procedure
- Hibernate 学习笔记( 三)-- 复合主键
- hibernate注解方式实现复合主键
- hibernate注解方式实现复合主键
- Hibernate入门3(复合主键)
- Hibernate复合主键的处理
- hibernate复合主键及关联的实现
- 关于Hibernate复合主键的问题
- Hibernate学习21 -- 关联映射11 -- 复合主键映射(composite-id)
- Hibernate复合主键
- Hibernate复合主键
- hibernate-MySQL配置文件及简单应用(CRUD)
- Hibernate笔记之3复合主键
- MySQL 创建主键,外键和复合主键的语句
- Hibernate的复合主键映射