简体   繁体   中英

Unique Constraint over values of two domain classes in Grails

I have two domain classes. One is :

 class User {
    String login
    String password
    String firstName
    String lastName
    String address
    String email

    static constraints = {
        login blank:false, size:5..15,matches:/[\S]+/, unique:true
        password blank:false, size:5..15,matches:/[\S]+/
        firstName blank:false
        lastName blank:false
        email email: true
    }
 }

And other is

class AddWebsite {

String website
User user
static constraints = { 
                     website blank:false 
                     website(unique: ['user'])
                     }
}

I am working with MongoDB at the backend. I need that for a particular login value, all siteURL values should be unique. Ex: login = abc@gmail.com. Then this user can have all unique url only in the database. But same urls can exist for different users. How do I do that using the unique constraint or any other approach?

In this case you should be able to place unique constraint on the AddWebsite domain class such as this:

class AddWebsite {
  String website
  User user
  static constraints = { 
    website(blank:false, unique: ['user'])
  }
}

This will ensure that each website is unique in the database per user. Notice that multiple constraints are applied to the property website.

edited to match updated question.

Use embedded sub-documents to store SiteURL instances right inside the User . Then you define the collection to be a Set, which makes sure, all it's entries are unique. If you want to use the default mongo collection types or want to persist the order, define an interceptor like:

def beforeSave = {
  urls = urls.unique() 
}

UPDATE: If your urls are plain strings, use the default primitive collection (no hasMany ):

class User {
  String login
  //...
  Set urls = new HashSet()
}

It finally worked. I was getting the user cannot be null error while entering the website though it was not being validated in the AddWebsite domain class. I made the following changes and got it to work:

class AddWebsite{
    String website
    User user
    static belongsTo = [user: User]
    static constraints = {
        website( url:true, unique: ['user'])
    }
}

And in my controller also, I set the value of the user object to the session variable:

def addWebsites() {
    if(request.method == 'POST') {
        def w = new AddWebsite()
        w.properties[
                    'website'
                ] = params
        w.user = session["user"]                       //modified to make it work
     if(w.save()) {
            render view:'addWebsites', model:[message: "Successfully saved"]
        }
        else {
            return [addWebsite:w]
        }
    }

Hope it helps someone :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM