0

Does JPA 2.0 support user defined types? I'm using OpenJPA. I have looked through the 2.0 spec, and I did not see anything about user defined types. I am a bit surprised, did I miss something?

By user defined types, I mean like Hibernate allows. For example, you can defined a customer Color type, that maps to RED, GREEN, and BLUE columns, then have a Color field in your object that is persisted. This can also be accomplished with an Embedded object too, but user defined types can be used for other things.

The particular problem I have is this:

I have a LocationCode type (which forms part of a Location entity), that is made up of 3 numeric fields. In the database schema I have been given, this is mapped to 1 column, which holds the 3 fields appended together as strings. I wanted to hide this 'mapping' logic from the application, in order to keep the business logic clean, and defined at a more abstract level than having to deal with storage logic like this.

My first attempt was to define LocationCode as a @Transient on my object, and have another String field mapped to the column. On @PreUpdate @PrePersist and on @PreLoad, the transformation between 3 numeric values and 1 string and the other way around is performed, by setting up the String field, or parsing it into the LocationCode field.

The problem is with EntityManager.merge(). I cannot merge a Location entity onto the database, as the @Transient LocationCode field is not copied by the merge. Therefore I must handle this part of the merge() myself, which kind of defeats the use of cascaded merges.

I figure that if I used a custom user defined type for LocationCode, then I could have a LocationCode which is a managed field, but is still able to perform the mapping of 3 numeric values onto 1 database column. Then merge() would work.

Are user defined types being added in a newer version of JPA, if they are not in 2.0? or perhaps I can find them in the OpenJPA specific API?

Thanks for your help.

user2800708
  • 1,890
  • 2
  • 18
  • 31

1 Answers1

0

No, JPA 2.0 does not, but JPA 2.1 does.

user2800708
  • 1,890
  • 2
  • 18
  • 31