NHibernate EventListeners - getting property value of persisted entity

I am implementing a custom EventListener to store audit information in NHibernate.

I am currently extending the DefaultSaveOrUpdateEventListener, overriding PerformSaveOrUpdate, looking at the properties of each object and saving them in a different place.

This works with simple properties, but a cascade failure is to maintain a one-to-many relationship.

If I take the following objects:

[ActiveRecord]
public class Child
{
    [PrimaryKey(PrimaryKeyType.GuidComb)]
    public Guid Id { get; set; }

    [BelongsTo]
    public Parent Parent { get; set; }
}

[ActiveRecord]
public class Parent
{
    [PrimaryKey(PrimaryKeyType.GuidComb)]
    public Guid Id { get; set; }

    [HasMany(Cascade = ManyRelationCascadeEnum.SaveUpdate)]
    public IList<Child> Children { get; set; }
}

And then save the parent with the child:

ActiveRecordMediator<Parent>.Save(new Parent
{
    Children = new List<Child>
    {
        new Child()
    }
});

The child will receive the correct parent assigned to it when it is stored in the database, but the parent property of the child is null when my EventListener is called.

How can I get a value that will actually be stored in the database in this case?

[EDIT] , , , , , NHibernate , .

+3
3

, ActiveRecord, , NHibernate .

NHibernate , "inverse = true", "not-null = true" ( , ). .

, . INSERT, INSERT AND UPDATE, , , . , , , , , . :

https://www.hibernate.org/hib_docs/nhibernate/html/example-parentchild.html

+2

, Castle ActiveRecord. .

- , , , Parent , . . ( , ActiveRecord, NHibernate.)

, Child, .

var parent = new Parent();
var child = new Child()
{
    Parent = parent
};
parent.Children.Add(child);

ActiveRecordMediator<Parent>.Save(child);
ActiveRecordMediator<Parent>.Save(parent);

, , , - .

0

ActiveRecord, NHibernate, , - (https://www.hibernate.org/hib_docs/nhibernate/html/example-parentchild.html)

, ORM ( Inverse = true HasMany)?

[ActiveRecord]
public class Parent
{
    [PrimaryKey(PrimaryKeyType.GuidComb)]
    public Guid Id { get; set; }

    [HasMany(Cascade = ManyRelationCascadeEnum.SaveUpdate, Inverse=true)]
    public IList<Child> Children { get; set; }
}
0
source

Source: https://habr.com/ru/post/1723125/


All Articles