Thank you, thank you. Now i can intercept lazy initialization and set
additional operations, flags etc on my implementation of the
PersistentBag. Every relationship in my classes is annotated with a
RelationshipAttribute that I use to do auto-synchronization of the bi-
directional relationships.
[Relationship("lineItems", RelationshipType.Aggregation,
Cardinality.OneOrMore, inverseProperty = "invoice")]
public virtual IBag<LineItem> lineItems
{
//Set up relationship synchronization on the bag after collection has
been initialized.
namespace MC.NHibernate.Collections
{
public class InitializeCollectionEventListener :
IInitializeCollectionEventListener
{
#region Implementation of IInitializeCollectionEventListener
private readonly DefaultInitializeCollectionEventListener
defaultListener;
public InitializeCollectionEventListener()
{
defaultListener = new
DefaultInitializeCollectionEventListener();
}
public void OnInitializeCollection(InitializeCollectionEvent
@event)
{
IPersistentCollection collection = @event.Collection;
ISessionImplementor source = @event.Session;
CollectionEntry ce =
source.PersistenceContext.GetCollectionEntry(collection);
bool wasInitialized = collection.WasInitialized;
defaultListener.OnInitializeCollection(@event);
if (!wasInitialized)
{
string role = ce.LoadedPersister.Role;
string propertyName = role.Substring(role.LastIndexOf
(".") + 1);
Type ownerType = collection.Owner.GetType();
PropertyInfo pi = ownerType.GetProperty(propertyName);
object[] attributes = pi.GetCustomAttributes(typeof
(RelationshipAttribute), true);
if (attributes.Length == 1)
{
RelationshipAttribute ra = (RelationshipAttribute)
attributes[0];
if (String.IsNullOrEmpty(ra.inverseProperty) ==
false)
{
MethodInfo mi = pi.PropertyType.GetMethod
("SetUpRelationshipSynch");
//get a proxy class so that references
object owner = @event.Session.Load
(collection.Owner.GetType(), ((PersistentObject)
collection.Owner).RecordId);
mi.Invoke(collection, new[] { owner,
ra.relationshipName, ra.inverseProperty });
}
}
}
}
#endregion
On Nov 25, 4:43 am, "Richard \(Google\)" <
fluke...@googlemail.com>
wrote:
> Hi epitka,
>
> If you re-load the object from the session, you'll get the proxy back (and
> it won't hit the database). In the past I've added a 'This' property on my
> domain class to do this:
>
> private Pepe This
> {
> get
> {
> return Session.Load<Pepe>(typeof(Pepe), Id);
> }
>
> }
>
>
http://forum.hibernate.org/viewtopic.php?t=982610&highlight=
>
> Is that what you are after?
>
> Cheers,
> Richard
>
> --------------------------------------------------
> From: "epitka" <
exptrade2...@yahoo.com>