Skip to content

Commit bf6fa53

Browse files
fixup! Support mixed formulas and columns
Throw on invalid usages of columns and formulas attributes and elements
1 parent 280efad commit bf6fa53

File tree

7 files changed

+65
-54
lines changed

7 files changed

+65
-54
lines changed

src/NHibernate.Test/NHSpecificTest/NH1007/Mappings.hbm.xml

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,15 @@
66
<property name="Name" />
77
</class>
88

9-
<!--
10-
Mapping to demonstrate that the 'column' attribute takes precedence over a 'column' element - and, for consistency,
11-
the same behaviour applies to the generator attribute/element
9+
<!--
10+
Mapping to demonstrate that the 'generator' attribute takes precedence over a 'generator' element
1211
-->
1312
<class name="Employer2">
1413
<id name="Id" generator="guid">
1514
<generator class="invalid_generator_class" />
1615
</id>
17-
18-
<property name="Name" column="Name">
19-
<column name="InvalidColumnName" />
20-
</property>
16+
17+
<property name="Name" />
2118
</class>
2219

23-
</hibernate-mapping>
20+
</hibernate-mapping>

src/NHibernate/Cfg/MappingSchema/HbmElement.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,15 @@ public IEnumerable<object> ColumnsAndFormulas
8080
{
8181
get
8282
{
83-
if (Items != null)
84-
return Items;
85-
// Avoid a possible breaking change for mapping having left a column attribute along with a formula one.
86-
// This is a mapping error, but previous implementation was silently ignoring the column attribute in
87-
// such case.
88-
if (!string.IsNullOrEmpty(formula))
89-
return AsFormulas();
90-
return AsColumns();
83+
if (Items != null && (!string.IsNullOrEmpty(column) || !string.IsNullOrEmpty(formula)))
84+
throw new MappingException(
85+
$"On an element: specifying columns or formulas with both attributes and xml sub-elements is " +
86+
$"invalid. Please use only xml sub-elements, or only one of them as attribute");
87+
if (!string.IsNullOrEmpty(column) && !string.IsNullOrEmpty(formula))
88+
throw new MappingException(
89+
$"On an element: specifying both column and formula attributes is invalid. Please " +
90+
$"specify only one of them, or use xml sub-elements");
91+
return Items ?? AsColumns().Cast<object>().Concat(AsFormulas().Cast<object>());
9192
}
9293
}
9394
}

src/NHibernate/Cfg/MappingSchema/HbmManyToMany.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,16 @@ public IEnumerable<object> ColumnsAndFormulas
8383
{
8484
get
8585
{
86-
if (Items != null)
87-
return Items;
88-
// Avoid a possible breaking change for mapping having left a column attribute along with a formula one.
89-
// This is a mapping error, but previous implementation was silently ignoring the column attribute in
90-
// such case.
91-
if (!string.IsNullOrEmpty(formula))
92-
return AsFormulas();
93-
return AsColumns();
86+
if (Items != null && (!string.IsNullOrEmpty(column) || !string.IsNullOrEmpty(formula)))
87+
throw new MappingException(
88+
$"On a many-to-many targeting {EntityName}: specifying columns or formulas with both " +
89+
$"attributes and sub-elements is invalid. Please use only sub-elements, or only one of them " +
90+
$"as attribute");
91+
if (!string.IsNullOrEmpty(column) && !string.IsNullOrEmpty(formula))
92+
throw new MappingException(
93+
$"On a many-to-many targeting {EntityName}: specifying both column and formula attributes is " +
94+
$"invalid. Please specify only one of them, or use sub-elements");
95+
return Items ?? AsColumns().Cast<object>().Concat(AsFormulas().Cast<object>());
9496
}
9597
}
9698
}

src/NHibernate/Cfg/MappingSchema/HbmManyToOne.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -118,16 +118,23 @@ public HbmNotFoundMode NotFoundMode
118118
[XmlIgnore]
119119
public IEnumerable<object> ColumnsAndFormulas
120120
{
121-
// when Items is empty the column attribute AND formula attribute will be used
122-
// and it may cause an issue (breaking change)
123-
// On the other hand it work properly when a mixing between <formula> and <column> tags are used
124-
// respecting the order used in the mapping to map multi-columns id.
125-
get { return Items ?? Columns.Cast<object>().Concat(Formulas.Cast<object>()); }
121+
get
122+
{
123+
if (Items != null && (!string.IsNullOrEmpty(column) || !string.IsNullOrEmpty(formula)))
124+
throw new MappingException(
125+
$"On {Name} many-to-one: specifying columns or formulas with both attributes and " +
126+
$"sub-elements is invalid. Please use only sub-elements, or only one of them as attribute");
127+
if (!string.IsNullOrEmpty(column) && !string.IsNullOrEmpty(formula))
128+
throw new MappingException(
129+
$"On {Name} many-to-one: specifying both column and formula attributes is invalid. Please " +
130+
$"specify only one of them, or use sub-elements");
131+
return Items ?? AsColumns().Cast<object>().Concat(AsFormulas().Cast<object>());
132+
}
126133
}
127134

