<p>Given the following domain classes:</p>

<pre><code>class Post {
   SortedSet tags
   static hasMany = [tags: Tag]
}

class Tag {
   static belongsTo = Post
   static hasMany = [posts: Post]
}
</code></pre>

<p>From my understanding so far, using a <code>hasMany</code> will result in hibernate <code>Set</code> mapping.
However, in order to maintain uniqueness/order, Hibernate needs to load the entire set from the database and compare their hashes.</p>

<p>This could lead to a significant performance problem with adding and deleting posts/tags
if their sets get large. What is the best way to work around this issue?</p>

The ordering of the set is guaranteed by the
```
Set
```
implementation, ie, the
```
SortedSet
```
. Unless you use a
```
List
```
, which keeps track of indexes on the db, the ordering is server-side only.
If your domain class is in a
```
SortedSet
```
, you have to implement
```
Comparable
```
in order to enable the proper sorting of the set.
The question of performance is not really a question per se. If you want to access a single
```
Tag
```
, you should get it by its
Id
. If you want the sorted tags, well, the sort only makes sense if you are looking at all
```
Tags
```
, not a particular one, so you end up retrieving all
```
Tags
```
at once. Since the sorting is performed server-side and not db-side, there is really not much difference between a
```
SortedSet
```
and a regular
```
HashSet
```
in regards to Db.