-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: HQL; how to join over a MapKeyManyToMany
PostPosted: Tue Dec 21, 2010 5:17 pm 
Beginner
Beginner

Joined: Fri Nov 21, 2003 4:50 am
Posts: 23
After doing a lot of search, without finding a solution I hope to find one here

having a Map with a Entity as key and a Long a value I try to find a hql query to join to the key of the Map:


class Owner {
@CollectionOfElements()
@JoinTable(name = "Amounts", joinColumns = {@JoinColumn(name = "ownerId")})
@Column(name = "amount")
@MapKeyManyToMany(joinColumns = {@JoinColumn(name = "entityId")})
Map<Entity, Long> amounts;
}

so the question is, how to join from owner to entity

select entity from Owner owner join key(owner.amounts) as entity
select entity from Owner owner join owner.amounts.key as entity
select entity from Owner owner join index(owner.amounts) as entity
select entity from Owner owner join owner.amounts.index as entity
select entity from Owner owner, Entity entity where index(owner.amounts)=entity

all fails.

Is there a way to do it? I'm using hibernate 3.3.2


Top
 Profile  
 
 Post subject: Re: HQL; how to join over a MapKeyManyToMany
PostPosted: Tue Dec 21, 2010 5:37 pm 
Regular
Regular

Joined: Wed Feb 15, 2006 9:09 pm
Posts: 76
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-5396

You 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.


Top
 Profile  
 
 Post subject: Re: HQL; how to join over a MapKeyManyToMany
PostPosted: Tue Dec 21, 2010 6:05 pm 
Beginner
Beginner

Joined: Fri Nov 21, 2003 4:50 am
Posts: 23
i found a query which seems to work:

select entity from Owner owner join owner.amounts as amount join amount.index as entity


Top
 Profile  
 
 Post subject: Re: HQL; how to join over a MapKeyManyToMany
PostPosted: Tue Dec 21, 2010 6:08 pm 
Regular
Regular

Joined: Wed Feb 15, 2006 9:09 pm
Posts: 76
Wow... I've never seen that.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.