Hibernate无主键配置文件编写

来自:http://yuwenhu.blog.51cto.com/672091/160930
说明:

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

如以下片段:
<?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> 


网上的另外的方法:

来自: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://dato0123.iteye.com/blog/1265511,有时间在细看

猜你喜欢

转载自13521308103.iteye.com/blog/1886424