Skip to content
Advertisement

How to map column with type BIT(24) in PostgreSQL with Hibernate

I have a table with a column which type is a bit (24). I tried to use String and when I try to get this object it maps well, but when I try to save it, Hibernate throws Exception: org.postgresql.util.PSQLException: ERROR: column is of type bit but expression is of type character varying

I know that if it’s a bit(1) I can use Boolean, but I don’t know what should I do in my case.

Thanks in advance!

Advertisement

Answer

Unfortunately, JPA/hibernate does not support BIT having a parametrized length.

Fortunately, you can define your own custom type with hibernate.

First, you need to create BitStringType, BitStringJavaDescriptor, and BitStringSqlDescriptor:

JavaScript
JavaScript
JavaScript

Having those classes, you can define a type for your field. Please, use the correct package (in my case I’ve used the one from my demo com.yonlabs.jpa):

JavaScript

You can also register this type with hibernate to use a registered name instead of a fully qualified Java class.

Advertisement