您的位置:首页 > 数据库 > MySQL

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: