Kapilan Lakshmi
Kapilan Lakshmi

Reputation: 33

Grails 2.4.4 : hasOne is not solving the Bi-directional relationship

I have two tables Users and Role

These two tables should have bi-directional relationship for security purposes, so I have followed hasOne combination in Users table and belongs to in Role table. But bi-directional relationship is not happening.

Can anyone please help in this ?

class User
{

    Long number
    Integer inhabitants

    static hasOne = [ role: Role]

    static constraints = {
        role nullable: true, unique: true
    }
}

class Role
{

     Users user
     static belongsTo = [user: Users ] 
     static constraints = {
     }
}

Upvotes: 2

Views: 113

Answers (2)

Arjang
Arjang

Reputation: 729

I use Grails Plugin spring-security-core and these two pages to help me setup. Simplified Spring Security with Grails and Tutorials - Reference Documentation. It creates a table called UserRole that takes care of all that.

Upvotes: 0

shaydel
shaydel

Reputation: 589

Change Role to

class Role
{

     Users user
     static belongsTo = [user: User ] //you previously had Users here
     static constraints = {
     }
}

Upvotes: 1

Related Questions