使用JPA2和Hibernate的Java原语的排序图? [英] Sorted map of Java primitives using JPA2 and Hibernate?

查看:90
本文介绍了使用JPA2和Hibernate的Java原语的排序图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Hibernate 3.6和JPA2用作Play Framework 1.2的一部分.我有一个实体类,需要按键排序的Double-to-Double映射.这是到目前为止我得到的:

I'm using Hibernate 3.6 and JPA2 as part of the Play Framework 1.2. I have an entity class that needs a Double-to-Double map, sorted on the keys. Here's what I've got so far:

@Entity
public class MyEntity extends Model
{
    @ElementCollection
    @MapKeyColumn(name="keycolumn")
    @OrderBy("keycolumn")
    public Map<Double, Double> myMap;   
}

我已经使用YAML故意将测试数据集加载到DB中,以便可以验证排序是否正常:

I've got a test set of data being loaded into my DB using YAML, deliberately not in order so that I can verify the sorting is working:

myMap: {100.0: -10.0, 200.0: -5.0, 125.0: -8.0, 300.0: -2.0, 50.0: -12.0}

不幸的是,到目前为止还不是:

And unfortunately, so far it's not:

Key: 100.000000, value: -10.000000
Key: 200.000000, value: -5.000000
Key: 125.000000, value: -8.000000
Key: 300.000000, value: -2.000000
Key: 50.000000, value: -12.000000

如果排序有效,我希望看到:

If the sorting was working, I would expect to see:

Key: 50.000000, value: -12.000000
Key: 100.000000, value: -10.000000
Key: 125.000000, value: -8.000000
Key: 200.000000, value: -5.000000    
Key: 300.000000, value: -2.000000

数据库不是我的强项,而且我对JPA还是很陌生.一直在浏览Hibernate文档和各种论坛,但没有真正的运气.非常感谢您的协助.谢谢!

Databases are not my strong suit, and I'm also fairly new to JPA. Been digging through the Hibernate docs and various forums, with no real luck. Any assistance is greatly appreciated. Thanks!

推荐答案

最快的方法是将其设置为SortedMap并使用@Sort:

The quickest way is to make it a SortedMap and use @Sort:

@Sort(type = SortType.COMPARATOR, comparator = WhateverComparator.class)

根据注释参考.

这篇关于使用JPA2和Hibernate的Java原语的排序图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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