关于hibernate的一个问题

04-11-04 xpf702b

有两个类:XActivityModel和XActivityModelRun,后者从前者继承,两者结构完全相同,只是保存到数据库不同的表中,表的结构也完全相同,在构造函数中使用模板方法,对对象进行初始化,使用Hibernate的createQuery方法,数据表名用重载父类的方法实现,XActivityModelRun工作正常,但是XActivityModel在初始化时生成了两条sql语句,它自己类的和其继承类的,导致初始化失败,下面是部分源代码和控制台的输出信息:

XActivityModel 类的源码:

public class XActivityModel extends XStepModel {

/**

* 构造函数

*

* @param objectID

* 对象编号

*/

public XActivityModel(String objectID) { //进行初始化的构造函数

super(objectID);

}

...

/**

* 初始化模型内容,调用父类的getModelContent()函数返回对象内容,然后对模型进行初始化

*

* @see org.xliu.xflowbuilder.model.XModel#getModelContent(java.lang.String,

* int)

*/

public void initModel() {

try {

Session session = XResource.SESSION_FACTORY.openSession();

//这就是生成query的代码,但是这个类总是产生两个sql语句,而其继承类能产生正确的语句

Query q = session

.createQuery("select t.name, t.description, t.memo, t.activityType, t.createTime, t.creatorID, t.group, t.processID, t.firstStep, t.programID, t.runMode, t.startCondition from "+getClassName()+" as t where t.objID = '"

+ getObjID() + "'");

List l = q.list();

...

} catch (Exception e) {

if (XResource.isDEBUG()) {

e.printStackTrace();

} //if

} //catch

}

...

/**

* @return 提取信息的类名称(构建时)

*/

protected String getClassName() { //提供不同的表名(其实是类名)

return "XActivityModel";

}

XActivityModelRun 类的源码:

public class XActivityModelRun extends XActivityModel {

/**

* @param objectID

*/

public XActivityModelRun(String objectID) { //进行初始化的构造函数

super(objectID);

}

/**

* @return 提取信息的类名称(运行时)

*/

protected String getClassName() { //重载父类的方法,提供不同的表名(其实是类名)

return "XActivityModelRun";

}

public void initModel() { //这个方法加不加都一样

super.initModel();

}

...

}

控制台的输出:

----------------begin

log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).

log4j:WARN Please initialize the log4j system properly.

Hibernate: select xactivitym0_.FD_ACTIVITYNAME as col_0_0_, xactivitym0_.FD_ACTIVITYDESCRIPTION as col_1_0_, xactivitym0_.FD_ACTIVITYREMARK as col_2_0_, xactivitym0_.FD_ACTIVITYTYPE as col_3_0_, xactivitym0_.FD_CREATETIME as col_4_0_, xactivitym0_.FD_CREATORID as col_5_0_, xactivitym0_.FD_GROUP as col_6_0_, xactivitym0_.FD_PROCESSID as col_7_0_, xactivitym0_.FD_ISFISRTACTIVITY as col_8_0_, xactivitym0_.FD_PROGRAMID as col_9_0_, xactivitym0_.FD_RUNMODE as col_10_0_, xactivitym0_.FD_STARTCONDITION as col_11_0_ from TB_WF_ACTIVITYINFO xactivitym0_ where (xactivitym0_.FD_ACTIVITYID='ACTI0000050008' )//这句是正常的

Hibernate: select xactivitym0_.FD_ACTIVITYNAME as col_0_0_, xactivitym0_.FD_ACTIVITYDESCRIPTION as col_1_0_, xactivitym0_.FD_ACTIVITYREMARK as col_2_0_, xactivitym0_.FD_ACTIVITYTYPE as col_3_0_, xactivitym0_.FD_CREATETIME as col_4_0_, xactivitym0_.FD_CREATORID as col_5_0_, xactivitym0_.FD_GROUP as col_6_0_, xactivitym0_.FD_PROCESSID as col_7_0_, xactivitym0_.FD_ISFISRTACTIVITY as col_8_0_, xactivitym0_.FD_PROGRAMID as col_9_0_, xactivitym0_.FD_RUNMODE as col_10_0_, xactivitym0_.FD_STARTCONDITION as col_11_0_ from TB_WF_ACTIVITYINFO_RUN xactivitym0_ where (xactivitym0_.FD_ACTIVITYID='ACTI0000050008' )//这句不应该有

null//初始化失败,没有取出数据

Hibernate: update TB_WF_ACTIVITYINFO set FD_OBJECTID=?, FD_PROCESSID=?, FD_ACTIVITYNAME=?, FD_ACTIVITYDESCRIPTION=?, FD_CREATORID=?, FD_CREATETIME=?, FD_ISFISRTACTIVITY=?, FD_ACTIVITYTYPE=?, FD_ACTIVITYREMARK=?, FD_GROUP=?, FD_RUNMODE=?, FD_STARTCONDITION=?, FD_PROGRAMID=? where FD_ACTIVITYID=?//更新的时候可以成功,因为表中存在这条记录

----------------end

请高手指点一下,怎么解决这个问题,是因为继承的原因,还是hibernate用的不对,向这种情况还用其他好的办法解决吗?

banq
2004-11-09 10:54

如果只是保存到不同表,表结构是一样的,只要配置时改一下Hibernate映射配置的数据库名即可,不建议做成两个持久类,然后还要继承