Adding Unqiueness Constraint To List

View: New views
12 Messages — Rating Filter:   Alert me  

Adding Unqiueness Constraint To List

by kouphax :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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



RE: Adding Unqiueness Constraint To List

by kouphax :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Actually apologies it seems I was doing something wrong.
 
With the code below you can add the same Tag twice...
 
        def snippet =  new Snippet(
            title:'Success Test',
            category:'dummy',
            code:"Simple Test",
            tags:[new Tag(name:"test01")]
        )          
       
        assert snippet.validate() == true        
        snippet.addToTags(new Tag(name:"test02"))
        assert snippet.validate() == true
        snippet.addToTags(new Tag(name:"test04"))
        assert snippet.validate() == true
        snippet.addToTags(new Tag(name:"test04"))
        assert snippet.validate() == true
        snippet.addToTags(new Tag(name:"test04"))
        assert snippet.validate() == true
        snippet.addToTags(new Tag(name:"test04"))
        assert snippet.validate() == true
        snippet.addToTags(new Tag(name:"test04"))
        assert snippet.validate() == true
 
However if i create the Tag object first only a single unqiue entry is added for Tag(name:"test04").  Now I realise the code above creates 5 DIFFERENT Tag objects with the name of test04 but I was wondering why the unique constraint in Tag doesn't fail during validation?  I assume this is expected behaviour but I would like to know why?  Does the code not cascade constraints?
 
Thanks
 
James Hughes | Senior Software Engineer | Kainos | M: +353 (0)877 931 634 | j.hughes@...
 
P Please consider the environment and do not print this mail unless necessary

________________________________

From: James Hughes [mailto:J.Hughes@...]
Sent: Thu 15/05/2008 09:38
To: user@...
Subject: [grails-user] Adding Unqiueness Constraint To List



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

winmail.dat (11K) Download Attachment

Re: Adding Unqiueness Constraint To List

by Jean-Noël Rivasseau-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

I am also wondering myself if the validation code cascades, I am not
sure of this. Anyway you are probably doing things wrong - as the
uniqueness constraint you impose means that there cannot be two tags
with same name either - whereas you probably want to allow tags with
identical names if they belong to different snippets.

I think you would need to write custom validation code to check if two
tags within a snippet have the same name. There's probably no built in
stuff to check that.

On Thu, May 15, 2008 at 11:05 AM, James Hughes <J.Hughes@...> wrote:

> Actually apologies it seems I was doing something wrong.
>
> With the code below you can add the same Tag twice...
>
>        def snippet =  new Snippet(
>            title:'Success Test',
>            category:'dummy',
>            code:"Simple Test",
>            tags:[new Tag(name:"test01")]
>        )
>
>        assert snippet.validate() == true
>        snippet.addToTags(new Tag(name:"test02"))
>        assert snippet.validate() == true
>        snippet.addToTags(new Tag(name:"test04"))
>        assert snippet.validate() == true
>        snippet.addToTags(new Tag(name:"test04"))
>        assert snippet.validate() == true
>        snippet.addToTags(new Tag(name:"test04"))
>        assert snippet.validate() == true
>        snippet.addToTags(new Tag(name:"test04"))
>        assert snippet.validate() == true
>        snippet.addToTags(new Tag(name:"test04"))
>        assert snippet.validate() == true
>
> However if i create the Tag object first only a single unqiue entry is added for Tag(name:"test04").  Now I realise the code above creates 5 DIFFERENT Tag objects with the name of test04 but I was wondering why the unique constraint in Tag doesn't fail during validation?  I assume this is expected behaviour but I would like to know why?  Does the code not cascade constraints?
>
> Thanks
>
> James Hughes | Senior Software Engineer | Kainos | M: +353 (0)877 931 634 | j.hughes@...
>
> P Please consider the environment and do not print this mail unless necessary
>
> ________________________________
>
> From: James Hughes [mailto:J.Hughes@...]
> Sent: Thu 15/05/2008 09:38
> To: user@...
> Subject: [grails-user] Adding Unqiueness Constraint To List
>
>
>
> 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
>
>

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



RE: Adding Unqiueness Constraint To List

by kouphax :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

"as the uniqueness constraint you impose means that there cannot be two tags
with same name either - whereas you probably want to allow tags with
identical names if they belong to different snippets."
 
No actually.  If a Tag for say "Java" exists I only want ONE instance of this and for it to be shared between multiple Snippets.  I can do this - I have proven it with some extra code in a controler class.  However I was just trying to tighten up my domain class constraints and came across this issue.
 
James Hughes | Senior Software Engineer | Kainos | M: +353 (0)877 931 634 | j.hughes@...
 
