NHibernate Auditing - PostInsert Not Working - nhibernate

My application has the following entities:
public class User
{
public virtual int UserID { get; set; }
public virtual string UserName { get; set; }
public virtual IList<UserLog> Log { get; private set; }
public User()
{
Log = new List<UserLog>();
}
}
public class UserLog
{
public virtual int UserLogID { get; set; }
public virtual User User { get; set; }
public virtual string UserName { get; set; }
public virtual DateTime DateCreated { get; set; }
}
With the following fluent mappings:
public class UserMap : ClassMap<User>
{
public UserMap()
{
Table("Users");
Id(x => x.UserID);
Map(x => x.UserName);
HasMany(x => x.Log)
.KeyColumn("UserID")
.OrderBy("DateCreated")
.Inverse()
.Cascade.All();
}
}
public class UserLogMap : ClassMap<UserLog>
{
public UserLogMap()
{
Table("UsersLog");
Id(x => x.UserLogID);
References(x => x.User, "UserID");
Map(x => x.UserName);
Map(x => x.DateCreated);
}
}
The UsersLog table simply logs any changes which are made to the User. I'm trying to hook this up automatically using the NHibernate event listeners. I have setup my configuration which successfully calls the following 2 methods:
public void OnPostInsert(PostInsertEvent #event)
{
if (#event.Entity is User)
InsertLog(#event.Entity);
}
public void OnPostUpdate(PostUpdateEvent #event)
{
if (#event.Entity is User)
InsertLog(#event.Entity);
}
Edit (here is the InsertLog method):
private void InsertLog(object entity)
{
var context = ServiceLocator.Current.GetInstance<IDataContext>();
var user = (User)entity;
context.Repository<UserLog>().Insert(new UserLog
{
User = user,
UserName = user.UserName,
DateCreated = DateTime.UtcNow
}); // Insert calls ISession.SaveOrUpdate(entity)
}
When i update a record a log is successfully inserted but when i insert a record i get an error telling me the UserID cannot be null in the UsersLog table. I've played around with a few different variations but nothing seems to work.
I'd really appreciate it if someone could show me how this can be done. Thanks

Solution posted in question comments. I simply had to use:
#event.Session.GetSession(NHibernate.EntityMode.Poco).Save(...);
to save the user instead of accessing the repository.

Related

FluentNhibernate and HasMany

I try a simple Test Application with FluentNhibernate but it doesnt work as I expected.
Here are my classes:
public class Document : DataEntity
{
public virtual string Title { get; set; }
public virtual string FileName { get; set; }
public virtual DateTime LastModificationDate { get; set; }
public virtual User LastModificationBy { get; set; }
public virtual byte[] Content { get; set; }
public virtual User Owner { get; set; }
}
public class User : DataEntity
{
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
public virtual string Login { get; set; }
public virtual string PasswordHash { get; set; }
public virtual string Email { get; set; }
public virtual IList<Document> OwnedDocuments { get; set; }
public User()
{
this.OwnedDocuments = new List<Document>();
}
}
internal class UserMapping : ClassMap<User>
{
public UserMapping()
{
this.Id(x => x.Id);
this.Map(x => x.FirstName);
this.Map(x => x.LastName);
this.HasMany(x => x.OwnedDocuments).Inverse();
}
}
public DocumentMapping()
{
this.Id(x => x.Id);
this.Map(x => x.Title);
this.Map(x => x.FileName).Not.Nullable();
this.Map(x => x.LastModificationDate).Index("IDX_ModificationDate");
this.Map(x => x.Content);
this.References(x => x.LastModificationBy).Column("LastModificationBy");
this.References(x => x.Owner).Column("Owner");
this.Table("Document");
}
I try to test it with the following code
using (var transaction = Session.BeginTransaction())
{
var users = this.kernel.Get<IRepository<User>>();
var document = this.kernel.Get<IRepository<Document>>();
var user = new User { Login = "Blubb" };
users.Add(user);
var list = Builder<Document>.CreateListOfSize(50).All().With(x => x.Owner = user).Build().ToList();
var list2 = Builder<Document>.CreateListOfSize(50).All().Build().ToList();
list.ForEach(x => user.OwnedDocuments.Add(x));
document.Add(list);
document.Add(list2);
transaction.Commit();
var i = document.All().Count();
i.Should().Be(50);
var docs = ((IGuidKeyedRepository<User>)users).FindBy(user.Id).OwnedDocuments.Count();
docs.Should().Be(50);
}
The first problem is, why is the document count always 0 when I dont call document.Add(list);? I thought when I add some documents to the document collection of the user, they will be automaticly added to the documents?
And why ist the last test 100? Because I filter on the documents belongs to that user.
It looks like you need to set a cascade option on the child collection OwnedDocuments
this.HasMany(x => x.OwnedDocuments).Inverse().Cascade.AllDeleteOrphan();
The setting above will save all the children if you add any to the collection and if you remove them from the collection and save the object it will delete those child objects. You can find out more information about these settings in the nhibernate documentation:
http://www.nhforge.org/doc/nh/en/

NHibernate Criteria Queries - how use in One to One relationship

I have simple 3 POCO classes:
public class User
{
//PK
public virtual int UserId { get; set; }
//ONE to ONE
public virtual Profil Profil{ get; set; }
//ONE to MANY
public virtual IList<PhotoAlbum> Albums { get; set; }
}
public class Profil
{
//PK
public virtual int ProfilId { get; set; }
public virtual int Age { get; set; }
public virtual int Sex { get; set; }
}
public class PhotoAlbum
{
//PK
public virtual int PhotoAlbumId { get; set; }
public virtual string Name { get; set; }
public virtual int NumberOfPhoto { get; set; }
}
I created these mapping classes:
public class UserMap : ClassMap<User>
{
public UserMap()
{
//PK
Id(p => p.UserId)
.GeneratedBy.Identity();
//FK
References(p => p.Profil)
.Column("ProfilId")
.Cascade.All();
//ONE TO MANY
HasMany(p => p.Albums)
.Cascade.All();
Table("Users");
}
}
public class ProfilMap: ClassMap<Profil>
{
public ProfilMap()
{
Id(p => p.ProfilId)
.GeneratedBy.Identity();
Map(p => p.Age)
.Not.Nullable();
Map(p => p.Sex)
Table("Profiles");
}
}
public class PhotoAlbumMap : ClassMap<PhotoAlbum>
{
public PhotoAlbumMap()
{
Id(p => p.PhotoAlbumId)
.GeneratedBy.Identity();
Map(p => p.Name)
.Not.Nullable();
Map(p => p.NumberOfPhoto)
.Not.Nullable();
Table("PhotoAlbums");
}
}
Then I created simple NHibernate repository class with this method:
public IList<T> GetItemsByCriterions(params ICriterion[] criterions)
{
ICriteria criteria = AddCriterions(_session.CreateCriteria(typeof(T)),
criterions);
IList<T> result = criteria.List<T>();
return result ?? new List<T>(0);
}
For test I created repository for some entity, for example User:
_userRepo = new NHibRepository<User>(NHibeHelper.OpenSession());
and I would like have possibility make query in this style:
var users = _userRepo.GetItemsByCriterions(new ICriterion[]
{
Restrictions.Gt("Profile.Age",10)
});
this attempt finished with error:
could not resolve property: Profile of: Repository.User
User has property Profile type of Profile and this property has properties ProfileId, Age
and sex.
** #1 EDITED:**
# I tried this:
var users = _userRepo.GetItemsByCriterions(new ICriterion[]
{
Restrictions.Where<User>(u=>u.Profil.Sex==0)
});
finished with error:
could not resolve property: Profil.Sex of: Repository.User
#2 EDITED
I tried use Nathan’s advice:
var result = _userRepo.Session.CreateCriteria<User>()
.CreateAlias("Profile", "profile", JoinType.InnerJoin)
.Add(Restrictions.Eq("profile.Sex", 0));
IList<User> users=null;
if (result != null)
users = result.List<User>();
If I tried convert result to List I again get this error: could not resolve property: Profile of: Repository.User
Looking at your example, User has a Profil property not a Profile property.
If it is supposed to be Profil then I would change the Restrictions.Gt(Profile.Age,10) to Restrictions.Gt(Profil.Age,10) otherwise change the name of the property and mapping to match the query.
Edit:
You are trying to query the User Object. you need to include the CreateAlias let nhibernate know that you want to link to a different object.
Try This.
var users = session.CreateCriteria<User>()
.CreateAlias("Profile", "profile", JoinType.InnerJoin)
.Add(Restrictions.Eq("profile.Age", 10));

Should I let the ORM directly populate the DTOs?

and should I save the DTOs directly to the database?
public class Product
{
public virtual int ProductId { get; set; }
public virtual string ProductCode { get; set; }
public virtual string ProductName { get; set; }
public virtual Category Category { get; set; }
}
public class ProductMap : ClassMap<Product>
{
public ProductMap()
{
Id(x => x.ProductId);
Map(x => x.ProductCode);
Map(x => x.ProductName);
References(x => x.Category).Column("CategoryId");
}
}
public class ProductDto
{
public virtual int ProductId { get; set; }
public virtual string ProductCode { get; set; }
public virtual string ProductName { get; set; }
public virtual int CategoryId { get; set; }
}
public class ProductDtoMap : ClassMap<ProductDto>
{
public ProductDtoMap()
{
Table("Product");
Id(x => x.ProductId);
Map(x => x.ProductCode);
Map(x => x.ProductName);
Map(x => x.CategoryId);
}
}
Here's how I create, open and save record:
public ActionResult Input()
{
return View(new ProductDto());
}
public ActionResult Edit(int id)
{
using(var s = SessionFactoryBuilder.GetSessionFactory().OpenSession())
{
return View("Input", s.Get<ProductDto>(id));
}
}
// Save
[HttpPost]
public ActionResult Input(ProductDto p)
{
if (ModelState.IsValid)
{
using (var s = SessionFactoryBuilder.GetSessionFactory().OpenSession())
{
s.Merge(p);
s.Flush();
}
return RedirectToAction("Index");
}
else
{
return View(p);
}
}
Suffice to say, I wanted convenience, I want to persist the DTOs directly to database, and retrieve them back directly too.
Now what need do I have for Product class if I just use ProductDto exclusively for CRUD? I'll just use Product class for reporting only :-)
Is populating DTOs directly from ORM and saving them back directly to ORM a sound practice?
Yes if you can do it, it's the best way. Don't feel guilty about it :)

NHibernate - Delete Not Peristing in the Database

i'm trying to remove an item from a one to many list and have it persist in the database. Here are the entities i have defined:
public class SpecialOffer
{
public virtual int SpecialOfferID { get; set; }
public virtual string Title { get; set; }
public virtual IList<SpecialOfferType> Types { get; private set; }
public SpecialOffer()
{
Types = new List<SpecialOfferType>();
}
}
public class SpecialOfferType
{
public virtual SpecialOffer SpecialOffer { get; set; }
public virtual Type Type { get; set; }
public virtual int MinDaysRemaining { get; set; }
#region composite id requirements
public override bool Equals(object obj)
{
if (obj == null || !(obj is SpecialOfferType))
return false;
var t = (SpecialOfferType)obj;
return SpecialOffer.SpecialOfferID == t.SpecialOffer.SpecialOfferID && Type.TypeID == t.Type.TypeID;
}
public override int GetHashCode()
{
return (SpecialOffer.SpecialOfferID + "|" + Type.TypeID).GetHashCode();
}
#endregion
}
public class Type
{
public virtual int TypeID { get; set; }
public virtual string Title { get; set; }
public virtual decimal Price { get; set; }
}
With the following fluent mappings:
public class SpecialOfferMap : ClassMap<SpecialOffer>
{
public SpecialOfferMap()
{
Table("SpecialOffers");
Id(x => x.SpecialOfferID);
Map(x => x.Title);
HasMany(x => x.Types)
.KeyColumn("SpecialOfferID")
.Inverse()
.Cascade.All();
}
}
public class SpecialOfferTypeMap : ClassMap<SpecialOfferType>
{
public SpecialOfferTypeMap()
{
Table("SpecialOfferTypes");
CompositeId()
.KeyReference(x => x.SpecialOffer, "SpecialOfferID")
.KeyReference(x => x.Type, "TypeID");
Map(x => x.MinDaysRemaining);
}
}
public class TypeMap : ClassMap<Type>
{
public TypeMap()
{
Table("Types");
Id(x => x.TypeID);
Map(x => x.Title);
Map(x => x.Price);
}
}
The problem i have is that if i remove an item from the SpecialOffer.Types collection it successfully removes it from the list but when i try to save the session the change is not persisted in the database. I'm assuming this is something to do with the composite id on the join table since i have been able to do this successfully in the past with a standard id.
I'd appreciate it if someone could show me what i'm doing wrong. Thanks
I think you have to 1) Change the cascade setting on SpecialOffer.Types to Cascade.AllDeleteOrphan() and 2) set SpecialOfferType.SpecialOffer = null when you remove it from the collection. Since the collection is the inverse side of the relationship, the many-to-one reference to SpecialOffer on SpecialOfferType has to be set to null to make it an orphan, then Cascade.AllDeleteOrphan will cause it to be deleted.

Composite Key/Id Mapping with NHibernate

I have the following tables in my database:
Announcements:
- AnnouncementID (PK)
- Title
AnouncementsRead (composite PK on AnnouncementID and UserID):
- AnnouncementID (PK)
- UserID (PK)
- DateRead
Users:
- UserID (PK)
- UserName
Usually I'd map the "AnnouncementsRead" using a many-to-many relationship but this table also has an additional "DateRead" field.
So far I have defined the following entities:
public class Announcement
{
public virtual int AnnouncementID { get; set; }
public virtual string Title { get; set; }
public virtual IList<AnnouncementRead> AnnouncementsRead { get; private set; }
public Announcement()
{
AnnouncementsRead = new List<AnnouncementRead>();
}
}
public class AnnouncementRead
{
public virtual Announcement Announcement { get; set; }
public virtual User User { get; set; }
public virtual DateTime DateRead { get; set; }
}
public class User
{
public virtual int UserID { get; set; }
public virtual string UserName { get; set; }
public virtual IList<AnnouncementRead> AnnouncementsRead { get; private set; }
public User()
{
AnnouncementsRead = new List<AnnouncementRead>();
}
}
With the following mappings:
public class AnnouncementMap : ClassMap<Announcement>
{
public AnnouncementMap()
{
Table("Announcements");
Id(x => x.AnnouncementID);
Map(x => x.Title);
HasMany(x => x.AnnouncementsRead)
.Cascade.All();
}
}
public class AnnouncementReadMap : ClassMap<AnnouncementRead>
{
public AnnouncementReadMap()
{
Table("AnnouncementsRead");
CompositeId()
.KeyReference(x => x.Announcement, "AnnouncementID")
.KeyReference(x => x.User, "UserID");
Map(x => x.DateRead);
}
}
public class UserMap : ClassMap<User>
{
public UserMap()
{
Table("Users");
Id(x => x.UserID);
Map(x => x.UserName);
HasMany(x => x.AnnouncementsRead)
.Cascade.All();
}
}
However when I run this I receive the following error:
"composite-id class must override Equals(): Entities.AnnouncementRead"
I'd appreciate it if someone could point me in the right direction. Thanks
You should do just what NHibernate is telling you. AnnouncementRead should override Equals and GetHashCode methods. They should be based on fields that are part of primary key
When implementing equals you should use instanceof to allow comparing with subclasses. If Hibernate lazy loads a one to one or many to one relation, you will have a proxy for the class instead of the plain class. A proxy is a subclass. Comparing the class names would fail.
More technically: You should follow the Liskows Substitution Principle and ignore symmetricity.
The next pitfall is using something like name.equals(that.name) instead of name.equals(that.getName()). The first will fail, if that is a proxy.
http://www.laliluna.de/jpa-hibernate-guide/ch06s06.html