-->
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.  [ 3 posts ] 
Author Message
 Post subject: Annotating a many-to-many join table with extra data
PostPosted: Wed Jun 25, 2008 6:30 pm 
Newbie

Joined: Thu Dec 06, 2007 1:03 pm
Posts: 4
I have the following tables (in pseudocode):

CREATE TABLE FOO (
id int primary key,
name varchar(50)
)

CREATE TABLE GROUP (
id int primary key,
name varchar(50)
)

CREATE TABLE FOO_GROUP (
id int primary key,
foo_id int foreign key references FOO(id),
group_id int foreign key references GROUP(id),
order_index int
)

Note the "order_index" column in FOO_GROUP. This will show up later. I have the following objects (also in pseudocode):

@Entity
public class Foo {
... basic persisted bean ...
}

@Entity
public class Group {
...basic persisted stuff...

@ManyToMany
@JoinTable(...name, joinColumns, inverseJoinColumns...)
private List<Foo> foosInGroup;
}

Basically, if the FOO_GROUP table didn't have the "order_index" column, I'd be fine. However, I need the "foosInGroup" List in Group to be ordered based on the "order_index" field.

Is this possible? If so, how?

Thanks in advance!


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jun 26, 2008 3:46 am 
Hibernate Team
Hibernate Team

Joined: Thu Apr 05, 2007 5:52 am
Posts: 1689
Location: Sweden
Hi,

one way of doing this is to create an intermediate entity which you use to create a link between Foo and Group. This type of mapping was just recently discussed here http://forum.hibernate.org/viewtopic.php?t=987912.

One you have such an intermediate entity say FooGroupLink you should be able to use the @OrderBy annotation on your foosInGroup property:

Code:
    @OneToMany(mappedBy="group")
    @OrderBy("order_index")
    public List<FooGroupLink> getFooGroupLinks() {
        return FooGroupLinks;
    }


Another alternative would be to use to map the joint table ot a collection of components. This approach is described in "Java Persistence with Hibernate". The idea stays the same though. You need an intermediate 'entity' FooGroupLink which in this case is embeddable.

Hope this helps,
--Hardy


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jul 29, 2008 6:29 am 
Newbie

Joined: Thu Jul 24, 2008 10:13 am
Posts: 17
One more question about this situation:

Is there any way to retrieve "order_index" from Group or Foo?

This situation refers to a own attribute in the relation N:N, and I need to use retrieve it from one of the sides. ¿Can anyone help me?

I'm searching for this long time, but no answer got >_<


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.