P Please consider the environment and do not print this mail unless necessary

________________________________

From: Jean-Noël Rivasseau [mailto:elvanor@...]
Sent: Thu 15/05/2008 10:12
To: user@...
Subject: Re: [grails-user] Adding Unqiueness Constraint To List



Hi,

I am also wondering myself if the validation code cascades, I am not
sure of this. Anyway you are probably doing things wrong - as the
uniqueness constraint you impose means that there cannot be two tags
with same name either - whereas you probably want to allow tags with
identical names if they belong to different snippets.

I think you would need to write custom validation code to check if two
tags within a snippet have the same name. There's probably no built in
stuff to check that.

On Thu, May 15, 2008 at 11:05 AM, James Hughes <J.Hughes@...> wrote:

> Actually apologies it seems I was doing something wrong.
>
> With the code below you can add the same Tag twice...
>
>        def snippet =  new Snippet(
>            title:'Success Test',
>            category:'dummy',
>            code:"Simple Test",
>            tags:[new Tag(name:"test01")]
>        )
>
>        assert snippet.validate() == true
>        snippet.addToTags(new Tag(name:"test02"))
>        assert snippet.validate() == true
>        snippet.addToTags(new Tag(name:"test04"))
>        assert snippet.validate() == true
>        snippet.addToTags(new Tag(name:"test04"))
>        assert snippet.validate() == true
>        snippet.addToTags(new Tag(name:"test04"))
>        assert snippet.validate() == true
>        snippet.addToTags(new Tag(name:"test04"))
>        assert snippet.validate() == true
>        snippet.addToTags(new Tag(name:"test04"))
>        assert snippet.validate() == true
>
> However if i create the Tag object first only a single unqiue entry is added for Tag(name:"test04").  Now I realise the code above creates 5 DIFFERENT Tag objects with the name of test04 but I was wondering why the unique constraint in Tag doesn't fail during validation?  I assume this is expected behaviour but I would like to know why?  Does the code not cascade constraints?
>
> Thanks
>
> James Hughes | Senior Software Engineer | Kainos | M: +353 (0)877 931 634 | j.hughes@...
>
> P Please consider the environment and do not print this mail unless necessary
>
> ________________________________
>
> From: James Hughes [mailto:J.Hughes@...]
> Sent: Thu 15/05/2008 09:38
> To: user@...
> Subject: [grails-user] Adding Unqiueness Constraint To List
>
>
>
> 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
>
>
---------------------------------------------------------------------
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

winmail.dat (13K) Download Attachment

Re: Adding Unqiueness Constraint To List

by Jean-Noël Rivasseau-5 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On Thu, May 15, 2008 at 11:15 AM, James Hughes <J.Hughes@...> wrote:
> "as the uniqueness constraint you impose means that there cannot be two tags
> with same name either - whereas you probably want to allow tags with
> identical names if they belong to different snippets."
>
> No actually.  If a Tag for say "Java" exists I only want ONE instance of this and for it to be shared between multiple Snippets.  I can do this - I have proven it with some extra code in a controler class.  However I was just trying to tighten up my domain class constraints and came across this issue.

Ha, I see. Then you are probably fine - you just have to learn if the
validation code cascades (and as I said I would like to know the
answer too).

