14

In my project, somewhere I have to work with a unitOfIssue of Items. Now, various items of course can have different units of representation. So, I was searching for some API or some way, to elegantly handle this situation.

Is there any available API, that provides some way to represent these units? I've heard of JScience which seems impressive, but again I am facing another problem with mapping it in JPA. After some google work, I found out that some work is going on in this context as - JScience-JPA, but it seems like it is not yet stable to be used in production.

I also found something about JSR-275, but from this JCP page it seems like it has been rejected.

Then I came across unitsofmeasure, which I haven't yet dig into details. But, again the same issue comes. Can this be mapped with JPA?

Edit: - From this SO question I came across Java Numbers with Unit but I don't know whether it is production ready or not.

I'm really confused, specially after seeing those options above. And JPA is an added concern as to whether I can use any of them or not. Have anyone ever faced such kind of situation, and found a way out of it? I really need some help here. What should I use? And if there is no other way out, then what would be appropriate way to represent these units. One way I see is by using enums. but, of course, that would be the last choice.

Community
  • 1
  • 1
Rohit Jain
  • 209,639
  • 45
  • 409
  • 525
  • @msandiford.. Yeah, I know. And I've quoted that thing in my question also with that link. But, it can't be used with JPA. Problem there. :( – Rohit Jain Jan 10 '13 at 07:20

2 Answers2

4

I was in a project in which we had extensive use of JSR-275 (before it was rejected by JCP). Originally we were not using JPA, but later it was decided that we should persist our model using JPA 2.0 and I had to deal with the persistence of my measure objects.

This may not be the answer to your question, but may bring some interesting thoughts to the discussion.

We considered several alternatives:

  1. Measures were serializable, and JPA can map any serializable object. The problem here is that the measures would be saved as binary objects in the database and they'd be subject to all the issues of serialization (i.e. versioning, etc.)
  2. We could adapt our model by keeping the data in raw types (i.e. integers, double, etc) provided that we agree on using the international unit of measure for every unit. Thus, fields would be based in JPA-supported types, but getters and setters would use measure objects. Configure JPA mapping to be based on fields, not properties. The problem here was having to alter the model to change encapsulated data without affecting the public interface of the domain objects.
  3. Since we were using hibernate as our persistence provider, we could accept to deviate from the JPA standard and use hibernate custom value types to map measure objects to ther corresponding primitive types. Then we could map our types using hibernate annotations for this purpose. (See an example here). The caveat in this one is losing persistence vendor independence.

We ended up using alternative #3 and it worked pretty well for us.

Edwin Dalorzo
  • 76,803
  • 25
  • 144
  • 205
  • Your 3rd alternative is what also came to my mind. Now, since we don't have any `Measure` object now, so it can't be considered. But, just to quote, in JPA, the conversion part can be done using `EclipseLink @Converter` annotation. I did that a few days back for mapping *joda time* `DateTime` to MySQL TimeStamp, and it worked pretty fine. I'll look into these conversions, by creating my own custom class. But it would be time taking. – Rohit Jain Jan 10 '13 at 08:22
  • @RohitJain That's true. It takes some time to develop a good converter that works for all units, but once it's done, the rest is a piece of cake. The caveat is that once you do that, you depend on that JPA implementation; you depend on your persistence provider from there on. Not every one has that luxury :-) – Edwin Dalorzo Jan 10 '13 at 08:27
  • 1
    @Edwin.. You're right. Dependency on a particular PersistenceProvider is a concern here. But, if I have to do that on my own, it would be freakingly crazy. :( – Rohit Jain Jan 10 '13 at 08:38
  • 1
    Note type converters are (will be) standardised in JPA2.1, so once you've written such a thing (to the standard) it can work with any JPA(2.1) provider – DataNucleus Jan 11 '13 at 19:43
1

I suggest you to write your own. Probably you may need Unit Of Measure Conversion also.

Definition of UOM from ARTS Retail Data Model Logical View

enter image description here

and for conversion

enter image description here

vels4j
  • 11,208
  • 5
  • 38
  • 63
  • Well, writing my own would be my last choice. But if I can get some pre-built API, then that will save me lot of time. Let's see if someone can come up with some magic. :) – Rohit Jain Jan 10 '13 at 07:54
  • Sure. I'm not sure how can you map with third party api in JPA or else you need to use some sort of primitive type code instead of using UOM entities. – vels4j Jan 10 '13 at 07:58