Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA configure boolean fields to persist as integers

Tags:

java

jpa

In JPA is there an annotation to specify that boolean fields should be persisted as an integer. I'm using OpenJPA and it's currently persisting boolean fields as bits. I'd rather use integer 1 or 0.

like image 607
Joel Avatar asked Oct 21 '10 13:10

Joel


People also ask

What does @column do in JPA?

@Column annotation is used for Adding the column the name in the table of a particular MySQL database. Attribute: Name: The name of the column. length: The column length.

How do I set the default value of Boolean in Java Spring boot?

you can use assigning to change default value. create setter method and set true value. private boolean include = true; will work on Java. Still, you have to make sure your database has direct support for boolean fields or if you have to use a converter.

Is @column mandatory in JPA?

Let's start with the @Column annotation. It is an optional annotation that enables you to customize the mapping between the entity attribute and the database column.

What is the easiest way to ignore a JPA field during persistence?

use @Transient to make JPA ignoring the field.


2 Answers

You can specify the column definition:

@Column(name="boolColumn",
     columnDefinition="INT(1)")
like image 171
Bozho Avatar answered Oct 22 '22 10:10

Bozho


You can use the following annotation:

@Type(type="numeric_boolean")

If you want to write Y and N instead of 0, 1, you can use

@Type(type="yes_no")
like image 22
grep Avatar answered Oct 22 '22 10:10

grep