JAXB 对象 - 哈希码和等于
我们有一个巨大的java应用程序,完全基于JAXB序列化工作。中间件服务器执行所有数据库访问并发送JAXB对象中的所有数据对象,序列化为XML并将数据发送到UI(C#.Net)。
大多数时候,在数据从数据库访问填充到 JAXB java 对象中后,我将不得不进行一些处理,例如“根据属性对对象集合进行排序”,找到平均值,对对象列表中的对象进行一些计算 我的
主要问题是,JAXB 对象没有 equals 和 hashcode。所以我正在做的是将所有数据移动到一些用户定义的数据对象,其中定义了 hashcode、equals、compareTo,这样我就可以在集合中执行所有操作,然后复制到 JAXB 对象。我认为这是额外的开销。
问题:
1)jaxb 对象是否支持 equals /hashcode/compareTo - 我可以在模式中指定这些吗?
2)还有其他更好的选择吗?
谢谢。
We have a huge java application that entirely works based on JAXB serialization.The middleware server does all db access and sends all the data Objects in JAXB objects and serializes to XML and sends the data to UI ( C#.Net).
Most of the times after the data is populated from db access into the JAXB java objects , I will have to some processing like "sort the collection of objects based on attribute" , find the avg , do some calculation on the list of objects in the collection etc.
My major problem is, JAXB objects don't have equals and hashcode. So what I am doing is moving all the data to some user defined Data objects where I have hashcode, equals, compareTo defined so I can do all operations in the collections and then copy to the JAXB objects. I think this is a extra overhead.
Questions:
1) does jaxb objects support equals /hashcode/ compareTo - can I specifiy these in schema?
2) Any other better alternatives?
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
不幸的是,jaxb 并没有提供开箱即用的功能。你可以使用这个插件,或者编写你自己的插件以获得更多可定制的行为。
unfortunately, jaxb does not provide this out of the box. you can use this plugin, or write your own for more customizable behavior.
看来您需要使用 Collections.sort(list,可比较) 来完成您想要的排序。等于和哈希码对您提到的任何一种情况都没有帮助,因为您的情况依赖于特定属性的比较,而不是整个对象的比较。
查找平均值和执行计算的其他情况也与我所看到的 equals/hashcode 无关。这些操作只需要解析列表并执行适当的算法。
It looks like you need to do use Collections.sort(list, Comparable) to accomplish the sorting that you want. Equals and hashcode won't help either of the cases you mentioned since your cases rely on comparison of specific attributes, not the object as a whole.
The other cases of finding averages and performing calculations also have nothing to do with equals/hashcode that I can see. These operations would simply require parsing the lists and performing your appropriate algorithm.
FWIW,虽然 JAXB 生成的 Java 类不会具有 equals 和 hashcode,但您可以在使用 JAXB 注释编写的类中添加这些覆盖 - JAXB 将忽略这些方法。
FWIW, while the JAXB-generated Java classes will not have equals and hashcode, you can add these overrides in the classes you write with JAXB annotations - JAXB will ignore the methods.
maven-jaxb2-plugin 可以使用自己的插件生成 hashcode 和 equals 方法: org.jvnet.jaxb2_commons。有关配置的更多详细信息可以在此处找到。
pom.xml的相关部分如下:
maven-jaxb2-plugin can generate hashcode and equals method using its own plugin: org.jvnet.jaxb2_commons. More details about configuration can be found here.
The relevant parts of the pom.xml are as follows: