0

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.

  • It's a bad idea to have types that don't match, but see https://stackoverflow.com/questions/38719133/whats-the-equivalent-of-convert-in-hibernate-hbm-file – tgdavies Jan 12 '23 at 21:11

1 Answers1

0

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.

schtever
  • 3,210
  • 16
  • 25