本站首页    管理页面    写新日志    退出


«August 2025»
12
3456789
10111213141516
17181920212223
24252627282930
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='流浪' 数据出现则操作正确


阅读全文(2170) | 回复(0) | 编辑 | 精华
 



发表评论:
昵称:
密码:
主页:
标题:
验证码:  (不区分大小写,请仔细填写,输错需重写评论内容!)



站点首页 | 联系我们 | 博客注册 | 博客登陆

Sponsored By W3CHINA
W3CHINA Blog 0.8 Processed in 0.031 second(s), page refreshed 144786142 times.
《全国人大常委会关于维护互联网安全的决定》  《计算机信息网络国际联网安全保护管理办法》
苏ICP备05006046号