简体   繁体   English

Hibernate UserType将@Formula结果映射到非实体自定义对象

[英]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.

相关问题 Hibernate - createNativeQuery带有“非实体类”结果 - Hibernate - createNativeQuery with “non-entity class” result Hibernate / JPA将本机查询的结果映射到非实体持有实体 - Hibernate/JPA map Result of native Query to non-Entity holding Entities 如何使用Hibernate将SQL查询的结果最佳映射到非实体Java对象? - How to best map results from an SQL query to a non-entity Java object using Hibernate? 使用Spring JPA将SQL查询结果映射到Java对象(在非实体类中) - Map sql query result to java object(in Non-Entity class) using spring jpa 使用基于非实体对象的联接 - Using join based on a non-entity object Spring 数据 JPA map 存储过程结果从多个数据源到非实体 POJO - Spring Data JPA map the stored procedure result to Non-Entity POJO from multiple data source 从 3.6 更新到 Hibernate 5.1 会产生传递给驱逐异常的非实体 object 实例 - Updating to Hibernate 5.1 from 3.6 produce Non-entity object instance passed to evict exception Hibernate:来自非实体类的实体属性 - Hibernate: Entity property from non-entity class 如何在Hibernate中从非实体子类中持久化实体 - How to persist an entity from an non-entity subclass in Hibernate 是否可以调用本机查询并将结果集存储在非实体对象中? - Is it possible to call a native query and store the result set in a non-entity object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM