简体   繁体   中英

Hibernate UserType to map @Formula result to non-entity custom object

Using 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. (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.

 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(...) .

I specify the sql types in my DateOptionsUserType

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).

Any idea what might be causing the issue? Is it even possible (mapping a @Formula to custom non-entity object)?

since you basicly have 3 fields with a formula i would see if executing the function 3 times is still fast enough

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM