Skip to content

Commit 12cc7fb

Browse files
author
graeme
committed
fix for GRAILS-1704
git-svn-id: https://svn.codehaus.org/grails/trunk@5780 1cfb16fd-6d17-0410-8ff1-b7e8e1e2867d
1 parent c067da4 commit 12cc7fb

File tree

3 files changed

+94
-6
lines changed

3 files changed

+94
-6
lines changed

src/persistence/org/codehaus/groovy/grails/orm/hibernate/cfg/GrailsDomainBinder.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,13 @@ private static void bindCollectionSecondPass(GrailsDomainClassProperty property,
289289
ColumnConfig cc = getColumnConfig(property);
290290
// Configure one-to-many
291291
if(collection.isOneToMany() ) {
292+
293+
GrailsDomainClass referenced = property.getReferencedDomainClass();
294+
if(referenced != null && !referenced.isRoot()) {
295+
// NOTE: Work around for http://opensource.atlassian.com/projects/hibernate/browse/HHH-2855
296+
collection.setWhere(RootClass.DEFAULT_DISCRIMINATOR_COLUMN_NAME + " = '"+referenced.getFullName()+"'");
297+
}
298+
292299
OneToMany oneToMany = (OneToMany)collection.getElement();
293300
String associatedClassName = oneToMany.getReferencedEntityName();
294301

@@ -1090,16 +1097,12 @@ protected static void createClassProperties(GrailsDomainClass domainClass, Persi
10901097
// if its inherited skip
10911098
if(currentGrailsProp.isInherited() && !isBidirectionalManyToOne(currentGrailsProp))
10921099
continue;
1093-
else if(domainClass.hasSubClasses() && (currentGrailsProp.isOneToOne() || currentGrailsProp.isManyToOne()))
1094-
continue;
1095-
/*if(currentGrailsProp.isManyToMany() && !currentGrailsProp.isOwningSide())
1096-
continue;*/
10971100
if (isCompositeIdProperty(gormMapping, currentGrailsProp)) continue;
10981101

10991102
if(LOG.isDebugEnabled())
11001103
LOG.debug("[GrailsDomainBinder] Binding persistent property [" + currentGrailsProp.getName() + "]");
11011104

1102-
Value value = null;
1105+
Value value;
11031106

11041107
// see if its a collection type
11051108
CollectionType collectionType = CollectionType.collectionTypeForClass( currentGrailsProp.getType() );
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/**
2+
* @author Graeme Rocher
3+
*/
4+
package org.codehaus.groovy.grails.orm.hibernate
5+
class InheritanceWithAssociationsTests extends AbstractGrailsHibernateTests {
6+
7+
protected void onSetUp() {
8+
gcl.parseClass('''
9+
class A {
10+
Long id
11+
Long version
12+
LinkToA link
13+
}
14+
class B {
15+
Long id
16+
Long version
17+
LinkToB link
18+
}
19+
class Link {
20+
Long id
21+
Long version
22+
23+
static belongsTo = Root
24+
25+
Root root
26+
}
27+
class LinkToA extends Link {
28+
Long id
29+
Long version
30+
31+
static belongsTo = A
32+
A a
33+
34+
}
35+
class LinkToB {
36+
Long id
37+
Long version
38+
39+
static belongsTo = B
40+
B b
41+
}
42+
class Root {
43+
Long id
44+
Long version
45+
46+
Set links
47+
static hasMany = [links : Link]
48+
49+
}
50+
''')
51+
}
52+
53+
54+
void testMapping() {
55+
def rootClass = ga.getDomainClass("Root")
56+
def aClass = ga.getDomainClass("A")
57+
def linkToAClass = ga.getDomainClass("LinkToA")
58+
59+
def root = rootClass.newInstance()
60+
def a = aClass.newInstance()
61+
def link = linkToAClass.newInstance()
62+
link.a = a
63+
link.root = root
64+
a.link = link
65+
66+
a.save()
67+
68+
root.addToLinks(link)
69+
root.save()
70+
session.flush()
71+
session.clear()
72+
73+
root = rootClass.clazz.get(1)
74+
assert root
75+
assertEquals 1, root.links.size()
76+
77+
link = root.links.iterator().next()
78+
assert link
79+
assert link.a
80+
81+
}
82+
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
* Created: Sep 21, 2007
66
*/
77
package org.codehaus.groovy.grails.orm.hibernate
8+
9+
import org.springframework.util.Log4jConfigurer
10+
811
class OneToManyWithInheritanceTests extends AbstractGrailsHibernateTests {
912

10-
protected void onSetUp() {
13+
protected void onSetUp() {
1114
gcl.parseClass('''
1215
class OwnerObject {
1316
Long id

0 commit comments

Comments
 (0)