In Java Persistence with Hibernate they recommend mapping Many-Many as 2
One-Manys with an explicitly mapped join class, in your case a "SnippetTag".
The rationale is that often you need to add extra columns to the join table
in addition to the 2 FKs.
If you use this approach it's simple to make one end of the association
unique.
Burt
On Thursday 15 May 2008 4:38:49 am James Hughes wrote:
> Hello,
>
> I am going to try and explain this as best I can. I have 2 domain classes
> Snippet and Tag - they are reated by a Many-to-Many relationship -- they
> look like this
>
> class Snippet {
> static hasMany = [tags:Tag]
>
> String title
> String description
> String category
> String code
>
> static constraints = {
> title(blank:false, nullable:false, maxSize:30)
> description(blank:true, nullable:true, maxSize:300)
> category(blank:false, nullable:false)
> code(blank:false, nullable:false)
> tags(size:1..10)
> }
> }
>
> And tag
>
> class Tag {
>
> static belongsTo = Snippet
> static hasMany = [snippets:Snippet]
>
> String name
>
> static constraints = {
> name(unique:true, blank:false, nullable:false)
> }
>
> boolean equals (Object other) {
> name.equals(other.name)
> }
> }
>
> What I am trying to do is add a constraint to the Snippet domain class so
> that you can't add the same Tag object twice. I am pretty unsure of how to
> do this. Can anyone offer assistance?
>
> Thanks
>
> James Hughes | Senior Software Engineer
>
> P Please consider the environment and do not print this mail unless
> necessary
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>
http://xircles.codehaus.org/manage_email---------------------------------------------------------------------
To unsubscribe from this list, please visit:
http://xircles.codehaus.org/manage_email