hibernate 在mysql中复合主键应用
来源:互联网 发布:游泳减肥 知乎 编辑:程序博客网 时间:2024/05/21 23:15
一: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>
0 0
- hibernate 在mysql中复合主键应用
- hibernate中复合主键
- Hibernate中复合主键映射
- Hibernate中复合主键的配置
- Hibernate中复合主键的配置
- Hibernate中复合主键的配置
- Hibernate中复合主键的配置
- Hibernate中复合主键的用法
- hibernate中复合主键的使用方法
- Hibernate复合主键
- hibernate 复合主键
- hibernate复合主键
- Hibernate复合主键例子
- Hibernate复合主键
- Hibernate复合主键
- Hibernate 复合主键
- hibernate复合主键映射
- Hibernate复合主键映射
- 标准化与归一化
- 一个总结很好的机器学习算法链接
- fusioncharts 之animation动画
- TTCN-3初探
- C++3 循环
- hibernate 在mysql中复合主键应用
- N皇后问题公式解!!!
- fusioncharts水平,垂直标题动画展示
- MFC实现静态图片控件拖动(改)
- word中添加和删除菜单项
- hdu-1874 畅通工程续 最短路径问题
- 试图对C++友元函数,静态成员函数做个小结
- 安卓Static关键字
- 春js代码实现tabs功能