Skip to content

Commit a8cbaff

Browse files
author
graeme
committed
fix for GRAILS-1089
git-svn-id: https://svn.codehaus.org/grails/trunk@4070 1cfb16fd-6d17-0410-8ff1-b7e8e1e2867d
1 parent e09db38 commit a8cbaff

File tree

2 files changed

+57
-4
lines changed

2 files changed

+57
-4
lines changed

src/groovy/org/codehaus/groovy/grails/plugins/orm/hibernate/HibernateGrailsPlugin.groovy

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,16 +159,31 @@ class HibernateGrailsPlugin {
159159
else {
160160
throw new MissingMethodException(dc.clazz, "addTo${collectionName}", [arg] as Object[])
161161
}
162-
if(prop.bidirectional) {
163-
obj[prop.otherSide.name] = delegate
162+
if(prop.bidirectional) {
163+
if(prop.manyToMany) {
164+
String name = prop.otherSide.name
165+
if(!obj[name]) {
166+
obj[name] = GrailsClassUtils.createConcreteCollection(prop.otherSide.type)
167+
}
168+
obj[prop.otherSide.name].add(delegate)
169+
}
170+
else {
171+
obj[prop.otherSide.name] = delegate
172+
}
164173
}
165174
delegate
166175
}
167176
dc.metaClass."removeFrom${collectionName}" = { Object arg ->
168177
if(otherDomainClass.clazz.isInstance(arg)) {
169178
delegate[prop.name]?.remove(arg)
170-
if(prop.bidirectional) {
171-
arg[prop.otherSide.name] = null
179+
if(prop.bidirectional) {
180+
if(prop.manyToMany) {
181+
String name = prop.otherSide.name
182+
arg[name]?.remove(delegate)
183+
}
184+
else {
185+
arg[prop.otherSide.name] = null
186+
}
172187
}
173188
}
174189
else {

test/groovy/org/codehaus/groovy/grails/orm/hibernate/RelationshipManagementMethodsTests.groovy

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,27 @@ class RelationshipManagementMethodsTests extends org.codehaus.groovy.grails.plug
5454

5555
}
5656

57+
void testWithManyToManyRelationship() {
58+
def bookmarkClass = ga.getDomainClass("Bookmark")
59+
def tagClass = ga.getDomainClass("Tag")
60+
61+
def bookmark = bookmarkClass.newInstance()
62+
bookmark.url='http://www.ru'
63+
64+
def tag = tagClass.newInstance()
65+
tag.name = "foo"
66+
67+
bookmark.addToTags(tag)
68+
69+
assert bookmark.tags.contains(tag)
70+
assert tag.bookmarks.contains(bookmark)
71+
72+
bookmark.removeFromTags(tag)
73+
assert !bookmark.tags.contains(tag)
74+
assert !tag.bookmarks.contains(bookmark)
75+
76+
}
77+
5778

5879
void onSetUp() {
5980

@@ -78,6 +99,23 @@ class Address {
7899
String number
79100
Person person
80101
}
102+
class Bookmark {
103+
Long id
104+
Long version
105+
106+
String url
107+
Set tags = new HashSet()
108+
static hasMany = [tags:Tag]
109+
static belongsTo = [Tag]
110+
}
111+
class Tag {
112+
Long id
113+
Long version
114+
115+
String name
116+
Set bookmarks = new HashSet()
117+
static hasMany = [bookmarks:Bookmark]
118+
}
81119
'''
82120
)
83121
}

0 commit comments

Comments
 (0)