views:

266

answers:

1

I am using Hibernate + JPA as my ORM solution.

I am using HSQL for unit testing and PostgreSQL as the real database.

I want to be able to use Postgres's native UUID type with Hibernate, and use the UUID in its String representation with HSQL for unit testing (since HSQL does not have a UUID type).

I am using a persistence XML with different configurations for Postgres and HSQL Unit Testing.

Here is how I have Hibernate "see" my custom UserType:

@Id
@Column(name="UUID", length=36)
@org.hibernate.annotations.Type(type="com.xxx.UUIDStringType")
public UUID getUUID() {
 return uuid;
}


public void setUUID(UUID uuid) {
 this.uuid = uuid;
}

and that works great. But what I need is the ability to swap out the "com.xxx.UUIDStringType" part of the annotation in XML or from a properties file that can be changed without re-compiling.

Any ideas?

+1  A: 

Perhaps you can build some smarts in your user type to do the right thing depending on the database capabilities. Hibernate itself takes a similar approach with its "native" ID generator, which behaves differently depending on the type of database you are using. An approach like this eliminates the need to switch the mapping at runtime.

For example, you could create one strategy class for each database. Then in your user type class, detect what database you're connected to when you're called for the first time, instantiate the proper strategy for that database, and then delegate all calls to the strategy object.

Rob H
Thanks man.. you are a life saver. The Strategy Pattern worked like a charm.
Grasper