Joel
Joel

Reputation: 30166

JPA configure boolean fields to persist as integers

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.

Upvotes: 12

Views: 19836

Answers (4)

Marcus
Marcus

Reputation: 725

Three more years later, in Hibernate 6, use

@Convert(converter = NumericBooleanConverter.class)
public Boolean getEnabled() {
    return this.enabled;
}

Upvotes: 2

Sticking with the question, a long time later, but very related but for Hibernate 4.3.7 higher, you can use

import org.hibernate.annotations.Type;
@Column(name="enabled1", nullable=false)
@Type(type = "org.hibernate.type.NumericBooleanType")  
public Boolean getEnabled() {
    return this.enabled;
}

As indicated in enter link description here

This has been tested in a migration from PostgreSQL to Oracle to avoid impact on JPA entities

Upvotes: 0

grep
grep

Reputation: 5623

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")

Upvotes: 8

Bozho
Bozho

Reputation: 597422

You can specify the column definition:

@Column(name="boolColumn",
     columnDefinition="INT(1)")

Upvotes: 17

Related Questions