1

I have a grails application. I've installed spring security core plugin 2.0 RC2 and I'm creating roles, users and requestmap in BootStrap.groovy Grails is throwing the following error when I run the app.

ERROR context.GrailsContextLoader  - Error initializing the application: Unknown entity: Role

Here is my Role class (generated by the spring security plugin and modified to extend AbstractActivity with is an abstract class that has beforeInsert() method/closure and does some validation).

package com.mypackage

class Role extends AbstractDomain {

    String authority

    Date dateCreated
    Date lastUpdated
    User createdBy
    User updatedBy

    static mapping = {
        cache true
    }

    static constraints = {
        authority blank: false, unique: true

            createdBy nullable: true
            updatedBy nullable: true
    }
}    

AbstractDomain.groovy

package com.mypackage

import com.mypackage.User

class AbstractDomain implements Serializable {

transient springSecurityService

def beforeInsert() {

    if(null != springSecurityService) {

        User user = springSecurityService.getCurrentUser()

        if(null != user) {

            this.createdBy = user
        }
    }
}

}

BootStrap.groovy

import com.mypackage.Role

class BootStrap {
    def init = { servletContext ->
          if (Role.findByAuthority('ROLE_ADMIN') == null)
                def adminRole = new Role(authority: 'ROLE_ADMIN').save(flush: true)
    }
    def destroy = {}
}

Config.groovy

grails.plugin.springsecurity.userLookup.userDomainClassName ='com.mypackage.User'
grails.plugin.springsecurity.userLookup.authorityJoinClassName 'com.mypackage.UserRole'
grails.plugin.springsecurity.authority.className = 'com.mypackage.Role'
grails.plugin.springsecurity.requestMap.className = 'com.mypackage.Requestmap'
grails.plugin.springsecurity.securityConfigType = 'Requestmap'

How do I solve the Unknown entity issue?

Siguza
  • 21,155
  • 6
  • 52
  • 89
biniam
  • 8,099
  • 9
  • 49
  • 58

4 Answers4

1

I found the solution! My AbstractDomain Class which was extended by Role class was in 'src/groovy' folder and I forgot to make it 'abstract'. Then I added the abstract keyword before class, it works like magic. Thanks @mgg for giving me the idea here

So, my AbstractDomain class now looks like

abstract class AbstractDomain implements Serializable {
...
Community
  • 1
  • 1
biniam
  • 8,099
  • 9
  • 49
  • 58
0

Try replacing

import com.mypackage.Role

with

import com.mypackage.model.Role
Maurizio In denmark
  • 4,226
  • 2
  • 30
  • 64
tim_yates
  • 167,322
  • 27
  • 342
  • 338
0

You should have following configuration in Config.groovy to let Spring security plugin know which class you use for roles persistence:

  grails.plugins.springsecurity.authority.className = "com.mypackage.model.Role"

the same applies for users:

  grails.plugins.springsecurity.userLookup.userDomainClassName = "com.mypackage.model.User"

See http://grails-plugins.github.io/grails-spring-security-core/docs/manual/guide/domainClasses.html#authorityClass

lukelazarovic
  • 1,510
  • 11
  • 19
  • Thanks but I already have the classNames in Config.groovy and I have a properly formatted package name. This (mypackage) is just for example. I edited the question by adding Config.groovy – biniam Apr 02 '14 at 11:07
  • Ok but as tim_yates noticed there is discrepancy between package you use in Config.groovy and Bootstrap and the one you have in your Role class source - com.mypackage.Role vs. com.mypackage.model.Role – lukelazarovic Apr 02 '14 at 11:18
  • Thanks @lukelazarovic for noticing that. It was a typo error. Evenif the package name in the model, the BuildConfig and Config.groovy is the same, I'm getting the same problem. I think the problem might be that Role is extending AbstractDomain class. Could that be the issue, since Role was created by spring security? It's not even creating the tables in mysql. – biniam Apr 02 '14 at 11:44
  • Yes, that could be the problem. You can find out easily;) Anyway, post also the source of AbstractDomain so we can advise a solution. – lukelazarovic Apr 02 '14 at 11:50
  • Thanks for following up. I added the AbstractDomain and modified the Role model. Can you please check now? – biniam Apr 02 '14 at 11:55
0

This doesn't appear to be a Spring Security issue. The error is coming from Grails - "Unknown entity: Role". This usually happens when there is a mismatch between the package name in a domain class (or other artifact) and the folder the file is in.

If you want the Role class to be in the com.mypackage package, make sure that the file is in grails-app/domain/com/mypackage/Role.groovy. The names are case-sensitive too, so make sure that the folder and package names agree there too. Also, you probably didn't do this, but I've seen people make mistakes like creating a "com.mypackage" directory, putting the file in grails-app/domain/com.mypackage/Role.groovy. Any of these problems will cause the mismatch that you're seeing.

Burt Beckwith
  • 75,342
  • 5
  • 143
  • 156
  • Thanks @Burt. I really admire your work in Grails. The package name is correct. It was actually working before I made the Role, User and UserRole models extend AbstractDomain. I tested it by creating new models using s2-quickstart and it worked fine. Then when I add extends AbstractDomain to Role, then error " org.hibernate.MappingException: An association from the table user_role refers to an unmapped class: com.mypackage.Role" was thrown. Then I added extends AbstractDomain to UserRole and the earlier error came. I want to know what the real cause is. Can you please test it and tell me. – biniam Apr 03 '14 at 07:20