This week's giveaway is in the Spring forum.
We're giving away four copies of Microservices Testing (Live Project) and have Chris Love & Andres Sacco on-line!
See this thread for details.
Win a copy of Microservices Testing (Live Project) this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Conversion between DB data and java data ?

 
Ranch Hand
Posts: 311
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I've just started using JPA, and would appreciate it if anyone could help with the following question:

What is the recommended way to define a conversion rule between my database data, and my Java data ?
For example, say my (badly designed) database stores string values of "YES" and "NO", which I'd like to convert to java boolean :


What is the most recommended way to do the conversion ?
Thanks
[ September 30, 2007: Message edited by: Sol Mayer-Orn ]
 
Ranch Hand
Posts: 364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not sure that you can do this directly in JPA. With a bit of flexibility, though, either on the Java side (use an enum) or on the DB side (change the column type to whatever a boolean is persisted to), I think one can move forward here.
 
Sol Mayer-Orn
Ranch Hand
Posts: 311
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks very much
 
Ranch Hand
Posts: 553
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The most portable way to define the conversion is to use property access for the class and define get/set methods that convert the value.

i.e.
public class Subscription {
..
@Transient
public boolean isMonthly() {
return isMonthly;
}
public void setIsMonthly(boolean isMonthly) {
this.isMonthly = isMonthly;
}
@Basic
private String getMonthlyString() {
if (isMonthly() {
return "Yes";
} else {
return "No";
}
}
private String setMonthlyString(String value) {
setIsMonthly(value.eqauls("Yes"));
}
}

However most JPA implementation have extended conversion support. If you are using TopLink (or TopLink Essentials, or EclipseLink) you can use a Converter on your mapping. In TopLink 11g you can use the @ObjectTypeConverter and @Convert annotations for this.

If you are using Hibernate you can use their custom type system and their @Type annotation.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic