Unique Constraint over values of two domain classes in Grails

谁都会走 提交于 2019-12-14 03:25:04

问题


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?


回答1:


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()
}



回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/24244457/unique-constraint-over-values-of-two-domain-classes-in-grails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!