gpt4 book ai didi

java - EAP 6.3 基本 hibernate 更新抛出异常

转载 作者:行者123 更新时间:2023-11-30 22:57:52 26 4
gpt4 key购买 nike

我正在使用 openshift 托管 JBoss 网络应用程序。然而,他们最近更新到 EAP 6.3,这打破了 Hibernate 持久层。

当使用实体管理器完成标准更新时,我得到一个异常:

Every derived table must have its own alias

我知道这个错误是由 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 迫使我将有问题的 EAP6.3 版本与 MySQL 一起使用。在 JBoss 在 openshift 中修复它之前,我该如何规避这个问题?

最佳答案

问题似乎与使用 TABLE_PER_CLASS 的继承结构有关:由于某种原因,hibernate 在合并命令上对继承结构中的每个表执行计数查询。

我的对象结构如下:(包含 ExpenceFormDetails 列表的 expenceForm 对象)

ExpenceForm <- ExpenceFormDetails

ExpenceFormDetails(抽象类)

  • FixedExpenceFormDetail
  • ProvenExpenceFormDetail

代码:

@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();

但是我仍然认为这是一个“hack”,并没有解决真正的问题。如果对 hibernate 项目具有 jira 访问权限的人读到这篇文章,我认为打开一个专门的缺陷是合适的。

关于java - EAP 6.3 基本 hibernate 更新抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25470445/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com