Hibernate中对无主键表的操作

Hibernate中对无主键表的操作

说明:

日志表(T_B_OPERLOG)中无主键,在hibernate按一般有主键的方式来改写映射文件和生成的持久层对象;
以一般有主键的方式来操作持久层对象,就可以实现Hibernate中对无主键表的操作。
改写映射文件时要注意最后从所有属性中选择一个有唯一性的属性作为一个“主键”,如:
  <id name="operdate" type="java.util.Date">
            <column name="OPERDATE" length="7" />
   </id>
operdate是日志的操作时间,这样就可以把operdate作为伪主键,其他操作与平常有主键建对象的操作一样。
至于其中的hibernate的处理机制还不了解,但这样可以实现我的处理要求了,有时间再研究实现机制。

Hibernate的映射文件: TBOperlog.hbm.xml

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--   
        Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
        <class name="cn.isbn.db.model.TBOperlog" table="T_B_OPERLOG" schema="LAB1107">
               
                        <!--    
                <id name="logid" type="java.lang.String">
                        <column name="LOGID" length="20" />                       
                </id>
                -->
             <id name="operdate" type="java.util.Date">
                        <column name="OPERDATE" length="7" />
                </id>
             <property name="logid" type="java.lang.String">
                        <column name="LOGID" length="20" />                       
             </property>
       
                <property name="opertable" type="java.lang.String">
                        <column name="OPERTABLE" length="100" not-null="true" />
                </property>
                <property name="operkey" type="java.lang.String">
                        <column name="OPERKEY" length="100" not-null="true" />
                </property>
                <property name="opertype" type="java.lang.String">
                        <column name="OPERTYPE" length="10" not-null="true" />
                </property>
                <property name="operperson" type="java.lang.String">
                        <column name="OPERPERSON" length="200" not-null="true" />
                </property>
                <!--                   
                <property name="operdate" type="java.util.Date">
                        <column name="OPERDATE" length="7" />
                </property>
                -->
                <property name="operdesc" type="java.lang.String">
                        <column name="OPERDESC" length="4000" not-null="true" />
                </property>
                <property name="recordstauts" type="java.lang.String">
                        <column name="RECORDSTAUTS" length="1" />
                </property>
                <property name="field1" type="java.lang.String">
                        <column name="FIELD1" length="500" />
                </property>
                <property name="field2" type="java.lang.String">
                        <column name="FIELD2" length="500" />
                </property>
        </class>
</hibernate-mapping>

持久对象文件:TBOperlog.java
package cn.isbn.db.model;

import java.util.Date;

/**
* TBOperlog entity.
*   
* @author MyEclipse Persistence Tools
*/

public class TBOperlog implements java.io.Serializable
{

   
  // Fields

  private String logid;
  private String opertable;
  private String operkey;
  private String opertype;
  private String operperson;
  private Date operdate;
  private String operdesc;
  private String recordstauts;
  private String field1;
  private String field2;

  // Constructors


  /**
    * 无参数的构造函数,只初始化记录编号和时间
    */
  public TBOperlog()
  {
    this.recordstauts="1";
    this.operdate = new Date();
  }

  /**
    * 必填字段构造函数
    * @param opertable 操作数据表
    * @param operkey 操作主键
    * @param opertype 操作类型
    * @param operperson 操作人
    * @param operdesc 操作描述
    */
  public TBOperlog(String opertable, String operkey, String opertype,
      String operperson, String operdesc)
  {
    this.opertable = opertable;
    this.operkey = operkey;
    this.opertype = opertype;
    this.operperson = operperson;
    this.operdesc = operdesc;
    this.recordstauts="1";
    this.operdate = new Date();
  }

  /** full constructor */
  public TBOperlog(String opertable, String operkey, String opertype,
      String operperson, String operdesc,
      String field1, String field2)
  {
    this.opertable = opertable;
    this.operkey = operkey;
    this.opertype = opertype;
    this.operperson = operperson;
    this.operdate = new Date();
    this.operdesc = operdesc;
    this.recordstauts = "1";
    this.field1 = field1;
    this.field2 = field2;
  }

