OUR PROBLEM:
If the user which sould be notified is in the requested List ("users")
it's Address Association is initialized with the query provided above.
If we call the NotifyUser-Method the Address Association should be initalized with all Addresses of the user, but in this case the Address-Association is cached, and we only get the Addresses initalized by the IQuery
This is just one case this problem occurs. We always get this problem, when an Association is already initialize
Any help would be appreciated.
Comments to the "TransactionBlock":
A Nhibernate session and Transaction is opened when the block starts. The Transaction is commited and Closed when the last block ends.
public class UserManagement
{
/// <summary>
/// Gets all users with the given Url and its Addresses with the given Streetname.
/// </summary>
/// <returns>List of all users and its Associations to the requested Addresses</returns>
public IList<EoevUser> GetUsers(UrlDomainType url, StringDomainType street)
{
using (TransactionBlock tran = new TransactionBlock())
{
ISession session = NHibernateSessionManager.Instance.GetSession();
string query = "from EoevUser user left join fetch user.Address adr " +
"where " +
"user.WebAdresse like :url " +
"and adr.Street like :street";
IQuery q = session.CreateQuery(query);
q.SetString("url", url);
q.SetString("street", street);
q.SetResultTransformer(new DistinctRootEntityResultTransformer());
IList<EoevUser> users = q.List<EoevUser>();
// Gets the user which should be notified
Guid id = GetUserToNotify();
// OUR PROBLEM:
// If the user which sould be notified is in the requested List ("users")
// it's Address Association is initialized with the query provided above.
//
// If we call the NotifyUser-Method the Address Association should be initalized
// with all Addresses of the user, but in this case the Address-Association
// is cached, and we only get the Addresses initalized by the IQuery
//
// This is just one case this problem occurs. We always get this problem,
// when an Association is already initialized.
EoevUser fetchedUser = (EoevUser) session.Get(typeof (EoevUser), id);
NotifyUser(fetchedUser, "Some Message");
tran.IsValid = true;
return users;
}
}
/// <summary>
/// Sends a mail message
/// </summary>
public void NotifyUser(EoevUser user, StringDomainType msg)
{
using (TransactionBlock tran = new TransactionBlock())
{
IList<Address> addressToNotify = user.Address;
foreach (Address address in addressToNotify)
{
// send mail with msg
System.Diagnostics.Debug.WriteLine(msg+" "+address.Street);
}
tran.IsValid = true;
}
}
}