Skip to content

7.0.0 - Domain objects are automatically inserted when entity is fetched not in a session #14511

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
codeconsole opened this issue Mar 12, 2025 · 0 comments

Comments

@codeconsole
Copy link
Contributor

class User {
     String id 
     static mapping = {
        id generator: 'assigned'
     }
}
def graeme = new User()
graeme.id = 'graeme'
graeme.save(flush:true)

def user = User.get('graeme')
user.save(flush:true, insert:false) // results in BulkWriteError{index=0, code=11000, message='E11000 duplicate key error collection ..

This is caused because there is no common session.

https://github.com/grails/grails-data-mongodb/blob/c8287aadad3777f308ff625096171c24f0eebaf4/grails-datastore-gorm-mongodb/src/main/groovy/org/grails/datastore/mapping/mongo/engine/MongoCodecEntityPersister.groovy#L186-L188

https://github.com/grails/grails-data-mongodb/blob/c8287aadad3777f308ff625096171c24f0eebaf4/grails-datastore-gorm-mongodb/src/main/groovy/org/grails/datastore/mapping/mongo/engine/MongoCodecEntityPersister.groovy#L255-L257

returns false because session.contains(obj) returns false

The following resolves the situation, but it is not intuitive.

User.withSession { 
    def user = User.get('graeme')
    user.save(flush:true, insert:false)
}
@jdaugherty jdaugherty transferred this issue from grails/grails-data-mongodb Mar 13, 2025
@jdaugherty jdaugherty transferred this issue from apache/grails-data-mapping Apr 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants