Skip to content

Commit fc8e775

Browse files
committed
GRAILS-2773 applied Jean-Noel's patch
1 parent 3ff0071 commit fc8e775

File tree

3 files changed

+72
-7
lines changed

3 files changed

+72
-7
lines changed

src/java/org/codehaus/groovy/grails/commons/DefaultGrailsDomainClassProperty.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -539,25 +539,39 @@ public ComponentDomainClass(Class<?> type) {
539539
PropertyDescriptor[] descriptors = getPropertyDescriptors();
540540

541541
List tmp = getPropertyValue(GrailsDomainClassProperty.TRANSIENT, List.class);
542-
if (tmp!=null) transients = tmp;
543-
properties = createDomainClassProperties(this,descriptors);
542+
if (tmp != null) transients = tmp;
543+
properties = createDomainClassProperties(descriptors);
544544
constraints = GrailsDomainConfigurationUtil.evaluateConstraints(getClazz(), properties);
545545
DomainClassGrailsPlugin.registerConstraintsProperty(getMetaClass(), this);
546546
}
547547

548-
private GrailsDomainClassProperty[] createDomainClassProperties(
549-
@SuppressWarnings("hiding") ComponentDomainClass type, PropertyDescriptor[] descriptors) {
548+
private GrailsDomainClassProperty[] createDomainClassProperties(PropertyDescriptor[] descriptors) {
550549

551550
List<DefaultGrailsDomainClassProperty> props = new ArrayList<DefaultGrailsDomainClassProperty>();
551+
Collection<String> embeddedNames = getEmbeddedList();
552552
for (int i = 0; i < descriptors.length; i++) {
553553
PropertyDescriptor descriptor = descriptors[i];
554554
if (isPersistentProperty(descriptor)) {
555-
props.add(new DefaultGrailsDomainClassProperty(type,descriptor));
555+
DefaultGrailsDomainClassProperty property = new DefaultGrailsDomainClassProperty(
556+
this, descriptor);
557+
props.add(property);
558+
if (embeddedNames.contains(property.getName())) {
559+
property.setEmbedded(true);
560+
}
556561
}
557562
}
558563
return props.toArray(new GrailsDomainClassProperty[props.size()]);
559564
}
560565

566+
@SuppressWarnings("unchecked")
567+
private Collection<String> getEmbeddedList() {
568+
Object potentialList = GrailsClassUtils.getStaticPropertyValue(getClazz(), "embedded");
569+
if (potentialList instanceof Collection) {
570+
return (Collection<String>)potentialList;
571+
}
572+
return Collections.emptyList();
573+
}
574+
561575
private boolean isPersistentProperty(PropertyDescriptor descriptor) {
562576
String propertyName = descriptor.getName();
563577
return GrailsDomainConfigurationUtil.isNotConfigurational(descriptor) && !transients.contains(propertyName);

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,7 @@ private static void bindDependentKeyValue(GrailsDomainClassProperty property, De
743743
GrailsDomainClass refDomainClass = property.getDomainClass();
744744
final Mapping mapping = getMapping(refDomainClass.getClazz());
745745
if ((shouldCollectionBindWithJoinColumn(property) && hasCompositeIdentifier(mapping)) ||
746-
(hasCompositeIdentifier(mapping) && property.isManyToMany())) {
746+
(hasCompositeIdentifier(mapping) && property.isManyToMany())) {
747747
CompositeIdentity ci = (CompositeIdentity) mapping.getIdentity();
748748
bindCompositeIdentifierToManyToOne(property, key, ci, refDomainClass, EMPTY_PATH);
749749
}
@@ -1663,7 +1663,7 @@ else if (!"ordinal".equalsIgnoreCase(enumType)) {
16631663
Column column = new Column();
16641664

16651665
if (property.getDomainClass().isRoot()) {
1666-
column.setNullable(property.isOptional());
1666+
column.setNullable(property.isOptional());
16671667
} else {
16681668
Mapping mapping = getMapping(property.getDomainClass());
16691669
if(mapping == null || mapping.getTablePerHierarchy()) {
@@ -1795,6 +1795,10 @@ else if (currentGrailsProp.isManyToOne()) {
17951795
bindManyToOne(currentGrailsProp, (ManyToOne) value, path, mappings);
17961796
}
17971797
}
1798+
else if (currentGrailsProp.isEmbedded()) {
1799+
value = new Component(persistentClass);
1800+
bindComponent((Component) value, currentGrailsProp, true, mappings);
1801+
}
17981802
else {
17991803
if (LOG.isDebugEnabled())
18001804
LOG.debug("[GrailsDomainBinder] Binding property [" + currentGrailsProp.getName() + "] as SimpleValue");

src/test/org/codehaus/groovy/grails/orm/hibernate/ComponentMappingTests.groovy

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,30 @@ class ComponentMappingTests extends AbstractGrailsHibernateTests {
6161
assertEquals "3345243", p.homeAddress.postCode
6262
}
6363

64+
void testJira2773() {
65+
66+
def RegularPayment2773 = ga.getDomainClass('RegularPayment2773').clazz
67+
def payment = RegularPayment2773.newInstance()
68+
69+
def RegularPaymentPart2773 = ga.getDomainClass('RegularPaymentPart2773').clazz
70+
def paymentPart = RegularPaymentPart2773.newInstance()
71+
72+
def Money2773 = ga.getDomainClass('Money2773').clazz
73+
def money = Money2773.newInstance()
74+
75+
money.value = 123
76+
payment.regular = paymentPart
77+
paymentPart.amount = money
78+
79+
assertNotNull payment.save()
80+
81+
session.flush()
82+
session.clear()
83+
84+
payment = RegularPayment2773.list()[0]
85+
assertEquals 123, payment.regular.amount.value
86+
}
87+
6488
protected void onSetUp() {
6589
gcl.parseClass '''
6690
class ComponentMappingPerson {
@@ -97,6 +121,29 @@ class ComponentMappingPrice {
97121
BigDecimal amount
98122
Integer quantity
99123
}
124+
125+
class RegularPayment2773 {
126+
Long id
127+
Long version
128+
129+
RegularPaymentPart2773 regular
130+
static embedded = ['regular']
131+
}
132+
133+
class RegularPaymentPart2773 {
134+
Long id
135+
Long version
136+
137+
Money2773 amount
138+
static embedded = ['amount']
139+
}
140+
141+
class Money2773 {
142+
Long id
143+
Long version
144+
145+
BigDecimal value
146+
}
100147
'''
101148
}
102149
}

0 commit comments

Comments
 (0)