Skip to content

Commit 52df4ee

Browse files
committed
- Checked full porting (entityMode)
- Reformatting SVN: trunk@3483
1 parent 89994bc commit 52df4ee

10 files changed

+85
-131
lines changed

src/NHibernate/Engine/BatchFetchQueue.cs

+7-17
Original file line numberDiff line numberDiff line change
@@ -134,16 +134,13 @@ public void RemoveBatchLoadableEntityKey(EntityKey key)
134134
/// <param name="collectionPersister">The persister for the collection role.</param>
135135
/// <param name="id">A key that must be included in the batch fetch</param>
136136
/// <param name="batchSize">the maximum number of keys to return</param>
137+
/// <param name="entityMode">The entity mode.</param>
137138
/// <returns>an array of collection keys, of length batchSize (padded with nulls)</returns>
138-
public object[] GetCollectionBatch(
139-
ICollectionPersister collectionPersister,
140-
object id,
141-
int batchSize)
139+
public object[] GetCollectionBatch(ICollectionPersister collectionPersister, object id, int batchSize, EntityMode entityMode)
142140
{
143141
object[] keys = new object[batchSize];
144142
keys[0] = id;
145143
int i = 1;
146-
//int count = 0;
147144
int end = -1;
148145
bool checkForEnd = false;
149146

@@ -163,14 +160,14 @@ public object[] GetCollectionBatch(
163160

164161
//if ( end == -1 && count > batchSize*10 ) return keys; //try out ten batches, max
165162

166-
bool isEqual = collectionPersister.KeyType.IsEqual(id, ce.LoadedKey, EntityMode.Poco);
163+
bool isEqual = collectionPersister.KeyType.IsEqual(id, ce.LoadedKey, entityMode, collectionPersister.Factory);
167164

168165
if (isEqual)
169166
{
170167
end = i;
171168
//checkForEnd = false;
172169
}
173-
else if (!IsCached(ce.LoadedKey, collectionPersister))
170+
else if (!IsCached(ce.LoadedKey, collectionPersister, entityMode))
174171
{
175172
keys[i++] = ce.LoadedKey;
176173
//count++;
@@ -259,19 +256,12 @@ private bool IsCached(
259256
return false;
260257
}
261258

262-
private bool IsCached(
263-
object collectionKey,
264-
ICollectionPersister persister)
259+
private bool IsCached(object collectionKey, ICollectionPersister persister, EntityMode entityMode)
265260
{
266261
if (persister.HasCache)
267262
{
268-
CacheKey cacheKey = new CacheKey(
269-
collectionKey,
270-
persister.KeyType,
271-
persister.Role,
272-
EntityMode.Poco,
273-
context.Session.Factory
274-
);
263+
CacheKey cacheKey =
264+
new CacheKey(collectionKey, persister.KeyType, persister.Role, entityMode, context.Session.Factory);
275265
return persister.Cache.Cache.Get(cacheKey) != null;
276266
}
277267
return false;

src/NHibernate/Loader/Collection/BasicCollectionJoinWalker.cs

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public BasicCollectionJoinWalker(IQueryableCollection collectionPersister, int b
2727

2828
IList<OuterJoinableAssociation> allAssociations = new List<OuterJoinableAssociation>(associations);
2929

30+
// NH Different behavior : passing enabledFilters instead empty-filter
3031
allAssociations.Add(
3132
new OuterJoinableAssociation(collectionPersister.CollectionType, null, null, alias, JoinType.LeftOuterJoin, Factory,
3233
enabledFilters));

src/NHibernate/Loader/Collection/BasicCollectionLoader.cs

+18-29
Original file line numberDiff line numberDiff line change
@@ -6,42 +6,31 @@
66

77
namespace NHibernate.Loader.Collection
88
{
9+
/// <summary>
10+
/// Loads a collection of values or a many-to-many association.
11+
/// </summary>
12+
/// <remarks>
13+
/// The collection persister must implement <tt>QueryableCOllection<tt>. For
14+
/// other collections, create a customized subclass of <tt>Loader</tt>.
15+
/// </remarks>
16+
/// <seealso cref="OneToManyLoader"/>
917
public class BasicCollectionLoader : CollectionLoader
1018
{
11-
private static readonly ILog log = LogManager.GetLogger(typeof(BasicCollectionLoader));
19+
private static readonly ILog log = LogManager.GetLogger(typeof (BasicCollectionLoader));
1220

13-
public BasicCollectionLoader(
14-
IQueryableCollection collectionPersister,
15-
ISessionFactoryImplementor session,
16-
IDictionary<string, IFilter> enabledFilters)
17-
: this(collectionPersister, 1, session, enabledFilters)
18-
{
19-
}
21+
public BasicCollectionLoader(IQueryableCollection collectionPersister, ISessionFactoryImplementor session,
22+
IDictionary<string, IFilter> enabledFilters)
23+
: this(collectionPersister, 1, session, enabledFilters) {}
2024

21-
public BasicCollectionLoader(
22-
IQueryableCollection collectionPersister,
23-
int batchSize,
24-
ISessionFactoryImplementor factory,
25-
IDictionary<string, IFilter> enabledFilters)
26-
: this(collectionPersister, batchSize, null, factory, enabledFilters)
27-
{
28-
}
25+
public BasicCollectionLoader(IQueryableCollection collectionPersister, int batchSize,
26+
ISessionFactoryImplementor factory, IDictionary<string, IFilter> enabledFilters)
27+
: this(collectionPersister, batchSize, null, factory, enabledFilters) {}
2928

30-
protected BasicCollectionLoader(
31-
IQueryableCollection collectionPersister,
32-
int batchSize,
33-
SqlString subquery,
34-
ISessionFactoryImplementor factory,
35-
IDictionary<string, IFilter> enabledFilters)
29+
protected BasicCollectionLoader(IQueryableCollection collectionPersister, int batchSize, SqlString subquery,
30+
ISessionFactoryImplementor factory, IDictionary<string, IFilter> enabledFilters)
3631
: base(collectionPersister, factory, enabledFilters)
3732
{
38-
JoinWalker walker = new BasicCollectionJoinWalker(
39-
collectionPersister,
40-
batchSize,
41-
subquery,
42-
factory,
43-
enabledFilters
44-
);
33+
JoinWalker walker = new BasicCollectionJoinWalker(collectionPersister, batchSize, subquery, factory, enabledFilters);
4534
InitFromWalker(walker);
4635

4736
PostInstantiate();

src/NHibernate/Loader/Collection/BatchingCollectionInitializer.cs

+13-15
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,15 @@ namespace NHibernate.Loader.Collection
99
/// <summary>
1010
/// "Batch" loads collections, using multiple foreign key values in the SQL Where clause
1111
/// </summary>
12+
/// <seealso cref="BasicCollectionLoader"/>
13+
/// <seealso cref="OneToManyLoader"/>
1214
public class BatchingCollectionInitializer : ICollectionInitializer
1315
{
1416
private readonly Loader[] loaders;
1517
private readonly int[] batchSizes;
1618
private readonly ICollectionPersister collectionPersister;
1719

18-
public BatchingCollectionInitializer(
19-
ICollectionPersister collectionPersister,
20-
int[] batchSizes,
21-
Loader[] loaders)
20+
public BatchingCollectionInitializer(ICollectionPersister collectionPersister, int[] batchSizes, Loader[] loaders)
2221
{
2322
this.loaders = loaders;
2423
this.batchSizes = batchSizes;
@@ -27,7 +26,9 @@ public BatchingCollectionInitializer(
2726

2827
public void Initialize(object id, ISessionImplementor session)
2928
{
30-
object[] batch = session.PersistenceContext.BatchFetchQueue.GetCollectionBatch(collectionPersister, id, batchSizes[0]);
29+
object[] batch =
30+
session.PersistenceContext.BatchFetchQueue.GetCollectionBatch(collectionPersister, id, batchSizes[0],
31+
session.EntityMode);
3132

3233
for (int i = 0; i < batchSizes.Length; i++)
3334
{
@@ -44,11 +45,9 @@ public void Initialize(object id, ISessionImplementor session)
4445
loaders[batchSizes.Length - 1].LoadCollection(session, id, collectionPersister.KeyType);
4546
}
4647

47-
public static ICollectionInitializer CreateBatchingOneToManyInitializer(
48-
OneToManyPersister persister,
49-
int maxBatchSize,
50-
ISessionFactoryImplementor factory,
51-
IDictionary<string, IFilter> enabledFilters)
48+
public static ICollectionInitializer CreateBatchingOneToManyInitializer(OneToManyPersister persister, int maxBatchSize,
49+
ISessionFactoryImplementor factory,
50+
IDictionary<string, IFilter> enabledFilters)
5251
{
5352
if (maxBatchSize > 1)
5453
{
@@ -67,11 +66,10 @@ public static ICollectionInitializer CreateBatchingOneToManyInitializer(
6766
}
6867
}
6968

70-
public static ICollectionInitializer CreateBatchingCollectionInitializer(
71-
IQueryableCollection persister,
72-
int maxBatchSize,
73-
ISessionFactoryImplementor factory,
74-
IDictionary<string, IFilter> enabledFilters)
69+
public static ICollectionInitializer CreateBatchingCollectionInitializer(IQueryableCollection persister,
70+
int maxBatchSize,
71+
ISessionFactoryImplementor factory,
72+
IDictionary<string, IFilter> enabledFilters)
7573
{
7674
if (maxBatchSize > 1)
7775
{

src/NHibernate/Loader/Collection/CollectionJoinWalker.cs

+12-16
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,29 @@ namespace NHibernate.Loader.Collection
1414
public abstract class CollectionJoinWalker : JoinWalker
1515
{
1616
public CollectionJoinWalker(ISessionFactoryImplementor factory, IDictionary<string, IFilter> enabledFilters)
17-
: base(factory, enabledFilters)
18-
{
19-
}
17+
: base(factory, enabledFilters) {}
2018

21-
protected SqlStringBuilder WhereString(string alias, string[] columnNames, SqlString subselect,
22-
int batchSize)
19+
protected SqlStringBuilder WhereString(string alias, string[] columnNames, SqlString subselect, int batchSize)
2320
{
2421
if (subselect == null)
2522
{
2623
return WhereString(alias, columnNames, batchSize);
2724
}
2825
else
2926
{
30-
SqlStringBuilder buf = new SqlStringBuilder();
27+
string columnsJoin = StringHelper.Join(StringHelper.CommaSpace, StringHelper.Qualify(alias, columnNames));
3128

29+
SqlStringBuilder buf = new SqlStringBuilder();
3230
if (columnNames.Length > 1)
33-
buf.Add("(");
34-
35-
buf.Add(StringHelper.Join(StringHelper.CommaSpace, StringHelper.Qualify(alias, columnNames)));
36-
37-
if (columnNames.Length > 1)
38-
buf.Add(")");
31+
{
32+
buf.Add("(").Add(columnsJoin).Add(")");
33+
}
34+
else
35+
{
36+
buf.Add(columnsJoin);
37+
}
3938

40-
buf.Add(" in ")
41-
.Add("(")
42-
.Add(subselect)
43-
.Add(")");
39+
buf.Add(" in ").Add("(").Add(subselect).Add(")");
4440
return buf;
4541
}
4642
}

src/NHibernate/Loader/Collection/CollectionLoader.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ public class CollectionLoader : OuterJoinLoader, ICollectionInitializer
1414
{
1515
private readonly IQueryableCollection collectionPersister;
1616

17-
public CollectionLoader(IQueryableCollection persister, ISessionFactoryImplementor factory, IDictionary<string, IFilter> enabledFilters)
18-
: base(factory, enabledFilters)
17+
public CollectionLoader(IQueryableCollection persister, ISessionFactoryImplementor factory,
18+
IDictionary<string, IFilter> enabledFilters) : base(factory, enabledFilters)
1919
{
2020
collectionPersister = persister;
2121
}
@@ -25,14 +25,14 @@ protected internal override bool IsSubselectLoadingEnabled
2525
get { return HasSubselectLoadableCollections(); }
2626
}
2727

28-
public virtual void Initialize(object id, ISessionImplementor session)
28+
protected IType KeyType
2929
{
30-
LoadCollection(session, id, KeyType);
30+
get { return collectionPersister.KeyType; }
3131
}
3232

33-
protected IType KeyType
33+
public virtual void Initialize(object id, ISessionImplementor session)
3434
{
35-
get { return collectionPersister.KeyType; }
35+
LoadCollection(session, id, KeyType);
3636
}
3737

3838
public override string ToString()

src/NHibernate/Loader/Collection/ICollectionInitializer.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@ namespace NHibernate.Loader.Collection
55
/// <summary>
66
/// An interface for collection loaders
77
/// </summary>
8+
/// <seealso cref="BasicCollectionLoader"/>
9+
/// <seealso cref="OneToManyLoader"/>
810
public interface ICollectionInitializer
911
{
1012
/// <summary>
1113
/// Initialize the given collection
1214
/// </summary>
13-
/// <param name="id"></param>
14-
/// <param name="session"></param>
1515
void Initialize(object id, ISessionImplementor session);
1616
}
1717
}

src/NHibernate/Loader/Collection/OneToManyJoinWalker.cs

+13-16
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,24 @@ protected override bool IsDuplicateAssociation(string foreignKeyTable, string[]
1919
{
2020
//disable a join back to this same association
2121
bool isSameJoin = oneToManyPersister.TableName.Equals(foreignKeyTable)
22-
&& CollectionHelper.CollectionEquals<string>(foreignKeyColumns, oneToManyPersister.KeyColumnNames);
22+
&& CollectionHelper.CollectionEquals<string>(foreignKeyColumns, oneToManyPersister.KeyColumnNames);
2323
return isSameJoin || base.IsDuplicateAssociation(foreignKeyTable, foreignKeyColumns);
2424
}
2525

2626
public OneToManyJoinWalker(IQueryableCollection oneToManyPersister, int batchSize, SqlString subquery,
27-
ISessionFactoryImplementor factory, IDictionary<string, IFilter> enabledFilters)
27+
ISessionFactoryImplementor factory, IDictionary<string, IFilter> enabledFilters)
2828
: base(factory, enabledFilters)
2929
{
3030
this.oneToManyPersister = oneToManyPersister;
31-
IOuterJoinLoadable elementPersister = (IOuterJoinLoadable)oneToManyPersister.ElementPersister;
31+
IOuterJoinLoadable elementPersister = (IOuterJoinLoadable) oneToManyPersister.ElementPersister;
3232
string alias = GenerateRootAlias(oneToManyPersister.Role);
3333

3434
WalkEntityTree(elementPersister, alias);
3535

3636
IList<OuterJoinableAssociation> allAssociations = new List<OuterJoinableAssociation>(associations);
3737
allAssociations.Add(
3838
new OuterJoinableAssociation(oneToManyPersister.CollectionType, null, null, alias, JoinType.LeftOuterJoin, Factory,
39-
new CollectionHelper.EmptyMapClass<string, IFilter>()));
39+
new CollectionHelper.EmptyMapClass<string, IFilter>()));
4040

4141
InitPersisters(allAssociations, LockMode.None);
4242
InitStatementString(elementPersister, alias, batchSize, subquery);
@@ -56,23 +56,20 @@ private void InitStatementString(IOuterJoinLoadable elementPersister, string ali
5656

5757
JoinFragment ojf = MergeOuterJoins(associations);
5858
SqlSelectBuilder select =
59-
new SqlSelectBuilder(Factory)
60-
.SetSelectClause(
61-
oneToManyPersister.SelectFragment(null, null, alias, Suffixes[joins], CollectionSuffixes[0], true)
62-
+ SelectString(associations)
63-
)
64-
.SetFromClause(
65-
elementPersister.FromTableFragment(alias) + elementPersister.FromJoinFragment(alias, true, true)
66-
)
67-
.SetWhereClause(whereString.ToSqlString())
68-
.SetOuterJoins(
69-
ojf.ToFromFragmentString,ojf.ToWhereFragmentString + elementPersister.WhereJoinFragment(alias, true, true)
70-
);
59+
new SqlSelectBuilder(Factory).SetSelectClause(
60+
oneToManyPersister.SelectFragment(null, null, alias, Suffixes[joins], CollectionSuffixes[0], true)
61+
+ SelectString(associations)).SetFromClause(elementPersister.FromTableFragment(alias)
62+
+ elementPersister.FromJoinFragment(alias, true, true)).SetWhereClause(
63+
whereString.ToSqlString()).SetOuterJoins(ojf.ToFromFragmentString,
64+
ojf.ToWhereFragmentString
65+
+ elementPersister.WhereJoinFragment(alias, true, true));
7166

7267
select.SetOrderByClause(OrderBy(associations, oneToManyPersister.GetSQLOrderByString(alias)));
7368

7469
if (Factory.Settings.IsCommentsEnabled)
70+
{
7571
select.SetComment("load one-to-many " + oneToManyPersister.Role);
72+
}
7673

7774
SqlString = select.ToSqlString();
7875
}

src/NHibernate/Loader/Collection/OneToManyLoader.cs

+11-29
Original file line numberDiff line numberDiff line change
@@ -13,42 +13,24 @@ namespace NHibernate.Loader.Collection
1313
/// The collection persister must implement <see cref="IQueryableCollection" />.
1414
/// For other collections, create a customized subclass of <see cref="Loader" />.
1515
/// </remarks>
16+
/// <seealso cref="BasicCollectionLoader"/>
1617
public class OneToManyLoader : CollectionLoader
1718
{
18-
private static readonly ILog log = LogManager.GetLogger(typeof(OneToManyLoader));
19+
private static readonly ILog log = LogManager.GetLogger(typeof (OneToManyLoader));
1920

20-
public OneToManyLoader(
21-
IQueryableCollection oneToManyPersister,
22-
ISessionFactoryImplementor session,
23-
IDictionary<string, IFilter> enabledFilters)
24-
: this(oneToManyPersister, 1, session, enabledFilters)
25-
{
26-
}
21+
public OneToManyLoader(IQueryableCollection oneToManyPersister, ISessionFactoryImplementor session,
22+
IDictionary<string, IFilter> enabledFilters)
23+
: this(oneToManyPersister, 1, session, enabledFilters) {}
2724

28-
public OneToManyLoader(
29-
IQueryableCollection oneToManyPersister,
30-
int batchSize,
31-
ISessionFactoryImplementor factory,
32-
IDictionary<string, IFilter> enabledFilters)
33-
: this(oneToManyPersister, batchSize, null, factory, enabledFilters)
34-
{
35-
}
25+
public OneToManyLoader(IQueryableCollection oneToManyPersister, int batchSize, ISessionFactoryImplementor factory,
26+
IDictionary<string, IFilter> enabledFilters)
27+
: this(oneToManyPersister, batchSize, null, factory, enabledFilters) {}
3628

37-
public OneToManyLoader(
38-
IQueryableCollection oneToManyPersister,
39-
int batchSize,
40-
SqlString subquery,
41-
ISessionFactoryImplementor factory,
42-
IDictionary<string, IFilter> enabledFilters)
29+
public OneToManyLoader(IQueryableCollection oneToManyPersister, int batchSize, SqlString subquery,
30+
ISessionFactoryImplementor factory, IDictionary<string, IFilter> enabledFilters)
4331
: base(oneToManyPersister, factory, enabledFilters)
4432
{
45-
JoinWalker walker = new OneToManyJoinWalker(
46-
oneToManyPersister,
47-
batchSize,
48-
subquery,
49-
factory,
50-
enabledFilters
51-
);
33+
JoinWalker walker = new OneToManyJoinWalker(oneToManyPersister, batchSize, subquery, factory, enabledFilters);
5234
InitFromWalker(walker);
5335

5436
PostInstantiate();

0 commit comments

Comments
 (0)