# Issue with BooleanConverter in Postgres with smallint

**URL:** <https://discourse.hibernate.org/t/issue-with-booleanconverter-in-postgres-with-smallint/8564>\
**Category:** Hibernate ORM\
**Created:** [November 17, 2023, 9:33am UTC](https://discourse.hibernate.org/t/issue-with-booleanconverter-in-postgres-with-smallint/8564 "2023-11-17T09:33:03Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![boutss](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/boutss/32/1543_2.png) [@boutss](https://discourse.hibernate.org/u/boutss)\
**Post date:** [November 17, 2023, 9:33am UTC](https://discourse.hibernate.org/t/issue-with-booleanconverter-in-postgres-with-smallint/8564/1 "2023-11-17T09:33:03Z")

</div>

Hello,

The issue appeared with `HHH-16125 remove DDL generation stuff from converters`.

Because our Boolean Converter is typed as boolean.

 ![image](https://canada1.discourse-cdn.com/flex035/uploads/hibernate/original/2X/9/9c15b582346a8af5cdd4bd7f80713b61a4d9f362.png)

But for Postgres, it is actually stored as a smallint, similar to Oracle, which doesn’t have a boolean type but uses a number. Currently, we are coexisting with both database management systems as we are in the process of migration. It might be a future project to switch boolean types to the boolean type in the PG database, but it’s not the case yet.

So, I had to adapt the JDBC type for booleans specifically for PG by treating them as SmallIntJdbcType.

 ![image](https://canada1.discourse-cdn.com/flex035/uploads/hibernate/original/2X/8/80cc5f2a71dd646574a152ea75c3e6c68f601341.png)

The issue is that the new method BooleanJavaType#getCheckCondition assumes that the BooleanConverter returns an integer and attempts to invoke longValue() on it, which raises an exception.

 ![image](https://canada1.discourse-cdn.com/flex035/uploads/hibernate/original/2X/f/f4f80a544e1ae0f130c65a2268fb3df396c13cd6.png)

The solution I’ve found for now is to override it, specifying that the Converter indeed returns a Boolean type.

 ![image](https://canada1.discourse-cdn.com/flex035/uploads/hibernate/original/2X/a/abc2d3cc10324b66e21c09e3e46db53991661a98.png)

And to pass this custom type to the TypeIntegrator.

 ![image](https://canada1.discourse-cdn.com/flex035/uploads/hibernate/original/2X/e/e444036d12f81f7f15dfbc459d85e4fb5908cff5.png)

I tried converting the BooleanConverter to \<Boolean, Byte\> or \<Boolean, Short\>, but this is incorrect for Oracle and causes inverse problems. Also, there is no possibility to use one Converter for Oracle and another for Postgres, at least I haven’t found a way.

What do you think?

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [November 17, 2023, 9:46am UTC](https://discourse.hibernate.org/t/issue-with-booleanconverter-in-postgres-with-smallint/8564/2 "2023-11-17T09:46:11Z")

</div>

Why don’t you just set the `hibernate.type.preferred_boolean_jdbc_type` property to `smallint`?

---

<div class="post-metadata">

**Author:** ![boutss](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/boutss/32/1543_2.png) [@boutss](https://discourse.hibernate.org/u/boutss)\
**Post date:** [November 17, 2023, 10:42am UTC](https://discourse.hibernate.org/t/issue-with-booleanconverter-in-postgres-with-smallint/8564/3 "2023-11-17T10:42:28Z")

</div>

Indeed, it’s better than my override of the Dialect. I had already used it, but I had set it to BIT.

Unfortunately, it doesn’t solve the issue of the converter being treated as an integer with the SmallIntJdbcType =\> BooleanJavaType#getCheckCondition

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [November 17, 2023, 1:59pm UTC](https://discourse.hibernate.org/t/issue-with-booleanconverter-in-postgres-with-smallint/8564/4 "2023-11-17T13:59:12Z")

</div>

Why do you need the converter? Just remove it, no?

---

<div class="post-metadata">

**Author:** ![boutss](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/boutss/32/1543_2.png) [@boutss](https://discourse.hibernate.org/u/boutss)\
**Post date:** [November 17, 2023, 4:43pm UTC](https://discourse.hibernate.org/t/issue-with-booleanconverter-in-postgres-with-smallint/8564/5 "2023-11-17T16:43:02Z")

</div>

It is only used to handle nullity.

I will check with the person who implemented it about the specific case they encountered, and I will try to remove it.  
Indeed, that would solve our problem definitively. ^^  
I assume they had a good reason, but let’s see if we can resolve it differently.

---

<div class="post-metadata">

**Author:** ![boutss](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/boutss/32/1543_2.png) [@boutss](https://discourse.hibernate.org/u/boutss)\
**Post date:** [November 24, 2023, 9:57am UTC](https://discourse.hibernate.org/t/issue-with-booleanconverter-in-postgres-with-smallint/8564/6 "2023-11-24T09:57:54Z")

</div>

In our framework, boolean management is nullsafe, so apparently, this is not the case for Hibernate (see stack trace). This is to handle cases of creation through scripts, for example, where values might be null instead of false.

I have searched for another solution to make it nullsafe without using a converter, but I haven’t found anything simple.

 ![image](https://canada1.discourse-cdn.com/flex035/uploads/hibernate/original/2X/6/60540b06ce7ffd379d804eaf4560af4f6b8b2c60.png)

---

<div class="post-metadata">

**Author:** ![beikov](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/beikov/32/258_2.png) [@beikov](https://discourse.hibernate.org/u/beikov)\
**Post date:** [November 24, 2023, 10:18am UTC](https://discourse.hibernate.org/t/issue-with-booleanconverter-in-postgres-with-smallint/8564/7 "2023-11-24T10:18:56Z")

</div>

You mean that you use a primitive `boolean` in your entity model and want `null` from the database to be interpreted as `false`? That sounds very weird to me. Why not use `Boolean` then? Or make the column in the database `not null` and set existing `null` columns to `false`?  
If you really must, you can register a custom `BooleanJavaType` that implements `wrap` in a way that never passes through `null` but rather defaults to `false`.  
I would strongly recommend you to fix your data model though as querying might just get more complicated with nulls involved.

---

<div class="post-metadata">

**Author:** ![boutss](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.hibernate.org/boutss/32/1543_2.png) [@boutss](https://discourse.hibernate.org/u/boutss)\
**Post date:** [November 24, 2023, 10:40am UTC](https://discourse.hibernate.org/t/issue-with-booleanconverter-in-postgres-with-smallint/8564/8 "2023-11-24T10:40:49Z")

</div>

Yes, it’s a primitive boolean in the data model, but we don’t want to handle nullability. It’s just like how we use `smallint` in PostgreSQL and `number` in Oracle; we encounter cases of nullability.

So, we use this converter to translate nulls in the database to false in the data model, to overcome this issue. In the medium term, we will transition to a boolean type in the PostgreSQL database, and we won’t have this problem anymore.

![image](https://canada1.discourse-cdn.com/flex035/uploads/hibernate/original/2X/7/7b03ac1a7a78c7524b77337ddb2c73bfafd2dba9.png)

 ![image](https://canada1.discourse-cdn.com/flex035/uploads/hibernate/original/2X/4/442b43b32b2a9d2aab5f2f51fffa06ec70336c93.png)

![image](https://canada1.discourse-cdn.com/flex035/uploads/hibernate/original/2X/7/7847d179e89a0a7e676ee9833affdcccfb3e9d63.png)

 ![image](https://canada1.discourse-cdn.com/flex035/uploads/hibernate/original/2X/9/9c4bb11d697afeb416be9e17f23a76254f0fba10.png)