128135
public HbmLaziness? Lazy
129136
{
130137
get { return lazySpecified ? lazy : (HbmLaziness?) null;}
131138
}
132139
}
133-
}
140+
}

src/NHibernate/Cfg/MappingSchema/HbmMapKey.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,14 +73,15 @@ public IEnumerable<object> ColumnsAndFormulas
7373
{
7474
get
7575
{
76-
if (Items != null)
77-
return Items;
78-
// Avoid a possible breaking change for mapping having left a column attribute along with a formula one.
79-
// This is a mapping error, but previous implementation was silently ignoring the column attribute in
80-
// such case.
81-
if (!string.IsNullOrEmpty(formula))
82-
return AsFormulas();
83-
return AsColumns();
76+
if (Items != null && (!string.IsNullOrEmpty(column) || !string.IsNullOrEmpty(formula)))
77+
throw new MappingException(
78+
$"On a map-key: specifying columns or formulas with both attributes and sub-elements is " +
79+
$"invalid. Please use only sub-elements, or only one of them as attribute");
80+
if (!string.IsNullOrEmpty(column) && !string.IsNullOrEmpty(formula))
81+
throw new MappingException(
82+
$"On a map-key: specifying both column and formula attributes is invalid. Please specify " +
83+
$"only one of them, or use sub-elements");
84+
return Items ?? AsColumns().Cast<object>().Concat(AsFormulas().Cast<object>());
8485
}
8586
}
8687
}

src/NHibernate/Cfg/MappingSchema/HbmMapKeyManyToMany.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -82,14 +82,16 @@ public IEnumerable<object> ColumnsAndFormulas
8282
{
8383
get
8484
{
85-
if (Items != null)
86-
return Items;
87-
// Avoid a possible breaking change for mapping having left a column attribute along with a formula one.
88-
// This is a mapping error, but previous implementation was silently ignoring the column attribute in
89-
// such case.
90-
if (!string.IsNullOrEmpty(formula))
91-
return AsFormulas();
92-
return AsColumns();
85+
if (Items != null && (!string.IsNullOrEmpty(column) || !string.IsNullOrEmpty(formula)))
86+
throw new MappingException(
87+
$"On a map-key-many-to-many targeting {EntityName}: specifying columns or formulas with both " +
88+
$"attributes and sub-elements is invalid. Please use only sub-elements, or only one of them " +
89+
$"as attribute");
90+
if (!string.IsNullOrEmpty(column) && !string.IsNullOrEmpty(formula))
91+
throw new MappingException(
92+
$"On a map-key-many-to-many targeting {EntityName}: specifying both column and formula " +
93+
$"attributes is invalid. Please specify only one of them, or use sub-elements");
94+
return Items ?? AsColumns().Cast<object>().Concat(AsFormulas().Cast<object>());
9395
}
9496
}
9597
}

src/NHibernate/Cfg/MappingSchema/HbmProperty.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,14 +113,15 @@ public IEnumerable<object> ColumnsAndFormulas
113113
{
114114
get
115115
{
116-
if (Items != null)
117-
return Items;
118-
// Avoid a possible breaking change for mapping having left a column attribute along with a formula one.
119-
// This is a mapping error, but previous implementation was silently ignoring the column attribute in
120-
// such case.
121-
if (!string.IsNullOrEmpty(formula))
122-
return AsFormulas();
123-
return AsColumns();
116+
if (Items != null && (!string.IsNullOrEmpty(column) || !string.IsNullOrEmpty(formula)))
117+
throw new MappingException(
118+
$"On {Name} property: specifying columns or formulas with both attributes and " +
119+
$"sub-elements is invalid. Please use only sub-elements, or only one of them as attribute");
120+
if (!string.IsNullOrEmpty(column) && !string.IsNullOrEmpty(formula))
121+
throw new MappingException(
122+
$"On {Name} property: specifying both column and formula attributes is invalid. Please " +
123+
$"specify only one of them, or use sub-elements");
124+
return Items ?? AsColumns().Cast<object>().Concat(AsFormulas().Cast<object>());
124125
}
125126
}
126127
}

0 commit comments

Comments
 (0)