Hibernate中对无主键表的操作
Hibernate中对无主键表的操作
说明:
日志表(T_B_OPERLOG)中无主键,在hibernate按一般有主键的方式来改写映射文件和生成的持久层对象;
以一般有主键的方式来操作持久层对象,就可以实现Hibernate中对无主键表的操作。
改写映射文件时要注意最后从所有属性中选择一个有唯一性的属性作为一个“主键”,如:
<idname="operdate"type="java.util.Date">
<columnname="OPERDATE"length="7"/>
</id>
operdate是日志的操作时间,这样就可以把operdate作为伪主键,其他操作与平常有主键建对象的操作一样。
至于其中的hibernate的处理机制还不了解,但这样可以实现我的处理要求了,有时间再研究实现机制。
Hibernate的映射文件:TBOperlog.hbm.xml
<?xmlversion="1.0"encoding="utf-8"?>
<!DOCTYPEhibernate-mappingPUBLIC"-//Hibernate/HibernateMappingDTD3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<!--
MappingfileautogeneratedbyMyEclipsePersistenceTools
-->
<hibernate-mapping>
<classname="cn.isbn.db.model.TBOperlog"table="T_B_OPERLOG"schema="LAB1107">
<!--
<idname="logid"type="java.lang.String">
<columnname="LOGID"length="20"/>
</id>
-->
<idname="operdate"type="java.util.Date">
<columnname="OPERDATE"length="7"/>
</id>
<propertyname="logid"type="java.lang.String">
<columnname="LOGID"length="20"/>
</property>
<propertyname="opertable"type="java.lang.String">
<columnname="OPERTABLE"length="100"not-null="true"/>
</property>
<propertyname="operkey"type="java.lang.String">
<columnname="OPERKEY"length="100"not-null="true"/>
</property>
<propertyname="opertype"type="java.lang.String">
<columnname="OPERTYPE"length="10"not-null="true"/>
</property>
<propertyname="operperson"type="java.lang.String">
<columnname="OPERPERSON"length="200"not-null="true"/>
</property>
<!--
<propertyname="operdate"type="java.util.Date">
<columnname="OPERDATE"length="7"/>
</property>
-->
<propertyname="operdesc"type="java.lang.String">
<columnname="OPERDESC"length="4000"not-null="true"/>
</property>
<propertyname="recordstauts"type="java.lang.String">
<columnname="RECORDSTAUTS"length="1"/>
</property>
<propertyname="field1"type="java.lang.String">
<columnname="FIELD1"length="500"/>
</property>
<propertyname="field2"type="java.lang.String">
<columnname="FIELD2"length="500"/>
</property>
</class>
</hibernate-mapping>
持久对象文件:TBOperlog.java
packagecn.isbn.db.model;
importjava.util.Date;
/**
*TBOperlogentity.
*
*@authorMyEclipsePersistenceTools
*/
publicclassTBOperlogimplementsjava.io.Serializable
{
//Fields
privateStringlogid;
privateStringopertable;
privateStringoperkey;
privateStringopertype;
privateStringoperperson;
privateDateoperdate;
privateStringoperdesc;
privateStringrecordstauts;
privateStringfield1;
privateStringfield2;
//Constructors
/**
*无参数的构造函数,只初始化记录编号和时间
*/
publicTBOperlog()
{
this.recordstauts="1";
this.operdate=newDate();
}
/**
*必填字段构造函数
*@paramopertable操作数据表
*@paramoperkey操作主键
*@paramopertype操作类型
*@paramoperperson操作人
*@paramoperdesc操作描述
*/
publicTBOperlog(Stringopertable,Stringoperkey,Stringopertype,
Stringoperperson,Stringoperdesc)
{
this.opertable=opertable;
this.operkey=operkey;
this.opertype=opertype;
this.operperson=operperson;
this.operdesc=operdesc;
this.recordstauts="1";
this.operdate=newDate();
}
/**fullconstructor*/
publicTBOperlog(Stringopertable,Stringoperkey,Stringopertype,
Stringoperperson,Stringoperdesc,
Stringfield1,Stringfield2)
{
this.opertable=opertable;
this.operkey=operkey;
this.opertype=opertype;
this.operperson=operperson;
this.operdate=newDate();
this.operdesc=operdesc;
this.recordstauts="1";
this.field1=field1;
this.field2=field2;
}
//Propertyaccessors
publicStringgetLogid()
{
returnthis.logid;
}
publicvoidsetLogid(Stringlogid)
{
this.logid=logid;
}
publicStringgetOpertable()
{
returnthis.opertable;
}
publicvoidsetOpertable(Stringopertable)
{
this.opertable=opertable;
}
publicStringgetOperkey()
{
returnthis.operkey;
}
publicvoidsetOperkey(Stringoperkey)
{
this.operkey=operkey;
}
publicStringgetOpertype()
{
returnthis.opertype;
}
publicvoidsetOpertype(Stringopertype)
{
this.opertype=opertype;
}
publicStringgetOperperson()
{
returnthis.operperson;
}
publicvoidsetOperperson(Stringoperperson)
{
this.operperson=operperson;
}
publicDategetOperdate()
{
returnthis.operdate;
}
publicvoidsetOperdate(Dateoperdate)
{
this.operdate=operdate;
}
publicStringgetOperdesc()
{
returnthis.operdesc;
}
publicvoidsetOperdesc(Stringoperdesc)
{
this.operdesc=operdesc;
}
publicStringgetRecordstauts()
{
returnthis.recordstauts;
}
publicvoidsetRecordstauts(Stringrecordstauts)
{
this.recordstauts=recordstauts;
}
publicStringgetField1()
{
returnthis.field1;
}
publicvoidsetField1(Stringfield1)
{
this.field1=field1;
}
publicStringgetField2()
{
returnthis.field2;
}
publicvoidsetField2(Stringfield2)
{
this.field2=field2;
}
}
调用对象:
publicbooleanaddLog(TBOperloglog)
{
//TODOAuto-generatedmethodstub
booleanresult=false;
Stringlogid=getLogid();//得到记录编号
if(!logid.equals(""))
{//如果取得记录编号
log.setLogid(logid);
this.save(log);
result=true;
}
returnresult;
}
补充:
下面是从网上找的一个解决方法,对我的处理没帮助,也许日后对对其他的处理用的着,也贴出来,作为备忘储备吧。
来自:http://www.host01.com/article/jsp/00040007/20060805215458644.htm
Db2表:Test只有一个测试字段:namecharacter(10)
Hibernate的hbm文件:Test.hbm.xml
<?xmlversion="1.0"?>
<!DOCTYPEhibernate-mappingSYSTEM"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd"
>
<hibernate-mapping>
<classname="Test"table="test">
<composite-idname="testpk"class="Testpk"unsaved-value="any">
<key-propertyname="name"column="name"type="string"/>
</composite-id>
</class>
</hibernate-mapping>
Test.java
importjava.io.Serializable;
publicclassTestimplementsSerializable{
privateTestpktestpk;
publicvoidsetTestpk(Testpkvalue){
this.testpk=value;
}
publicTestpkgetTestpk(){
returnthis.testpk;
}
}
Testpk.java
importjava.io.Serializable;
publicclassTestpkimplementsSerializable{
privateStringname;
publicStringgetName()
{
returnthis.name;
}
publicvoidsetName(Stringvalue){
this.name=value;
}
publicbooleanequals(Objectother){
Testpkthat=(Testpk)other;
returnthis.name.equals(that.name);
}
/**
*Returnsthehashcodeforthekey.
*/
publicinthashCode(){
return(this.name.hashCode());
}
}
测试代码:
Testt=newTest();
Testpktpk=newTestpk();
tpk.setName("test000000");
t.setTestpk(tpk);
session.save(t);
最后值得注意的是当要load的时候,不能简单的Testt=(Test)session.load(Test.class,"test000000");而
使用一个Testpk作为一个实体类的标识符。
所以应该这么写:
Testpktpk=newTestpk();
tpk.setName("test000000");
Testt=(Test)session.load(Test.class,tpk);
环境DB2、Eclipise、Hibernate2测试成功
本文出自“在路上”博客,请务必保留此出处http://yuwenhu.blog.51cto.com/672091/160930