The general advice for dealing with Map collections is to
stay away. Stay very away. The spec is fine but I don't think any implementors get it right.
That said, given the following "rows" in your database:
- Entity#1
- Entity#2
- Owner.amounts[Entity#1] = 800
- Owner.amounts[Entity#2] = 400
Code:
select index(ent), ent from Owner own join own.amounts ent order by ent
will give you:
- Entity#2 | 400
- Entity#1 | 800
So use the hibernate-specific built-in index(). For reference, here's what the JPA2 specification actually calls for:
Code:
select key(ent), value(ent) from Owner own join own.amounts ent order by value(ent)
But Hibernate doesn't support this:
http://opensource.atlassian.com/project ... e/HHH-5396You could also try mapping with JPA2 annotations just for the heck of it:
Code:
@ElementCollection
@CollectionTable( name="Amounts", joinColumns=@JoinColumn( name="ownerId" ) )
@Column( name="amount" )
@MapKeyJoinColumn( name="entityId" )
Map<Entity, Long> amounts;
and then let me know if you find it as broken as I do :). Yes, I'm bitter. I've had a heck of a lot of pain dealing with maps. I suspect a lot of it stems with the JPA2 compatibility toolkit, but thanks for letting me rant.