  // Property accessors

  public String getLogid()
  {
    return this.logid;
  }

  public void setLogid(String logid)
  {
    this.logid = logid;
  }

  public String getOpertable()
  {
    return this.opertable;
  }

  public void setOpertable(String opertable)
  {
    this.opertable = opertable;
  }

  public String getOperkey()
  {
    return this.operkey;
  }

  public void setOperkey(String operkey)
  {
    this.operkey = operkey;
  }

  public String getOpertype()
  {
    return this.opertype;
  }

  public void setOpertype(String opertype)
  {
    this.opertype = opertype;
  }

  public String getOperperson()
  {
    return this.operperson;
  }

  public void setOperperson(String operperson)
  {
    this.operperson = operperson;
  }

  public Date getOperdate()
  {
    return this.operdate;
  }

  public void setOperdate(Date operdate)
  {
    this.operdate = operdate;
  }

  public String getOperdesc()
  {
    return this.operdesc;
  }

  public void setOperdesc(String operdesc)
  {
    this.operdesc = operdesc;
  }

  public String getRecordstauts()
  {
    return this.recordstauts;
  }

  public void setRecordstauts(String recordstauts)
  {
    this.recordstauts = recordstauts;
  }

  public String getField1()
  {
    return this.field1;
  }

  public void setField1(String field1)
  {
    this.field1 = field1;
  }

  public String getField2()
  {
    return this.field2;
  }

  public void setField2(String field2)
  {
    this.field2 = field2;
  }

}

调用对象:

public boolean addLog(TBOperlog log)
  {
    // TODO Auto-generated method stub
    boolean result = false;
    String logid = getLogid(); // 得到记录编号
    if (!logid.equals(""))
    {// 如果取得记录编号
      log.setLogid(logid);
      this.save(log);
      result = true;
    }
    return result;
  }

补充:

下面是从网上找的一个解决方法,对我的处理没帮助,也许日后对对其他的处理用的着,也贴出来,作为备忘储备吧。

来自:http://www.host01.com/article/jsp/00040007/20060805215458644.htm

Db2 表:Test 只有一个测试字段:name character(10)

Hibernate的hbm文件:Test.hbm.xml

<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping SYSTEM "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"   

>
<hibernate-mapping>
<class name="Test" table="test">
<composite-id name="testpk" class="Testpk" unsaved-value="any">   
    <key-property name="name" column="name" type="string"/>   
        </composite-id>   
</class>
</hibernate-mapping>

   

Test.java

   

import java.io.Serializable;

public class Test implements Serializable{

private Testpk testpk;
   
public void setTestpk(Testpk value){
    this.testpk=value;
}
   
public Testpk getTestpk(){
    return this.testpk;
}
   
}


Testpk.java

import java.io.Serializable;   

public class Testpk implements Serializable{
   
private String name;
public String getName()
{
    return this.name;
}
   
public void setName(String value){
    this.name=value;
}
   
public boolean equals(Object other) {   
            Testpk that = (Testpk) other;   
           
            return this.name.equals(that.name);   
            }   
        
            /**   
        * Returns the hash code for the key.   
        */
    
            public int hashCode() {   
           
            return (this.name.hashCode());
            
            }   

}


测试代码:

Test t=new Test();
    Testpk tpk=new Testpk();
    tpk.setName("test000000");
    t.setTestpk(tpk);
    session.save(t);

最后值得注意的是当要load的时候,不能简单的Test t=(Test)session.load(Test.class,"test000000"); 而

使用一个Testpk作为一个实体类的标识符。

所以应该这么写:

Testpk tpk=new Testpk();
tpk.setName("test000000");
Test t=(Test)session.load(Test.class,tpk);

   

环境DB2、Eclipise、Hibernate2测试成功



本文出自 “在路上” 博客,请务必保留此出处http://yuwenhu.blog.51cto.com/672091/160930

猜你喜欢

转载自yupengcc.iteye.com/blog/1320195