Search code examples
hibernategrailsgrails-ormspock

How can I avoid Found two representations of same collection


Hello I am facing a problem during a test and just during my integration test.

Found two representations of same collection: ClientPasswordPolicy.userCategoriesForProxyDuration;

In one of my domains I have the following thing :

Map<String, String> userCategoriesForProxyDuration

that is mapped by like :

userCategoriesForProxyDuration joinTable: 

my test looks like :

        Client client0 = Client.findByName('client0')
        UserCategory userCategory = UserCategory.build(value:'TEST_GUILHERME')

        Client client = Client.build(name: 'Monkey')
        ClientPasswordPolicy policy = ClientPasswordPolicy.build(client:client)

        client.save(flush:true)

        policy.userCategoriesForProxyDuration = ["TEST_GUILHERME":"36"]
        policy.addToUserCategoriesNeedApproval(userCategory)
        policy.proxyEnabled = true

        policy.save(flush:true,failOnError:true,insert:true)

        User user = User.build(username: "Test1", password: "password", client: client0)

        Team team = Team.build(name: 'myTeamMonkey', client: client, members: [user])

        ClientPasswordPolicy policy1 = ClientPasswordPolicy.build(client:Client.build(name:'Maria'),proxyApproverEmailAddress:'[email protected]')
        Client client1 = Client.findByName('Maria')
        Team team1 = Team.build(name: 'myTeamMaria1', client: client1, members: [user])

but in the line that I am creating the second policy I am getting the error, I did some tries and for example if I do like:

     ClientPasswordPolicy.findAll()

twice in the second time will get error anyway, the same error. so I am afraid that I don't know why the policy is not been flushed into the transaction and the transaction is holding, that is why I am using the flush after the save even if I am using the build to create my domains.

I found some things like, during the validation of the ClientPasswordPolicy we do things like this :

 userCategoriesForProxyDuration nullable: true, validator: { approvals, object ->
        if(object.proxyEnabled && !approvals) {
            return ['invalid.proxy.user.category.required']
        }
        for (approval in approvals) {
            if (!(approval.key in UserCategory.list().value)) {
                 return ['invalid.proxy.approval.userType']
            }
            try {
                Integer.parseInt(approval.value)
            } catch (NumberFormatException e) {
                return ['invalid.proxy.approval.duration']
            }
        }

And if I comment out won't have any problem I am afraid of this UserCategory.list() is causing the trouble but I am not sure what to do with this, I tried to use during the save(validate:false) won't work.


Solution

  • The solution that I found was saving and flushing everything into the test, because the validations where checking the database to validate and was messing with the hibernate.