Home > database >  How to map a Java string to a PostgreSQL smallint column in hibernate XML?
How to map a Java string to a PostgreSQL smallint column in hibernate XML?

Time:01-19

I currently have a Java object that I am using HBM XML file to map to. The Java datatype of the property "year" is String, while the postgres column it corresponds to is of type smallint. I would like to map it to each other in my HBM XML, but am facing issues.

<property name="year" type="string">
   <column name="year" sql-type="short" />
</property>

This is what I was expecting to work, but I am getting an error when persisting a record into this table that says:

SqlExceptionHelper ERROR: column "year" is of type smallint but expression is of type character varying

I have also tried putting "smallint" in sql-type, but with same error.

CodePudding user response:

Untested, but from https://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html/ch05.html#mapping-column-read-and-write

<property name="year">
    <column 
      name="year"
      read="cast(year as varchar(8))"
      write="cast(? as smallint)"
    />
</property>

I picked 8 for the varchar size because it's a smallint.

  • Related