« | August 2025 | » | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | | | | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | | | | | | | |
| 公告 |
|
Blog信息 |
blog名称:流浪狗 日志总数:44 评论数量:19 留言数量:-2 访问次数:191010 建立时间:2008年3月13日 |

| |
[Hibernate]Hibernate + My SQL 基于主键类的复合主键 软件技术
流浪狗 发表于 2008/3/13 13:25:01 |
实现复合主键,要实现以下要求
1.本类必须实现Serializable接口
2.复写equals和hashCode方法
equals:对象比较方法
hashCode:取得hashCode编码
对于equals和hashCode的复写可以使用第三方工具commons-lang-2.3.jar
http://apache.mirror.phpchina.com/commons/lang/binaries/ 下载包:commons-lang-2.3.zip
解压后将commons-lang-2.3.jar放到本地Web Porject->WebRoot->WEB-INF->lib下,在本例中主要使用它的两个方法EqualsBuilder和HashCodeBuilder
创建Guest表
DROP TABLE IF EXISTS `mydatabase`.`guest`;CREATE TABLE `mydatabase`.`guest` ( `LASTNAME` varchar(50) NOT NULL default '', `FIRSTNAME` varchar(50) NOT NULL default '', `AGE` int(10) unsigned NOT NULL default '0', PRIMARY KEY (`LASTNAME`,`FIRSTNAME`))
创建主键类GuestId.java
package org.cool.hibernate.demo02;
import java.io.Serializable;
import org.apache.commons.lang.builder.EqualsBuilder;import org.apache.commons.lang.builder.HashCodeBuilder;
/** * GuestId generated by MyEclipse - Hibernate Tools */
public class GuestId implements Serializable {
private static final long serialVersionUID = 1L;
private String lastname;
private String firstname;
// Constructors
/** default constructor */ public GuestId() { }
/** full constructor */ public GuestId(String lastname, String firstname) { this.lastname = lastname; this.firstname = firstname; }
// Property accessors
public String getLastname() { return this.lastname; }
public void setLastname(String lastname) { this.lastname = lastname; }
public String getFirstname() { return this.firstname; }
public void setFirstname(String firstname) { this.firstname = firstname; }
/*覆写equals方法*/
public boolean equals(Object other) { if(!(other instanceof Guest)){ return false; } Guest gst = (Guest)other; return new EqualsBuilder() .appendSuper(super.equals(other)) .append(this.lastname, gst.getId().getLastname()) .append(this.firstname, gst.getId().getFirstname()) .isEquals(); }
/*覆写hashCode方法*/
public int hashCode() { return new HashCodeBuilder(-528253723,-475504089) .appendSuper(super.hashCode()) .append(this.lastname) .append(this.firstname) .toHashCode(); }
}
创建POJO类Guest.java
package org.cool.hibernate.demo02;
import java.io.Serializable;
/** * Guest generated by MyEclipse - Hibernate Tools */
public class Guest implements Serializable {
private static final long serialVersionUID = 1L;
private GuestId id;
private Integer age;
// Constructors
/** default constructor */ public Guest() { }
/** full constructor */ public Guest(GuestId id, Integer age) { this.id = id; this.age = age; }
// Property accessors
public GuestId getId() { return this.id; }
public void setId(GuestId id) { this.id = id; }
public Integer getAge() { return this.age; }
public void setAge(Integer age) { this.age = age; }
}
创建配置文件Guest.hbm.xml
<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN""http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"><hibernate-mapping> <class name="org.cool.hibernate.demo02.Guest" table="guest" catalog="mydatabase"> <composite-id name="id" class="org.cool.hibernate.demo02.GuestId"> <key-property name="lastname" type="string"> <column name="LASTNAME" length="50" /> </key-property> <key-property name="firstname" type="string"> <column name="FIRSTNAME" length="50" /> </key-property> </composite-id> <property name="age" type="integer"> <column name="AGE" not-null="true" /> </property> </class></hibernate-mapping>
创建操作类GuestOperate.java
package org.cool.hibernate.demo02;
import org.hibernate.Session;import org.hibernate.cfg.Configuration;
public class GuestOperate { private Session session;
public GuestOperate() { this.session = new Configuration().configure().buildSessionFactory() .openSession(); }
public void insert(Guest p) { this.session.save(p); this.session.beginTransaction().commit(); }}
创建测试类Test.java
package org.cool.hibernate.demo02;
public class Test {
/** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Guest guest = new Guest(); GuestId id = new GuestId(); id.setLastname("狗"); id.setFirstname("流浪"); guest.setAge(25); guest.setId(id) ; GuestOperate go = new GuestOperate() ; go.insert(guest) ; }
}
在hibernate.cfg.xml中<session-factory>...</session-factory>之间加上
<!-- 映射文件配置,注意配置文件名必须包含其相对于根的全路径 -->
<mapping resource="org/cool/hibernate/demo02/Guest.hbm.xml" />
运行Test,然后到数据库Guest查询数据
SELECT *
FROM Guest
WHERE LASTNAME='狗'
AND FIRSTNAME='流浪'
数据出现则操作正确 |
|
|