Skip to content
This repository has been archived by the owner on Jan 19, 2022. It is now read-only.

Commit

Permalink
fix for GRAILS-8757 "In unit tests, back reference is not set for one…
Browse files Browse the repository at this point in the history
…-to-one association"
  • Loading branch information
graemerocher committed Feb 10, 2012
1 parent ba4af68 commit de730c6
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,14 @@ else if (association.doesCascade(CascadeType.PERSIST)) {
}

if(association.doesCascade(CascadeType.PERSIST)) {

if(association.isBidirectional()) {
Association inverseSide = association.getInverseSide();
if(inverseSide != null) {
EntityAccess inverseAccess = new EntityAccess(inverseSide.getOwner(), associatedObject);
inverseAccess.setProperty(inverseSide.getName(), obj);
}
}
associationPersister.persist(associatedObject);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package org.grails.datastore.gorm

import grails.persistence.Entity
import spock.lang.Issue
import grails.gorm.tests.GormDatastoreSpec

/**
*/
class HasOneSetInverseSideSpec extends GormDatastoreSpec{

@Issue('GRAILS-8757')
void "Test that saving a one-to-one automatically sets the inverse side"() {
when:"A bidirectional one-to-one is saved"
def address = new HouseAddress(street:"Street 001")
def house = new House(name:"Some house", address: address)

house.save(flush:true)

then:"The inverse side is autmotically set"
house.id != null
address.house != null

when:"The association is queried"
session.clear()
house = House.get(house.id)

then:"The data model is valid"
house.id != null
house.address != null
house.address.house != null


}

@Override
List getDomainClasses() {
[House, HouseAddress]
}


}

@Entity
class House {
Long id
String name

HouseAddress address
static hasOne = [address: HouseAddress]
}

@Entity
class HouseAddress {
Long id
String street

House house
}

0 comments on commit de730c6

Please sign in to comment.