SQL Zone is brought to you in partnership with:

More than 10 years of experience in Enterprise Applications development, and Project Management (since June 2001). Specialties: Enterprise Applications, J2EE, J2SE, Spring Framework, Spring Security, Spring Webflow, JPA and JDBC, JMX, JAXB, JUnit; XML, XSLT and XSL-FO; HTML/CSS, Javascript, JSON, jQuery/jQuery UI; RDBMS (Oracle, SQL Server, MySQL), PL/SQL; UML; Mobile Applications, mostly in Objective-C targeted iPhone/iPad Michal is a DZone MVB and is not an employee of DZone and has posted 12 posts at DZone. You can read more from them at their website. View Full User Profile

JPA - Hibernate - Type mapping on package level

  • submit to reddit

When we are finally mature enough to use some custom types mapping in JPA, we usually stuck with some provider specific solution, because JPA itself doesn't define any mechanism for doing it. Let me show you an example of custom type mapping definition for one of the JPA providers - Hibernate.

Suppose that we use Joda Money in our project, and have an entity with property having type Money. There are already pretty nice type mapping implementations for Money, provided by Jadira - User Types project. All we have to do is just let Hibernate know that we want to use specific type mapping.

When you look at the Hibernate Docs, Section Type, you'll see few possibilities, starting from the simplest - using @Type annotation on each property having Money type. This choice can be good if you have only one, or very few, properties of this type in your domain mapping. It is very probable that sooner or later, when your project will grow enough, there will be more and more of them, and you end up with many similar lines defining the same type mapping.

If you aren't a big fan of repeating yourself, or you don't trust in refactorings made by your apprentices ;), you should consider another way, using @TypeDefs and @TypeDef annotations.

As you may read in Hibernate documentation: "These annotations can be placed at the class or package level." - Let's focus on the second option - package level.

We will place these annotations in package-info.java for our domain entities holding package (see: Java Language Specification - 7.4.1. Named Packages). It will look like this:

* Provides the domain model.
* @author Warlock
@org.hibernate.annotations.TypeDef(name = "money", defaultForType = Money.class, typeClass = PersistentMoneyAmount.class)
package com.blogspot.vardlokkur.domain;
import org.jadira.usertype.moneyandcurrency.joda.PersistentMoneyAmount;
import org.joda.money.Money;

Now, when you map the property using Money type, you can do it without additional type mapping specification, just like this:

package com.blogspot.vardlokkur.domain;
import org.joda.money.Money;
@Table(name = "EMPLOYEE")
public class Employee implements Serializable {
@Column(name = "SALARY")
private Money salary;

One technical note, before you become happy Money mapping user ;) - Because PersistentMoneyAmount uses single column (holding amount) for Money mapping, it requires defining of currency which will be used along with the amount. The default currency can be defined as Persistence Unit property: jadira.usertype.currencyCode

PS. Don't treat the above Money example as the guideline of Joda Money mapping :), there are probably better ways of doing it, see Jadira User Types blog.

Few links for the dessert:

Published at DZone with permission of Michal Jastak, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)