开发者

JPA configure boolean fields to persist as integers

开发者 https://www.devze.com 2023-01-20 14:41 出处:网络
In JPA is there an annotation to specify that boolean fields should be persisted as an integer. I\'m using OpenJPA and it\'s开发者_StackOverflow currently persisting boolean fields as bits. I\'d rathe

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


You can specify the column definition:

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


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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消