>
> James Hughes | Senior Software Engineer | Kainos | M: +353 (0)877 931 634 | j.hughes@...
>
> P Please consider the environment and do not print this mail unless necessary
>
> ________________________________
>
> From: Jean-Noël Rivasseau [mailto:elvanor@...]
> Sent: Thu 15/05/2008 10:12
> To: user@...
> Subject: Re: [grails-user] Adding Unqiueness Constraint To List
>
>
>
> Hi,
>
> I am also wondering myself if the validation code cascades, I am not
> sure of this. Anyway you are probably doing things wrong - as the
> uniqueness constraint you impose means that there cannot be two tags
> with same name either - whereas you probably want to allow tags with
> identical names if they belong to different snippets.
>
> I think you would need to write custom validation code to check if two
> tags within a snippet have the same name. There's probably no built in
> stuff to check that.
>
> On Thu, May 15, 2008 at 11:05 AM, James Hughes <J.Hughes@...> wrote:
>> Actually apologies it seems I was doing something wrong.
>>
>> With the code below you can add the same Tag twice...
>>
>>        def snippet =  new Snippet(
>>            title:'Success Test',
>>            category:'dummy',
>>            code:"Simple Test",
>>            tags:[new Tag(name:"test01")]
>>        )
>>
>>        assert snippet.validate() == true
>>        snippet.addToTags(new Tag(name:"test02"))
>>        assert snippet.validate() == true
>>        snippet.addToTags(new Tag(name:"test04"))
>>        assert snippet.validate() == true
>>        snippet.addToTags(new Tag(name:"test04"))
>>        assert snippet.validate() == true
>>        snippet.addToTags(new Tag(name:"test04"))
>>        assert snippet.validate() == true
>>        snippet.addToTags(new Tag(name:"test04"))
>>        assert snippet.validate() == true
>>        snippet.addToTags(new Tag(name:"test04"))
>>        assert snippet.validate() == true
>>
>> However if i create the Tag object first only a single unqiue entry is added for Tag(name:"test04").  Now I realise the code above creates 5 DIFFERENT Tag objects with the name of test04 but I was wondering why the unique constraint in Tag doesn't fail during validation?  I assume this is expected behaviour but I would like to know why?  Does the code not cascade constraints?
>>
>> Thanks
>>
>> James Hughes | Senior Software Engineer | Kainos | M: +353 (0)877 931 634 | j.hughes@...
>>
>> P Please consider the environment and do not print this mail unless necessary
>>
>> ________________________________
>>
>> From: James Hughes [mailto:J.Hughes@...]
>> Sent: Thu 15/05/2008 09:38
>> To: user@...
>> Subject: [grails-user] Adding Unqiueness Constraint To List
>>
>>
>>
>> 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
>>
>>
>
> ---------------------------------------------------------------------
> 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
>
>

RE: Adding Unqiueness Constraint To List

by kouphax :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

I did a quick test and no validation is not cascading
 
def snippet =  new Snippet(
    title:'Success Test',
    category:'dummy',
    code:"Simple Test",
    tags:[new Tag(name:"test01")]
)          
       
new Tag(name:"test04").save()
assert snippet.validate() == true        
       
snippet.addToTags(new Tag(name:"test04"))
assert snippet.validate() == true
 
The last assert will pass even though it breaks the Tag.name unique constraint but when I attempt to do a save Hibernate throws a org.hibernate.exception.ConstraintViolationException.  Is this a bug or expected behaviour?  I would have thought at the very least this could be more graceful?
 
James Hughes | Senior Software Engineer | Kainos | M: +353 (0)877 931 634 | j.hughes@...
 
P Please consider the environment and do not print this mail unless necessary

________________________________

From: elvanor@... on behalf of Jean-Noël Rivasseau
Sent: Thu 15/05/2008 10:33
To: user@...
Subject: Re: [grails-user] Adding Unqiueness Constraint To List



On Thu, May 15, 2008 at 11:15 AM, James Hughes <J.Hughes@...> wrote:
> "as the uniqueness constraint you impose means that there cannot be two tags
> with same name either - whereas you probably want to allow tags with
> identical names if they belong to different snippets."
>
> No actually.  If a Tag for say "Java" exists I only want ONE instance of this and for it to be shared between multiple Snippets.  I can do this - I have proven it with some extra code in a controler class.  However I was just trying to tighten up my domain class constraints and came across this issue.

Ha, I see. Then you are probably fine - you just have to learn if the
validation code cascades (and as I said I would like to know the
answer too).

>
> James Hughes | Senior Software Engineer | Kainos | M: +353 (0)877 931 634 | j.hughes@...
>
> P Please consider the environment and do not print this mail unless necessary
>
> ________________________________
>
> From: Jean-Noël Rivasseau [mailto:elvanor@...]
> Sent: Thu 15/05/2008 10:12
> To: user@...
> Subject: Re: [grails-user] Adding Unqiueness Constraint To List
>
>
>
> Hi,
>
> I am also wondering myself if the validation code cascades, I am not
> sure of this. Anyway you are probably doing things wrong - as the
> uniqueness constraint you impose means that there cannot be two tags
> with same name either - whereas you probably want to allow tags with
> identical names if they belong to different snippets.
>
> I think you would need to write custom validation code to check if two
> tags within a snippet have the same name. There's probably no built in
> stuff to check that.
>
> On Thu, May 15, 2008 at 11:05 AM, James Hughes <J.Hughes@...> wrote:
>> Actually apologies it seems I was doing something wrong.
>>
>> With the code below you can add the same Tag twice...
>>
>>        def snippet =  new Snippet(
>>            title:'Success Test',
>>            category:'dummy',
>>            code:"Simple Test",
>>            tags:[new Tag(name:"test01")]
>>        )
>>
>>        assert snippet.validate() == true
>>        snippet.addToTags(new Tag(name:"test02"))
>>        assert snippet.validate() == true
>>        snippet.addToTags(new Tag(name:"test04"))
>>        assert snippet.validate() == true
>>        snippet.addToTags(new Tag(name:"test04"))
>>        assert snippet.validate() == true
>>        snippet.addToTags(new Tag(name:"test04"))
>>        assert snippet.validate() == true
>>        snippet.addToTags(new Tag(name:"test04"))
>>        assert snippet.validate() == true
>>        snippet.addToTags(new Tag(name:"test04"))
>>        assert snippet.validate() == true
>>
>> However if i create the Tag object first only a single unqiue entry is added for Tag(name:"test04").  Now I realise the code above creates 5 DIFFERENT Tag objects with the name of test04 but I was wondering why the unique constraint in Tag doesn't fail during validation?  I assume this is expected behaviour but I would like to know why?  Does the code not cascade constraints?
>>
>> Thanks
>>
>> James Hughes | Senior Software Engineer | Kainos | M: +353 (0)877 931 634 | j.hughes@...
>>
>> P Please consider the environment and do not print this mail unless necessary
>>
>> ________________________________
>>
>> From: James Hughes [mailto:J.Hughes@...]
>> Sent: Thu 15/05/2008 09:38
>> To: user@...
>> Subject: [grails-user] Adding Unqiueness Constraint To List
>>
>>
>>
>> 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
>>
>>
>
> ---------------------------------------------------------------------
> 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
>
>



