hibernate 在mysql中复合主键应用

一: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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值