我使用openshift来托管JBoss Web应用程序。 然而,他们最近更新到EAP 6.3,它打破了Hibernate持久层。
使用实体管理器完成标准更新后,我得到一个例外:
每个派生表都必须有自己的别名
我理解这个错误是由Hibernate生成无效的MySQL查询语法引起的。 但你怎么能绕过这个呢?
代码:
private Object updateObject(Object object) {
try {
return em.merge(object);
} catch(ConstraintViolationException ex) {
log.error("Error updating object");
throw ex;
}
}
激活SQL查询我发现代码执行以下更新:
update expence_form
set create_dat=?, last_update_dat=?, approvedby_id=?, approved_dat=?, comment=?, expence_submitter_id=?, ExternalRef=?, owner_id=?, paidby_id=?, paid_dat=?, season_id=?, status=?, submit_dat=?, submitter_id=?, expenceform_type=?, year=?
where id=?
=>行
select count(id)
from (
select id, create_dat, last_update_dat, expence_amt, expence_date, km_distance, km_expence_amt, route_desc, expence_form_id, comment, event_id, null as description, null as budget_group_id, null as expence_type_id, 1 as clazz_
from fixed_expence_detail
union
select id, create_dat, last_update_dat, expence_amt, expence_date, km_distance, km_expence_amt, route_desc, expence_form_id, null as comment, null as event_id, description, budget_group_id, expence_type_id, 2 as clazz_
from proven_expence_detail
)
where expence_form_id =?
=> NOK作为子查询没有别名。
我找到了以下错误报告,但没有说明如何解决问题。
https://hibernate.atlassian.net/browse/HHH-9139
目前我完全陷入困境,因为openshift迫使我与MySQL一起使用有缺陷的EAP6.3版本。 在JBoss将其修复为openshift之前,我怎么能绕过这个问题呢?
答案 0 :(得分:0)
问题似乎与使用TABLE_PER_CLASS的继承结构相关联: 出于某种原因,hibernate对合并命令的继承结构中的每个表执行计数查询。
我的对象结构如下:(包含ExpenceFormDetails列表的expenceForm对象)
ExpenceForm< - ExpenceFormDetails
ExpenceFormDetails(抽象类)
代码:
@Entity
@Table(name="expence_form")
public class ExpenceForm extends BaseEntityIdentityIdImpl
{
//some properties
@OneToMany(mappedBy="expenceForm", fetch=FetchType.LAZY)
@OrderBy("expenceDate")
public List<ExpenceFormDetails> getExpenceFormDetails() {
return expenceFormDetails;
}
}
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public abstract class ExpenceFormDetails extends BaseEntitySequenceIdImpl
implements Comparable<ExpenceFormDetails>
{
// some properies
}
@Entity
@Table(name="fixed_expence_detail")
public class FixedExpenceFormDetail extends ExpenceFormDetails
{
//Extra properties
}
由于我不知道如何影响hibernate查询,我只是通过为对象编写手动更新命令来解决问题:
Query updateQuery = em.createQuery("update ExpenceForm set submitter= :submitter, "
+ "owner= :owner, approvedBy=:approvedBy, paidBy= :paidBy, season= :season, "
+ "expenceSubmitter= :expenceSubmitter, submitDate= :submitDate, approvedDate= :approvedDate, paidDate= :paidDate, externalRef= :externalRef, "
+ "status=:status, type=:type, comment=:comment, year=:year "
+ "where id= :id")
.setParameter("submitter", f.getSubmitter())
.setParameter("owner", f.getOwner()).setParameter("approvedBy", f.getApprovedBy()).setParameter("paidBy", f.getPaidBy())
.setParameter("season", f.getSeason()).setParameter("expenceSubmitter", f.getExpenceSubmitter()).setParameter("submitDate", f.getSubmitDate())
.setParameter("approvedDate", f.getApprovedDate()).setParameter("paidDate", f.getPaidDate()).setParameter("externalRef", f.getExternalRef())
.setParameter("status", f.getStatus()).setParameter("type", f.getType()).setParameter("comment", f.getComment())
.setParameter("year", f.getYear()).setParameter("id", f.getId());
updateQuery.executeUpdate();
然而,我仍然认为这是一个&#39; hack&#39;没有解决真正的问题。 如果有jira访问hibernate项目的人读到这个,我认为打开一个专门的缺陷是合适的。