Hibernate Custom Basic Type with Database Function
Asked Answered
G

0

6

I'm making a custom Hibernate type using AbstractSingleColumnStandardBasicType to automatically convert the SQL UDT into a Java type. The SQL uses database functions to construct and serialize the column data from BLOBs and/or CLOBs, and I want to hide that from a user. So they don't have to use @ColumnTransformer(read="_serializer(column_name)", write="_constructor(?)") for every column.

I've made types using the BLOB SqlTypeDescriptor and a custom JavaTypeDescriptor to do the conversion at the JDBC level, but how can I have my custom type wrap the column and parameters in the proper SQL functions? I'm not finding anything describing anything like this.

Edit: To make this more concrete, this UDT (SDE ST_GEOMETRY) must be build via database functions (ST_LineFromText) or the defined UDT constructor (ST_LineString). Likewise, to read them from the database, you have to "export" them into a certain format (ST_AsText) for Java to read it. The regular approach of reading the fields of the type doesn't work in this case.

So:

create table geo_lines (
  line SDE.ST_GEOMETRY
);

And:

@Entity
@Table(name="geo_lines")
public class GeoLine {
  @Column(name="line")
  @Type(type="org.example.MyStLineType")
  // i'm wanting to be able to omit the following annotation
  @ColumnTransformer(read="SDE.ST_AsText(line)", 
         write="SDE.ST_ST_LineFromText(?, 3857)")
  private Polyline line;
}

So, all my type really needs from the user is the number (3857 in this case), which can be passed as a parameter, and everything else is standard boilerplate for any line. Repeat for the other types like points, polygons, etc.

Gaddis answered 24/5, 2019 at 21:37 Comment(4)
What do you mean by saying wrap the column and parameters in the proper SQL functions?Samantha
Generally you can annotate the entity class getter with @org.hibernate.annotations.Type(type = "your.custom.DataType")Samantha
Indeed. I haven't given you enough information. See edit.Gaddis
Seems like this should work by what you've described, unless you want the 3857 to be a parameter. The @ColumnTransformer(write) must only have one question mark. It is checked by the regular expression in org.hibernate.cfg.Ejb3Column#initMappingColumn function.Chide

© 2022 - 2024 — McMap. All rights reserved.