-->
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: 1:n ordering
PostPosted: Wed Feb 03, 2010 8:02 am 
Newbie

Joined: Wed Feb 03, 2010 7:58 am
Posts: 2
hi!
i'm looking for a solution to the following problem:

i have a post and a media table. a post has a set of media elements:

Code:
@ManyToMany(fetch = FetchType.EAGER)
public Set<Media> getMedia() {
     return media;
}


the table structure looks like this:
table post (title, text)
table media (title, url)
table post_media (post_pk, media-pk)

now i want to be able to control the order of the media elements for a post, in fact if i submit the post initially but i also want to be able to change the order of the posts' media elements later on after the post has been submitted.

so my problem is that a media element can be referenced in more than one post, so i can't just add an int order property to my media element which i can use to sort the media elements in my client code.


Top
 Profile  
 
 Post subject: Re: 1:n ordering
PostPosted: Wed Feb 03, 2010 11:49 am 
Newbie

Joined: Mon Dec 28, 2009 9:34 am
Posts: 4
You can use List instead of Set. Lists have an order, and hibernate can solve the problem for you. I have an example with a class called MenuEntity which has a List of Elements

Code:
@OneToMany(cascade = CascadeType.ALL, mappedBy="menuPadre")
@IndexColumn(name = "numero_menu", base=1 ,nullable=false)
public List<ElementoEntity> getElementos() {
   return this.elementos;
}



The important thing here is the @IndexColumn annotation. You can find more documentation in hibernate annotations online doc:
2.4.6.2. Extra collection types
2.4.6.2.1. List

Hope this helps!


Top
 Profile  
 
 Post subject: Re: 1:n ordering
PostPosted: Wed Feb 03, 2010 12:23 pm 
Newbie

Joined: Wed Feb 03, 2010 7:58 am
Posts: 2
That worked, thanks!


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.