-->
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: Sorted Collection in many-to-many JPA mapping
PostPosted: Fri Aug 29, 2008 1:39 am 
Newbie

Joined: Fri Aug 29, 2008 1:20 am
Posts: 4
Hi!
I have a small problem, have looked around for a while without success, so I thought I would ask for some help.
Here the problem: I have a many-to-many mapping between users and groups that I want to define using a JPA ORM.

My classes are:

Code:
public class User implements Comparable<User> {
  private String name;
  protected SortedSet<Group> groups;
  public User() { this.groups = new TreeSet<Group>(); }
  public synchronized String getName() { return name; }
  public synchronized void setName(String name) { this.name = name; }
  public synchronized boolean equals(Object obj) { [...] }
  public synchronized int hashCode() { [...] }
  public synchronized int compareTo(User otherUser) { [...] }
}

public class Group implements Comparable<Group> {
  private String name;
  public SortedSet<User> users;
  public Group() { this.users = new TreeSet<User>(); }
  public synchronized String getName() { return name; }
  public synchronized void setName(String name) { this.name = name; }
  public synchronized boolean equals(Object obj) { [...] }
  public synchronized int hashCode() { [...] }
  public synchronized int compareTo(Group otherGroup) { [...] }
}



Here is my mapping:

Code:
  <entity name="User" class="foo.bar.User">
    <table name="USERS" />
    <attributes>
      <id name="name">
        <column name="NAME" unique="true" nullable="false" />
      </id>
      <many-to-many name="groups" mapped-by="users">
        <order-by>name</order-by>
        <join-table name="USER_GROUP_MAPPINGS">
          <join-column name="USER_NAME" nullable="false" />
          <inverse-join-column name="GROUP_NAME" nullable="false" />
        </join-table>
      </many-to-many>
    </attributes>
  </entity>
 
  <entity name="Group" class="foo.bar.Group">
    <table name="GROUPS" />
    <attributes>
      <id name="name">
        <column name="NAME" unique="true" nullable="false" />
      </id>
      <many-to-many name="users" mapped-by="groups" />
    </attributes>
  </entity>


And my problem is...

org.hibernate.AnnotationException: A sorted collection has to define @Sort: foo.bar.User.groups
at org.hibernate.cfg.annotations.CollectionBinder.bind(CollectionBinder.java:364)
at org.hibernate.cfg.AnnotationBinder.processElementAnnotations(AnnotationBinder.java:1614)
at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:754)
...


I don't even know where I could put a <sort> element in the mapping, it is not even in the XSD :o/

Thanks for your help,

Nicolas.
Code:


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 29, 2008 1:49 am 
Newbie

Joined: Fri Aug 29, 2008 1:20 am
Posts: 4
Well... I just found a workaround, by annotating my classes with org.hibernate.annotations.Sort. the classes now look like this.
In the User class, I now have:

@Sort(type = SortType.NATURAL)
protected SortedSet<Group> groups;

And in the Group class, I now have:

@Sort(type = SortType.NATURAL)
public SortedSet<User> users;

Which is, of course, less than ideal, since the whole idea was:
1. To use JPA mappings and not Hibernate ones
2. To use XML mappings and no annotations

So... if anyone had any suggestions...

Thanks again,

Nicolas.


Top
 Profile  
 
 Post subject: Re: Sorted Collection in many-to-many JPA mapping
PostPosted: Tue Aug 03, 2010 5:19 am 
Newbie

Joined: Tue Sep 07, 2004 2:45 am
Posts: 1
Thanks ndamour for the tip, you save my day!


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.