---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email

winmail.dat (16K) Download Attachment

Re: Adding Unqiueness Constraint To List

by Jean-Noël Rivasseau-3 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Ok, so your results are the same as mine - I did not have the
impression that validation was cascading either.

I don't think it's a bug, as validation cascading can probably become
costly. It would be nice to have the choice in a configuration option
- you can maybe raise a JIRA about that.



On Thu, May 15, 2008 at 11:39 AM, James Hughes <J.Hughes@...> wrote:

> I did a quick test and no validation is not cascading
>
> def snippet =  new Snippet(
>    title:'Success Test',
>    category:'dummy',
>    code:"Simple Test",
>    tags:[new Tag(name:"test01")]
> )
>
> new Tag(name:"test04").save()
> assert snippet.validate() == true
>
> snippet.addToTags(new Tag(name:"test04"))
> assert snippet.validate() == true
>
> The last assert will pass even though it breaks the Tag.name unique constraint but when I attempt to do a save Hibernate throws a org.hibernate.exception.ConstraintViolationException.  Is this a bug or expected behaviour?  I would have thought at the very least this could be more graceful?
>
> James Hughes | Senior Software Engineer | Kainos | M: +353 (0)877 931 634 | j.hughes@...
>
> P Please consider the environment and do not print this mail unless necessary
>
> ________________________________
>
> From: elvanor@... on behalf of Jean-Noël Rivasseau
> Sent: Thu 15/05/2008 10:33
> To: user@...
> Subject: Re: [grails-user] Adding Unqiueness Constraint To List
>
>
>
> On Thu, May 15, 2008 at 11:15 AM, James Hughes <J.Hughes@...> wrote:
>> "as the uniqueness constraint you impose means that there cannot be two tags
>> with same name either - whereas you probably want to allow tags with
>> identical names if they belong to different snippets."
>>
>> No actually.  If a Tag for say "Java" exists I only want ONE instance of this and for it to be shared between multiple Snippets.  I can do this - I have proven it with some extra code in a controler class.  However I was just trying to tighten up my domain class constraints and came across this issue.
>
> Ha, I see. Then you are probably fine - you just have to learn if the
> validation code cascades (and as I said I would like to know the
> answer too).
>
>>
>> James Hughes | Senior Software Engineer | Kainos | M: +353 (0)877 931 634 | j.hughes@...
>>
>> P Please consider the environment and do not print this mail unless necessary
>>
>> ________________________________
>>
>> From: Jean-Noël Rivasseau [mailto:elvanor@...]
>> Sent: Thu 15/05/2008 10:12
>> To: user@...
>> Subject: Re: [grails-user] Adding Unqiueness Constraint To List
>>
>>
>>
>> Hi,
>>
>> I am also wondering myself if the validation code cascades, I am not
>> sure of this. Anyway you are probably doing things wrong - as the
>> uniqueness constraint you impose means that there cannot be two tags
>> with same name either - whereas you probably want to allow tags with
>> identical names if they belong to different snippets.
>>
>> I think you would need to write custom validation code to check if two
>> tags within a snippet have the same name. There's probably no built in
>> stuff to check that.
>>
>> On Thu, May 15, 2008 at 11:05 AM, James Hughes <J.Hughes@...> wrote:
>>> Actually apologies it seems I was doing something wrong.
>>>
>>> With the code below you can add the same Tag twice...
>>>
>>>        def snippet =  new Snippet(
>>>            title:'Success Test',
>>>            category:'dummy',
>>>            code:"Simple Test",
>>>            tags:[new Tag(name:"test01")]
>>>        )
>>>
>>>        assert snippet.validate() == true
>>>        snippet.addToTags(new Tag(name:"test02"))
>>>        assert snippet.validate() == true
>>>        snippet.addToTags(new Tag(name:"test04"))
>>>        assert snippet.validate() == true
>>>        snippet.addToTags(new Tag(name:"test04"))
>>>        assert snippet.validate() == true
>>>        snippet.addToTags(new Tag(name:"test04"))
>>>        assert snippet.validate() == true
>>>        snippet.addToTags(new Tag(name:"test04"))
>>>        assert snippet.validate() == true
>>>        snippet.addToTags(new Tag(name:"test04"))
>>>        assert snippet.validate() == true
>>>
>>> However if i create the Tag object first only a single unqiue entry is added for Tag(name:"test04").  Now I realise the code above creates 5 DIFFERENT Tag objects with the name of test04 but I was wondering why the unique constraint in Tag doesn't fail during validation?  I assume this is expected behaviour but I would like to know why?  Does the code not cascade constraints?
>>>
>>> Thanks
>>>
>>> James Hughes | Senior Software Engineer | Kainos | M: +353 (0)877 931 634 | j.hughes@...
>>>
>>> P Please consider the environment and do not print this mail unless necessary
>>>
>>> ________________________________
>>>
>>> From: James Hughes [mailto:J.Hughes@...]
>>> Sent: Thu 15/05/2008 09:38
>>> To: user@...
>>> Subject: [grails-user] Adding Unqiueness Constraint To List
>>>
>>>
>>>
>>> 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
>>>
>>>
>>
>> ---------------------------------------------------------------------
>> 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
>>
>>
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>

