是否有任何可用的API来代表各种单位的项目,如KG,升,米,KM等 [英] Is there any available API to represent various units of item like KG, Litre, Metre, KM, etc

查看:135
本文介绍了是否有任何可用的API来代表各种单位的项目,如KG,升,米,KM等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目中,我必须使用 项目 unitOfIssue 。现在,各种项目当然可以有不同的表示单位。所以,我正在寻找一些API或某种方式来优雅地处理这种情况。

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.

是否有任何可用的API,它提供了一些表示这些单位的方法?我听说过 JScience ,这看起来令人印象深刻,但我再次面临另一个问题,即将其映射到 JPA 。经过一些谷歌工作,我发现在这种情况下正在进行一些工作 - JScience-JPA ,但似乎在生产中使用它还不稳定。

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.

我还发现了一些关于 JSR-275 ,但来自这个JCP页面好像被拒绝了。

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

然后我遇到了 unitsofmeasure ,我还没有深入细节。但是,同样的问题也来了。这可以用JPA映射吗?

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?

编辑: - 来自这个问题 包含单位的Java编号但我不知道它是否已准备就绪。

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

我真的很困惑,特别是在看到上述选项之后。 JPA是一个额外的问题,我是否可以使用它们中的任何一个。有没有人遇到过这样的情况,并找到了解决方法?我真的需要一些帮助。我该怎么用?
如果没有其他出路,那么表示这些单位的适当方式是什么。我看到的一种方法是使用枚举。但是,当然,这将是最后的选择。

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.

推荐答案

我参与了一个广泛使用JSR的项目-275(在被JCP拒绝之前)。最初我们没有使用JPA,但后来我们决定使用JPA 2.0来保持我们的模型,并且我必须处理我的度量对象的持久性。

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.

我们考虑了几种选择:


  1. 测量是可序列化的,JPA可以映射任何可序列化的对象。这里的问题是测量将被保存为数据库中的二进制对象,并且它们将受到序列化的所有问题(即版本控制等)。

  2. 我们可以调整我们的如果我们同意对每个单位使用国际计量单位,则将数据保存在原始类型(即整数,双数等)中。因此,字段将基于JPA支持的类型,但getter和setter将使用measure对象。将JPA映射配置为基于字段,而不是属性。这里的问题是必须更改模型以更改封装数据而不影响域对象的公共接口。

  3. 由于我们使用hibernate作为持久性提供程序,我们可以接受偏离JPA标准并使用 hibernate自定义值类型将度量对象映射到相应的基元类型。然后我们可以使用hibernate注释来映射我们的类型。 (在此处查看示例)。这一点的警告是失去持久性供应商的独立性。

  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.

我们最终使用备选方案#3,它对我们来说效果很好。

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

这篇关于是否有任何可用的API来代表各种单位的项目,如KG,升,米,KM等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