ScArcher2
ScArcher2

Reputation: 87167

How do I map a CHAR(1) to a boolean using Hibernate for Java?

How do I map a CHAR(1) to a boolean using Hibernate for Java?

Upvotes: 8

Views: 10783

Answers (4)

Andrei Manolache
Andrei Manolache

Reputation: 925

For me the following did the thing:

@Type(type = "org.hibernate.type.NumericBooleanType")
@Column(name = "STATUS", columnDefinition = "CHAR(1)", length = 1)
private Boolean status;

Column definition:

STATUS NOT NULL CHAR(1)

Technologies:

  • Spring framework 4.0.6.RELEASE
  • Hibernate 4.3.9.Final

Upvotes: 0

pabloso18
pabloso18

Reputation: 43

You can map like this:

@Type(type = "org.hibernate.type.TrueFalseType")
@Column(name = "IsValid", columnDefinition = "CHAR(1)", length = 1)
private Boolean valid;

Upvotes: 0

Simon Groenewolt
Simon Groenewolt

Reputation: 10665

CharBooleanType is probably what you are looking for http://www.hibernate.org/hib_docs/v3/api/org/hibernate/type/class-use/CharBooleanType.html

edit: dtsazza's answer is probably more useful if you just want to get going and use characters y/n or t/f. If those don't match your usage you can implement your own type using CharBooleanType.

Upvotes: 2

Andrzej Doyle
Andrzej Doyle

Reputation: 103777

The true_false or yes_no types will do this for you.

Upvotes: 4

Related Questions