user8221504
user8221504

Reputation:

How to store Boolean type data into a column in a PostgreSQL db table with data type "bit" using HIbernate?

I have a variable of type Boolean in my entity class by name "isActive". It to mapped to a column by name "is_active" with data type as bit.

@Column(name = "is_active")
private boolean isActive;

But when ever I try to save isActive attribute of the object, I get an error:

column "is_active" is of type bit but expression is of type character 
varying Hint: You will need to rewrite or cast the expression.

How do I store the values the values of isActive? I want to store "1" in the database when value of "isActive" is true and "0" when "isActive" is false.

Thank you!

Upvotes: 0

Views: 5962

Answers (2)

dev_2014
dev_2014

Reputation: 371

Add below annotation above the field name, if using hibernate

@Type(type = "org.hibernate.type.NumericBooleanType")
@Column(name = "is_active")
private boolean isActive;

Upvotes: 0

dimitrisli
dimitrisli

Reputation: 21391

You can use Hibernate's NumericBooleanType like so:

@Type(type = "numeric_boolean")
@Column(name = "is_active")
private boolean isActive;

Upvotes: 1

Related Questions