【问题标题】:Hibernate UserType to map @Formula result to non-entity custom objectHibernate UserType 将@Formula 结果映射到非实体自定义对象
【发布时间】:2023-03-26 21:41:01
【问题描述】:

使用休眠 3.5.3:

在数据库中有一个函数(使用 Oracle 的流水线表函数),它在提供实体 ID 时返回许多行和值。 (此函数是遗留代码的一部分,无法更改)。

结果示例:

select * from table(DateOptions_Function('ID_OF_ENTITY'));

OPTION       DATE       
-----------------------
startDate    2012/09/01    
endDate      2013/04/01
otherDate    2011/01/01 

我想将@Formula(包含上述SQL)的结果映射到实体上的一个对象。

 public class DateOptions {
     private LocalDate startDate;
     private LocalDate endDate;
     private LocalDate otherDate;

     // getters and setters omitted
 }

我想在实体中像这样映射它:

@Formula("(select * from table(DateOptions_Function(ENTITY_ID)))")
@Type(type = "mypackage.DateOptionsUserType")
public DateOptions getDateOptions() {
    return dateOptions;
}

我尝试创建一个 Hibernate UserType,希望使用 nullSafeGet(...) 中的 ResultSet 创建一个 DateOptions 对象。

我在 DateOptionsUserType 中指定了 sql 类型

public int[] sqlTypes() {
    return new int[] {Types.VARCHAR, Types.DATE}; 
}

我在启动时不断收到以下异常: org.hibernate.MappingException: property mapping has wrong number of columns: mypackage.MyEnity.dateOptions type: mypackage.DateOptionsUserType(我也试过CompositeUserType,结果相同)。

知道可能导致问题的原因吗?是否有可能(将@Formula 映射到自定义非实体对象)?

【问题讨论】:

    标签: java oracle hibernate hibernate-mapping


    【解决方案1】:

    因为你基本上有 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;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-09-22
      • 2019-01-20
      • 1970-01-01
      • 2014-02-11
      • 2020-08-17
      • 2012-08-04
      • 2014-09-15
      • 2019-08-11
      相关资源
      最近更新 更多