Skip to content

One To One relationship automatic assignment issue #14652

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
hakanernam opened this issue May 23, 2016 · 0 comments
Open

One To One relationship automatic assignment issue #14652

hakanernam opened this issue May 23, 2016 · 0 comments

Comments

@hakanernam
Copy link

One to One relationship without any ownership: Setting one side in constructor results in reverse automatically set. I could reluctantly live with this if it is the officially supported behaviour, but would need to be explicitly documented so not not burn unsuspecting user, or disappear in a future release.

Sample in:

https://github.com/hakanernam/grails-onetoone

class Brother {
    String name;   
    Sister favouriteSister

    static constraints = {
            favouriteSister nullable:true
    }
    String toString(){
        "$id:$name --> ${favouriteSister?.name}"
    }
}

class Sister {
    String name;
    Brother favouriteBrother

    static constraints = {
        favouriteBrother nullable:true   
    }
     String toString(){
        "$id:$name --> ${favouriteBrother?.name}"
    }
}

and BrotherController action, setting favouriteSister in the constructor:

 @Transactional
    def setFavouriteSister(){
        Sister sister = Sister.get(params.id)
        log.info("Sister before:" + sister)
        def brother = new Brother(favouriteSister: sister);
        brother.name = "Harry";

        log.info("Brother: " + brother)
        log.info("Sister after:" + sister)

        brother.save(flush:true, failOnError:true)
        forward action:"index"
    }

INFO grails.app.controllers.onetoone.BrotherController - Sister before:2:Mary --> null
INFO grails.app.controllers.onetoone.BrotherController - Brother: null:Harry --> Mary
INFO grails.app.controllers.onetoone.BrotherController - Sister after:2:Mary --> Harry

results in automatic setting of favouriteBrother of the Sister instance. This is not what I expected, given there is no ownership in the relationship. In my actual use case, this is okay, although made me chase through my code, so not in line with least surprise principle, so I came up with the example, I may be her favourite Brother, but the reverse is not necessarily true.

brother1
sister1


In the SisterController, if we set favouriteBrother explicitly, then reverse direction is not set, which I believe the correct result.

 @Transactional
    def setFavouriteBrother(){
        Brother bro = Brother.get(params.id)
        log.info("Brother before:" + bro)

        def sister = new Sister();
        sister.name = "Jane"
        sister.favouriteBrother = bro;

        log.info("Sister: " + sister)
        log.info("Brother after:" + bro)

        sister.save(flush:true, failOnError:true)
        forward action:"index"
    }


INFO grails.app.controllers.onetoone.SisterController - Brother before:1:John --> null
INFO grails.app.controllers.onetoone.SisterController - Sister: null:Jane --> John
INFO grails.app.controllers.onetoone.SisterController - Brother after:1:John --> null

sister2

brother2

@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