Re: Adding Unqiueness Constraint To List

by Peter Ledbrook-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

2008/5/15 Jean-Noël Rivasseau <elvanor@...>:
> Ok, so your results are the same as mine - I did not have the
> impression that validation was cascading either.
>
> I don't think it's a bug, as validation cascading can probably become
> costly. It would be nice to have the choice in a configuration option
> - you can maybe raise a JIRA about that.

I think there's a logic bug in Grails: even if you have a "belongsTo"
field set, the validation does not cascade. In my opinion it should
(as long as deep validation is switched on). I haven't raised an issue
yet, though.

Cheers,

Peter

--
Software Engineer
G2One, Inc.
http://www.g2one.com/

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



RE: Adding Unqiueness Constraint To List

by kouphax :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

"as long as deep validation is switched on"
 
Is this on by default and where can I check it?  It could be the casue of my problem.
 
James Hughes | Senior Software Engineer | Kainos | M: +353 (0)877 931 634 | j.hughes@...
 
P Please consider the environment and do not print this mail unless necessary

________________________________

From: Peter Ledbrook [mailto:peter@...]
Sent: Thu 15/05/2008 12:39
To: user@...
Subject: Re: [grails-user] Adding Unqiueness Constraint To List



2008/5/15 Jean-Noël Rivasseau <elvanor@...>:
> Ok, so your results are the same as mine - I did not have the
> impression that validation was cascading either.
>
> I don't think it's a bug, as validation cascading can probably become
> costly. It would be nice to have the choice in a configuration option
> - you can maybe raise a JIRA about that.

I think there's a logic bug in Grails: even if you have a "belongsTo"
field set, the validation does not cascade. In my opinion it should
(as long as deep validation is switched on). I haven't raised an issue
yet, though.

Cheers,

Peter

--
Software Engineer
G2One, Inc.
http://www.g2one.com/

---------------------------------------------------------------------
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

winmail.dat (7K) Download Attachment

Re: Adding Unqiueness Constraint To List

by Peter Ledbrook-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

2008/5/15 James Hughes <J.Hughes@...>:
> "as long as deep validation is switched on"
>
> Is this on by default and where can I check it?  It could be the casue of my problem.

It's on by default. You can do a "shallow" validation like this:

  domain.validate(deepValidate: false)

Cheers,

Peter

--
Software Engineer
G2One, Inc.
http://www.g2one.com/

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: Adding Unqiueness Constraint To List

by burtbeckwith :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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



Re: Adding Unqiueness Constraint To List

by banditore :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

maybe i'm wrong, but shouldn't be this just the behaviour of your code:

the default collection type for relationships is a Set. And since you have implemented Tag.equals by comparing names, you may add an equal named Tag as often as you want