Skip to content

Commit 0c1867c

Browse files
paulbatumjagregory
authored andcommitted
Added support for setting optimistic-lock on a many-to-one
1 parent 0683385 commit 0c1867c

File tree

10 files changed

+55
-1
lines changed

10 files changed

+55
-1
lines changed

src/FluentNHibernate.Testing/ConventionsTests/ApplyingToModel/ManyToOneConventionTests.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,14 @@ public void ShouldSetFormulaProperty()
162162
VerifyModel(x => x.Formula.ShouldEqual("xxx"));
163163
}
164164

165+
[Test]
166+
public void ShouldSetOptimisticLockProperty()
167+
{
168+
Convention(x => x.OptimisticLock());
169+
170+
VerifyModel(x => x.OptimisticLock.ShouldBeTrue());
171+
}
172+
165173
#region Helpers
166174

167175
private void Convention(Action<IManyToOneInstance> convention)

src/FluentNHibernate.Testing/FluentInterfaceTests/ManyToOneMutablePropertyModelGenerationTests.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,17 @@ public void IndexShouldSetColumnIndexPropertyToValue()
228228
[Test]
229229
public void FormulaIsShouldSetModelFormulaPropertyToValue()
230230
{
231-
Property()
231+
ManyToOne()
232232
.Mapping(m => m.Formula("form"))
233233
.ModelShouldMatch(x => x.Formula.ShouldEqual("form"));
234234
}
235+
236+
[Test]
237+
public void OptimisticLockShouldSetModelOptimisticLockPropertyToValue()
238+
{
239+
ManyToOne()
240+
.Mapping(m => m.OptimisticLock())
241+
.ModelShouldMatch(x => x.OptimisticLock.ShouldBeTrue());
242+
}
235243
}
236244
}

src/FluentNHibernate.Testing/MappingModel/Output/XmlManyToOneWriterTester.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,5 +148,14 @@ public void ShouldWriteFormulaAttribute()
148148

149149
testHelper.VerifyAll(writer);
150150
}
151+
152+
[Test]
153+
public void ShouldWriteOptimisticLockAttribute()
154+
{
155+
var testHelper = new XmlWriterTestHelper<ManyToOneMapping>();
156+
testHelper.Check(x => x.OptimisticLock, true).MapsToAttribute("optimistic-lock");
157+
158+
testHelper.VerifyAll(writer);
159+
}
151160
}
152161
}

src/FluentNHibernate/Conventions/Inspections/IManyToOneInspector.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@ public interface IManyToOneInspector : IAccessInspector, IExposedThroughProperty
1919
string PropertyRef { get; }
2020
bool Update { get; }
2121
bool Nullable { get; }
22+
bool OptimisticLock { get; }
2223
}
2324
}

src/FluentNHibernate/Conventions/Inspections/ManyToOneInspector.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ public bool Nullable
4949
return !mapping.Columns.First().NotNull;
5050
}
5151
}
52+
public bool OptimisticLock
53+
{
54+
get { return mapping.OptimisticLock; }
55+
}
5256

5357
public Type EntityType
5458
{

src/FluentNHibernate/Conventions/Instances/IManyToOneInstance.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public interface IManyToOneInstance : IManyToOneInspector
1818
new INotFoundInstance NotFound { get; }
1919
void Index(string index);
2020
new void Insert();
21+
new void OptimisticLock();
2122

2223
/// <summary>
2324
/// Specify the lazy behaviour of this relationship.

src/FluentNHibernate/Conventions/Instances/ManyToOneInstance.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,13 @@ public void Index(string index)
124124
nextBool = true;
125125
}
126126

127+
public new void OptimisticLock()
128+
{
129+
if (!mapping.IsSpecified("OptimisticLock"))
130+
mapping.OptimisticLock = nextBool;
131+
nextBool = true;
132+
}
133+
127134
public new void LazyLoad()
128135
{
129136
if (!mapping.IsSpecified("Lazy"))

src/FluentNHibernate/Mapping/ManyToOnePart.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,5 +364,12 @@ ColumnMapping CreateColumn(string column)
364364
{
365365
return new ColumnMapping(columnAttributes.CloneInner()) { Name = column };
366366
}
367+
368+
public ManyToOneBuilder<TOther> OptimisticLock()
369+
{
370+
mapping.OptimisticLock = nextBool;
371+
nextBool = true;
372+
return this;
373+
}
367374
}
368375
}

src/FluentNHibernate/MappingModel/ManyToOneMapping.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,12 @@ public string EntityName
110110
set { attributes.Set(x => x.EntityName, value); }
111111
}
112112

113+
public bool OptimisticLock
114+
{
115+
get { return attributes.Get(x => x.OptimisticLock); }
116+
set { attributes.Set(x => x.OptimisticLock, value); }
117+
}
118+
113119
public IDefaultableEnumerable<ColumnMapping> Columns
114120
{
115121
get { return columns; }

src/FluentNHibernate/MappingModel/Output/XmlManyToOneWriter.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ public override void ProcessManyToOne(ManyToOneMapping mapping)
6565

6666
if (mapping.HasValue(x => x.EntityName))
6767
element.WithAtt("entity-name", mapping.EntityName);
68+
69+
if (mapping.HasValue(x => x.OptimisticLock))
70+
element.WithAtt("optimstic-lock", mapping.OptimisticLock);
6871
}
6972

7073
public override void Visit(ColumnMapping columnMapping)

0 commit comments

Comments
 (0)