Hibernate UserType to map @Formula result to non-standard user object

Using Hibernate 3.5.3:

There is a function in the database (which uses Oracle-related table functions) that returns the number of rows and values โ€‹โ€‹when the object identifier is supplied. (This function is part of legacy code that cannot be changed.)

Example result:

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 @Formula (containing the above SQL) to an object object.

  public class DateOptions { private LocalDate startDate; private LocalDate endDate; private LocalDate otherDate; // getters and setters omitted } 

I want to display it like in the object:

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

I tried to create a Hibernate UserType with the hope of creating a DateOptions object using the ResultSet in nullSafeGet(...) .

I am specifying sql types in my DateOptionsUserType

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

However, when I start, I get the following exception: org.hibernate.MappingException: property mapping has wrong number of columns: mypackage.MyEnity.dateOptions type: mypackage.DateOptionsUserType (I also tried CompositeUserType with the same result).

Any idea what might cause the problem? Is this possible (matching @Formula with a custom non-entity object)?

+4
source share
1 answer

since you basically have 3 fields with formula i, I see that executing a 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; 
0
source

Source: https://habr.com/ru/post/1433388/


All Articles