[英]Hibernate UserType to map @Formula result to non-entity custom object
Using Hibernate 3.5.3: 使用Hibernate 3.5.3:
In the database is a function (that uses Oracle's pipelined table functions) which returns a number of rows and values when supplying the ID of the entity. 在数据库中是一个函数(使用Oracle的流水线表函数),它在提供实体的ID时返回许多行和值。 (This function is part of legacy code which can't be changed).
(此功能是遗留代码的一部分,无法更改)。
Result example: 结果示例:
select * from table(DateOptions_Function('ID_OF_ENTITY'));
OPTION DATE
-----------------------
startDate 2012/09/01
endDate 2013/04/01
otherDate 2011/01/01
I want to map the result of a @Formula
(containing the above SQL) to an object on the entity. 我想将
@Formula
(包含上面的SQL)的结果映射到实体上的对象。
public class DateOptions {
private LocalDate startDate;
private LocalDate endDate;
private LocalDate otherDate;
// getters and setters omitted
}
I want to map it like so in the entity: 我想在实体中映射它:
@Formula("(select * from table(DateOptions_Function(ENTITY_ID)))")
@Type(type = "mypackage.DateOptionsUserType")
public DateOptions getDateOptions() {
return dateOptions;
}
I have tried creating a Hibernate UserType
with the hope of creating a DateOptions
object using the ResultSet
in nullSafeGet(...)
. 我尝试创建一个Hibernate
UserType
,希望使用nullSafeGet(...)
的ResultSet
创建一个DateOptions
对象。
I specify the sql types in my DateOptionsUserType
我在
DateOptionsUserType
指定了sql类型
public int[] sqlTypes() {
return new int[] {Types.VARCHAR, Types.DATE};
}
I keep getting the following exception on startup though: org.hibernate.MappingException: property mapping has wrong number of columns: mypackage.MyEnity.dateOptions type: mypackage.DateOptionsUserType
(I have also tried CompositeUserType
with the same result). 我在启动时不断收到以下异常:
org.hibernate.MappingException: property mapping has wrong number of columns: mypackage.MyEnity.dateOptions type: mypackage.DateOptionsUserType
(我也尝试过使用相同结果的CompositeUserType
)。
Any idea what might be causing the issue? 知道可能导致问题的原因吗? Is it even possible (mapping a
@Formula
to custom non-entity object)? 甚至可能(将
@Formula
映射到自定义非实体对象)?
since you basicly have 3 fields with a formula i would see if executing the function 3 times is still fast enough 因为你基本上有3个带有公式的字段,我会看到执行3次函数的速度是否足够快
@Formula("(select DATE from table(DateOptions_Function(ENTITY_ID))) WHERE option='startDate'")
private LocalDate startDate;
@Formula("(select DATE from table(DateOptions_Function(ENTITY_ID))) WHERE option='endDate'")
private LocalDate endDate;
@Formula("(select DATE from table(DateOptions_Function(ENTITY_ID))) WHERE option='otherDate'")
private LocalDate otherDate;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.