Skip to content
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

many-to-many associations misbehave when the belongsTo property is missing. No errors, but no data is saved. #1677

Open
4 tasks done
tircnf opened this issue Sep 16, 2022 · 0 comments

Comments

@tircnf
Copy link

tircnf commented Sep 16, 2022

Task List

  • Steps to reproduce provided
  • Stacktrace (if present) provided
  • Example that reproduces the problem uploaded to Github
  • Full description of the issue provided (see below)

Steps to Reproduce

  1. Create two domain classes with a hasMany relationship between the two
  2. Try and save a relationship

Expected Behaviour

Either an exception should be thrown warning that the many-to-many is misconfigured. (old grails 2 behavior).
Or the relationship data should be saved. (perhaps modeled as two different one-to-many relationships and two different join tables).

Actual Behaviour

No exception is thrown.
In Memory relationship data is stored, and back references are set up along with back references.
No relationship data is stored.

Environment Information

  • Operating System: windows/linux
  • GORM Version: 7.3.2
  • Grails Version (if using Grails): 5.2.3
  • JDK Version: 1.8

Example Application

https://github.com/tircnf/manytomany

The only real file in the project is a hibernateSpec. The Spec contains two entities with one-to-many mappings to each other.

@Entity
class Person {
    String name
    static hasMany = [pets: Pet]
}

@Entity
class Pet {
    String name
    static hasMany = [owners: Person]
}

Here is the test that shows the problem.

    void "Expect associations work"() {
        given: "A Person and Pet"
        Person steve = new Person(name: "steve").save(flush:true, failOnError:true)
        Pet spot = new Pet(name: "spot").save(flush:true, failOnError:true)

        when: "I associate them"
        steve.addToPets(spot)
        println "Saving... updates the version number on both person and pet, but doesn't write the association data"
        steve.save(flush:true, failOnError:true)
        println "Flush done."

        then: "It got persisted in memory"
        steve.pets.size()==1
        steve.pets[0].name=="spot"

        and: "back references are set up"
        spot.owners.size()==1
        spot.owners[0]==steve

        when: "if I reload from database"
        sessionFactory.currentSession.clear()
        Person steve2 = Person.get(steve.id)

        then: "those relationships are gone"
        steve2.pets.size()==1
        steve2.pets[0].name=="spot"
    }

I discovered this when I inadvertently tried to create two separate one-to-many relationships between two domain Classes. I was not looking to set up a many-to-many.

My original test didn't clear the session, so loaded the domain items from the hibernate cache, and they still had accesses to the relationships. After the session was cleared the associations just disappeared.

with sql logging turned on, you can verity that even though person/pet are getting the versions updated, no relationship data is stored.

At the very least having the old exception
No owner defined between domain classes [class manytomay.Person] and [class manytomay.Pet] in a many-to-many relationship. Example: static belongsTo = manytomay.Pet

would have made my error really obvious.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant