File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Object Relational Mapping and the fly likes How JPA persist a boolean Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Object Relational Mapping
Reply Bookmark "How JPA persist a boolean" Watch "How JPA persist a boolean" New topic
Author

How JPA persist a boolean

Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
My question is a quite simple mapping question. I have an oracle database and i'm using toplink as jpa implementation. I want to persist a pojo that contains a boolean. In fact the toplink dialect will create NUMBER column with two possible values 1 or 0.
But is there a standard way to force a different representation like the @Temporal(TemporalType.DATE) or the @Enumerated(EnumType.STRING).

Thanks in advance
Edvins Reisons
Ranch Hand

Joined: Dec 11, 2006
Posts: 364
The boolean type, like the other Java primitive types, is mapped with the optional @Basic annotation.
[ May 11, 2007: Message edited by: Edvins Reisons ]
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
Hello that's not exactly what i try to ask:
Imagine you have an existing database where a column of a table represent a Boolean value with Varchar2(1). Which can be 'Y' or 'N'.

Is there a simple way to create an jpa mapping that will map this column to a boolean?
Thanks in advance
Edvins Reisons
Ranch Hand

Joined: Dec 11, 2006
Posts: 364
My understanding is that this customization can be done at the persistence provider level (I've seen similar customization options in Hibernate), rather than in JPA.
Oliver Kamps
Greenhorn

Joined: May 15, 2007
Posts: 18
Hi,

the JPA specification itself does not specify how a boolean field is mapped to / represented in the database. You will have to look at your JPA implementation's documentation for vendor-specific extensions.

Having had the, err, opportunity to use TopLink (pre-JPA) in a previous life, I'd be surprised if TopLink didn't allow you to customize the mapping.

Cheers,
Oliver
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 12513

I don't know TopLink, but in Kodo JDO, it could be done either by defining fixed values in the vendor mapping support files (both for booleans and for enumerations) OR if you preferred, you could define a custom mapping class and bind it to the field.


One of the most odious afflictions that Business has inflicted on the modern English language is "pro-active". Most of the time it's simply redundantly used in place of the simple old word "active". And a good deal of the rest of the time it means "You're not overworked enough yet, so go out and find more!"
 
 
subject: How JPA persist a boolean
 
Threads others viewed
JPA - Many to Many Question
The future of Hibernate in Spring
Toplink witth JPA vs Toplink API
@OneToMany Cascading deletion problem
Do you think JPA will achieve what JDO could not?
MyEclipse, The Clear Choice

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture