Fluent NHibernate: How to perform INNER JOIN? - c#

I was wondering if someone could help me. I want to perform SQL INNER JOIN operation by using NHibernate. First of all let me introduce you to the structure of my database.
I have the following parameters into my C# method: int documentId, int userId, int folderId. My main goal is to get RoleDocumentValueEntity. By using USER_ID and FOLDER_ID I can get the unique FolderUserRoleEntity. I am looking for the ROLE_ID. I want to join ROLE_DOCUMENT_VALUE and FOLDER_USER_ROLES tables by ROLE_ID where DOCUMENT_ID is equal to specific value which I have as a parameter of C# method.
My entities:
public class RoleDocumentValueEntity
{
public virtual int Id { get; set; }
public virtual RoleEntity Role { get; set; }
public virtual DocumentEntity Document { get; set; }
public virtual string Text { get; set; }
}
public class RoleEntity
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
public virtual IList<UserRoleEntity> UserRoles { get; set; }
public virtual IList<FolderUserRoleEntity> FolderUserRoles { get; set; }
}
public class FolderEntity
{
public virtual int Id { get; set; }
public virtual UserEntity User { get; set; }
public virtual IList<DocumentEntity> Documents {get; set;}
}
public class UserEntity
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
}
public class UserRoleEntity
{
public virtual int Id { get; set; }
public virtual int UserId { get; set; }
public virtual RoleEntity Role { get; set; }
}
public class FolderUserRoleEntity
{
public virtual int Id { get; set; }
public virtual int UserId { get; set; }
public virtual RoleEntity Role { get; set; }
public virtual FolderEntity Folder { get; set; }
}
Mappings:
public class RoleDocumentValueMap : BaseDatabaseMap<RoleDocumentValueEntity>
{
public RoleDocumentValueMap()
{
this.Table("ROLE_DOCUMENT_VALUE");
Id(x => x.Id, "ROLE_DOCUMENT_VALUE_ID");
// Relationships
References(x => x.Role)
.Column("ROLE_ID")
.Cascade.None();
References(x => x.Document)
.Column("DOCUMENT_ID")
.Cascade.None();
}
}
public class RoleMap : BaseDatabaseMap<RoleEntity>
{
public RoleMap()
{
this.Table("ROLES");
Id(x => x.Id, "ROLE_ID");
HasMany(x => x.UserRoles)
.KeyColumn("ROLE_ID")
.Cascade.All()
.Inverse();
HasMany(x => x.FolderUserRoles)
.KeyColumn("ROLE_ID")
.Cascade.All()
.Inverse();
}
}
public class UserRoleMap : BaseDatabaseMap<UserRoleEntity>
{
public UserRoleMap()
{
this.Table("USER_ROLES");
this.Id(x => x.Id, "USER_ROLE_ID");
Map(x => x.UserId).Column("USER_ID");
References(x => x.Role).Column("ROLE_ID").Cascade.None().Fetch.Join();
}
}
public class FolderUserRoleMap : BaseDatabaseMap<FolderUserRoleEntity>
{
public FolderUserRoleMap()
{
this.Table("FOLDER_USER_ROLES");
Id(x => x.Id, "FOLDER_USER_ROLE_ID");
Map(x => x.UserId).Column("USER_ID");
References(x => x.Folder).Column("FOLDER_ID").Cascade.None().Fetch.Join();
References(x => x.Role).Column("ROLE_ID").Cascade.None().Fetch.Join().Not.LazyLoad();
}
}
public class FolderMap : BaseDatabaseMap<FolderEntity>
{
public FolderMap()
{
this.Table("FOLDERS");
Id(x => x.Id, "FOLDER_ID");
HasMany(x => x.Documents)
.Cascade
.AllDeleteOrphan()
.Inverse()
.KeyColumn("FOLDER_ID");
}
}
public class DocumentMap : BaseDatabaseMap<DocumentEntity>
{
public DocumentMap()
{
this.Table("DOCUMENTS");
Id(x => x.Id, "DOCUMENT_ID");
// Relationships
References(x => x.Folder)
.Column("FOLDER_ID")
.Cascade.None();
}
}
INNER JOIN
I manually created SQL query which I want to get by using NHibernate.
SELECT ROLE_DOCUMENT_VALUE.*
FROM ROLE_DOCUMENT_VALUE
INNER JOIN FOLDER_USER_ROLES ON FOLDER_USER_ROLES.ROLE_ID = ROLE_DOCUMENT_VALUE.ROLE_ID
AND FOLDER_USER_ROLES.FOLDER_ID = ?
AND FOLDER_USER_ROLES.USER_ID = ?
AND ROLE_DOCUMENT_VALUE.DOCUMENT_ID = ?;
It looks like my NHibernate criteria should look like this one:
var result = this.Session.CreateCriteria<RoleDocumentValueEntity>()
.CreateCriteria("FolderUserRoles", "fr")
.Add(Restrictions.Eq("fr.UserId", userId))
// etc
.List();
But this criteria cannot be performed because RoleDocumentValueEntity doesn't have such property as FolderUserRoles.
Please, could you provide me what is the best practice in this case? How can I create the SQL query which I want by using NHibernate?

you could use linq support for Nhibernate to achieve this easily,
from doc in Session.Query<RoleDocumentValueEntity>()
join role in Session.Query<FolderUserRoleEntity>()
on doc.Role equals role.Role

Related

nhibernate Could not determine type for entity exception

i am new to fluent nhibernate. i'm developing a project contains following 3 tables in its database:
"Person",
"RealPerson" and
"LegalPerson"
these three tables have relation as shown in picture:
all entities in my code such as these three entities are inherited from a base entity class here is the code of these entities
public class Person : Entity
{
public virtual RealPerson RealPerson { set; get; }
public virtual LegalPerson LegalPerson { set; get; }
}
public class RealPerson : Entity
{
public virtual string FirstName { set; get; }
public virtual string LastName { set; get; }
public virtual string FatherName { set; get; }
public virtual string NationalCode { set; get; }
public virtual DateTime BirthDate { set; get; }
public virtual string PhoneNumber { set; get; }
public virtual string MobileNumber { set; get; }
public virtual string EmailAddress { set; get; }
public virtual string HomeAddress { set; get; }
public virtual string WorkAddress { set; get; }
public virtual RealPerson Proxy { set; get; }
}
public class LegalPerson : Entity
{
public virtual string LegalPerson_Name { set; get; }
public virtual string RegistrationNumber { set; get; }
public virtual string Address { set; get; }
public virtual string PhoneNumber1 { set; get; }
public virtual string PhoneNumber2 { set; get; }
public virtual string PhoneNumber3 { set; get; }
public virtual RealPerson Proxy { set; get; }
}
and the code of base entity class is here:
public class Entity
{
protected bool Equals(Entity other)
{
if (other == null)
return false;
if (Id == Guid.Empty || other.Id == Guid.Empty)
return base.Equals(other);
return Id.Equals(other.Id);
}
public virtual Guid Id { set; get; }
public override int GetHashCode()
{
return Id.GetHashCode();
}
public override bool Equals(object obj)
{
if (obj is Entity)
return Equals((Entity)obj);
return base.Equals(obj);
}
protected ISession Session
{
get { return SessionAccountant.GetSession(); }
}
public virtual void Save()
{
Session.SaveOrUpdate(this);
}
public virtual void Delete()
{
Session.Delete(this);
}
}
Finally the classmaps are as following:
public class PersonMapping : ClassMap<Person>
{
public PersonMapping()
{
Table("Person");
Id(x => x.Id).GeneratedBy.GuidComb().Column("Person_Id");
References(x => x.RealPerson).Nullable().LazyLoad().Column("RealPerson_Id");
References(x => x.LegalPerson).Nullable().LazyLoad().Column("LegalPerson_Id");
}
}
public class RealPersonMapping : ClassMap<RealPerson>
{
public RealPersonMapping()
{
Table("RealPerson");
Id(x => x.Id).GeneratedBy.GuidComb().Column("RealPerson_Id");
Map(x => x.FirstName).Not.Nullable().Column("FirstName");
Map(x => x.LastName).Not.Nullable().Column("LastName");
Map(x => x.FatherName).Not.Nullable().Column("FatherName");
Map(x => x.NationalCode).Not.Nullable().Column("NationalCode");
Map(x => x.BirthDate).Nullable().Column("BirthDate");
Map(x => x.ShenasnamehNumber).Nullable().Column("ShenasnamehNumber");
Map(x => x.PhoneNumber).Nullable().Column("PhoneNumber");
Map(x => x.MobileNumber).Nullable().Column("MobileNumber");
Map(x => x.EmailAddress).Nullable().Column("EmailAddress");
Map(x => x.HomeAddress).Nullable().Column("HomeAddress");
Map(x => x.WorkAddress).Nullable().Column("WorkAddress");
References(x => x.Proxy).Nullable().LazyLoad().Column("Proxy_Id");
}
}
public class LegalPersonMapping : ClassMap<LegalPerson>
{
public LegalPersonMapping()
{
Table("LegalPerson");
Id(x => x.Id).GeneratedBy.GuidComb().Column("LegalPerson_Id");
Map(x => x.LegalPerson_Name).Not.Nullable().Column("LegalPerson_Name");
Map(x => x.RegistrationNumber).Not.Nullable().Column("RegistrationNumber");
Map(x => x.Address).Not.Nullable().Column("Address");
Map(x => x.PhoneNumber1).Nullable().Column("PhoneNumber1");
Map(x => x.PhoneNumber2).Nullable().Column("PhoneNumber2");
Map(x => x.PhoneNumber3).Nullable().Column("PhoneNumber3");
References(x => x.Proxy).Nullable().LazyLoad().Column("Proxy_Id");
}
}
i set the configuration and create a session. but when i run the project i get this exception at run time:
An unhandled exception of type 'NHibernate.MappingException' occurred in NHibernate.dll
Additional information: Could not determine type for: EntitiesClasses.Person
what is wrong with this code?!
You need to inherit from SubclassMap.
See here http://notherdev.blogspot.com/2012/01/mapping-by-code-inheritance.html?m=1
I think this is going to change your schema in that your base class isn't going to have the id of the subclass. The id of the base class is the id of the subclass.

Fluent Nhibernate Get Distinct parents having child count greater than n

Below are my entities. One ContractBill can have multiple SubscriberInvoices.
public class ContractBill
{
public virtual Int32 ContractID { get; set; }
public virtual Int32 CustomerID { get; set; }
public virtual string ContractStatus { get; set; }
public virtual Customer Customer { get; set; }
public virtual IList<SubscriberInvoice> InvoiceList { get; set; }
public ContractBill() { }
}
public class ContractBillMap : ClassMap<ContractBill>
{
public ContractBillMap()
{
Table("GT_CONTRACT");
Id(x => x.ContractID, "GF_CONT_ID").GeneratedBy.Increment();
Map(x => x.CustomerID, "GF_CUST_ID");
Map(x => x.ContractStatus, "GF_CONTRACT_STATUS");
HasMany(x => x.InvoiceList).KeyColumn("GF_CONT_ID").Not.LazyLoad();
}
}
public class SubscriberInvoice : Object
{
virtual public String InvoiceID { get; set; }
virtual public Int32 ContractID { get; set; }
virtual public Int32 CustomerID { get; set; }
virtual public String BillPeriod { get; set; }
virtual public Double Amount { get; set; }
virtual public Double MinimumAmount { get; set; }
virtual public DateTime PayByDate { get; set; }
virtual public String IsPaid { get; set; }
virtual public Double AmountPaid { get; set; }
public SubscriberInvoice()
{
}
}
public class SubscriberInvoiceMap : ClassMap<SubscriberInvoice>
{
public SubscriberInvoiceMap()
{
Table("GT_BILL");
CompositeId()
.KeyProperty(x => x.InvoiceID, "GF_BILL_ID")
.KeyProperty(x => x.ContractID, "GF_CONT_ID")
.KeyProperty(x => x.BillPeriod, "GF_BILL_PERIOD")
Map(x => x.Amount, "GF_AMOUNT");
Map(x => x.MinimumAmount, "GF_MINIMUM_AMOUNT");
Map(x => x.PayByDate, "GF_PAY_BY_DATE");
Map(x => x.IsPaid, "GF_PAID");
Map(x => x.AmountPaid, "GF_AMOUNT_PAID");
}
}
//Get distinct BillContract having more than two non-paid Subscriber Invoices.
ICriteria criteria = session.CreateCriteria(typeof(ContractBill), "cb1");
string aliasName = "invoice";
string prefix = aliasName + ".";
criteria = criteria.CreateAlias("InvoiceList", aliasName);
criteria = criteria.Add(Expression.Eq(prefix + "IsPaid", "N"));
// below detached query will give the count of non paid invoice list
DetachedCriteria crit = DetachedCriteria.For(typeof(ContractBill))
.CreateAlias("InvoiceList", "InvoiceList")
.SetProjection(Projections.ProjectionList()
.Add(Projections.RowCount()))
.Add(Property.ForName("ContractID").EqProperty("cb1.ContractID"));
//Non paid invoice Count = 2
criteria = criteria.Add(Restrictions.Gt(Projections.SubQuery(crit), 2));
//Not giving proper reasult when being used with paging
//criteria = Criteria.SetResultTransformer(Transformers.DistinctRootEntity);
criteria =criteria.SetFirstResult(0).SetMaxResults(10);
//Tried with below detached query, but not getting distinct record
DetachedCriteria crit1 = DetachedCriteria.For(typeof(ContractBill))
.Add(Restrictions.EqProperty ("ContractID", "cb1.ContractID"))
.CreateCriteria("InvoiceList")
.SetProjection(Projections.Distinct(Projections.Id()));
criteria = criteria.Add(Subqueries.PropertyIn("cb1.ContractID", crit1));
IList<ContractBill> matchingObjects = criteria.List<ContractBill>();
Above code is not giving distinct result. Please suggest how to get the distinct ContractBill records.
For displaying records with paging, I need to get the total record count for above criteria also.

Fluent NHibernate Many To Many on Same Table

How can I create relation like this?
public class UserMap : ClassMap<User>
{
public UserMap()
{
Id(x => x.UserId);
Map(x => x.UserName);
Map(x => x.Password);
Map(x => x.FirstName);
Map(x => x.LastName);
HasManyToMany(x => x.FBFriends).Cascade.All().Table("UserFBFriend");
HasManyToMany(x => x.FBFriends).Cascade.All().Inverse().Table("UserFBFriend");
}
}
public class User : BaseClass<User>
{
public virtual int UserId { get; set; }
public virtual string UserName { get; set; }
public virtual string Password { get; set; }
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
public virtual IList<User> FBFriends { get; set; }
public User()
{
FBFriends = new List<User>();
}
public virtual void AddUserFBFriend(User user)
{
user.FBFriends.Add(this);
FBFriends.Add(user);
}
}
UPDATE
HasManyToMany(x => x.LeftFBFriends).Cascade.All().Table("UserFBFriend");
HasManyToMany(x => x.RightFBFriends).Cascade.All().Inverse().Table("UserFBFriend");
public virtual IList<User> LeftFBFriends { get; set; }
public virtual IList<User> RightFBFriends { get; set; }
public User()
{
LeftFBFriends = new List<User>();
RightFBFriends = new List<User>();
}
As discussed, we need two collections, representing both directions A-B, B-A
public virtual IList<User> LeftFBFriends { get; set; }
public virtual IList<User> RightFBFriends { get; set; }
And we'd need explicit column mapping:
HasManyToMany(x => x.LeftFBFriends)
.Table("UserFBFriend")
.ParentKeyColumn("LeftColumn")
.ChildKeyColumn("RightColumn")
.Cascade.All().Table("UserFBFriend");
HasManyToMany(x => x.RightFBFriends)
.Table("UserFBFriend")
.ParentKeyColumn("RightColumn")
.ChildKeyColumn("LeftColumn")
.Cascade.All()
.Inverse()
So, this way we do correctly instruct NHibernate, which column will play parent (this ID) and which will be the child (collection ID). We have to do that for both collections

How to map a MainValue property using Fluent or LinqToNHibernate?

I have a one to many relationship between my product and image entities. What I'm trying to achieve is to map a main image in the product entity, using Fluent mapping or LinqToNHibernate.
My classes:
public class Product {
public virtual int Id { get; set; }
public virtual IList<Image> images { get; set; }
}
public class Image {
public virtual int Id { get; set; }
public virtual Product Product { get; set; }
public virtual bool IsMain { get; set; }
}
My mappings:
public class ProductMap : ClassMap<Product> {
public ProductMap() {
Id(x => x.Id);
HasMany(x => x.images).KeyColumn("id_product");
}
}
public class ImageClassMap : ClassMap<Image> {
public ImageClassMap() {
Table("product_image");
Id(x => x.Id);
References(x => x.Product, "id_product");
Map(x => x.IsMain).Column("is_main");
}
}
I was able to achieve this with QueryOver as follows:
Image imageAlias = null;
var product = session.QueryOver<Product>()
.Where(x => x.Id == 2)
.Left.JoinQueryOver(x => x.images, () => imageAlias, x => x.IsMain)
.SingleOrDefault();
Is it possible to have a MainImage property in the Product class, where IsMain property is true, using Fluent mapping or LinqToNHibernate?
Yes, you can try,
public class Product {
...
public Image MainImage { get; set; }
}
and then the mapping,
public ProductMap() {
...
HasOne(product => product.MainImage).KeyColumn("main_image_id");
}

Fluent NHibernate Composite Key Query

I am a newbie to Fluent Nhibernate and .NET. So please let me know if my question is not clear.
I have 3 tables in app as below
Table A
AID(PK), CreatedDate(PK), ZID, AFirstname, ALastname, AAddress, AZipCode
Table B
CID(PK), AID(PK), Date(PK), Field1, Field2
Table C
CID (PK), Cname
Please correct me if I am wrong. My Model Classes look like below.
public class A {
public A() {}
public virtual long AID { get; set; }
public virtual int ZID { get; set; }
public virtual DateTime CreatedDate { get; set; }
public virtual string AFirstName { get; set; }
public virtual string ALastName { get; set; }
public virtual string AZip { get; set; }
public virtual string AAddress { get; set; }
}
MapperClass for Class A
public class AMap : ClassMap<A> {
public AMap() {
Table("A");
CompositeId().KeyProperty(x => x.AID, "AID").KeyProperty(x => x.CreatedDate, "CreatedDate");
Map(x => x.ZID).Column("ZID").Not.Nullable();
Map(x => x.AFirstName).Column("AFirstname").Not.Nullable();
Map(x => x.ALastName).Column("ALastname").Not.Nullable();
//Same as above for Address and ZipCode
}
}
My B Model Class looks like below
public class B{
public B() { }
public virtual long AID { get; set; }
public virtual int CID { get; set; }
public virtual DateTime Date { get; set; }
public virtual string Field1 {get; set; }
public virtual string Field2 {get; set; }
}
Mapper Class for Class B
public class BMap : ClassMap<B> {
public BMap() {
Table("B");
CompositeId().KeyProperty(x => x.AID, "AID").KeyProperty(x => x.CID, "CID").KeyProperty(x => x.Date, "Date");
Map(x => x.Field1).Column("Field1").Not.Nullable();
//Same for Field2
}
}
My Table C Model Class looks like below.
public class C{
public C() { }
public virtual int C{ get; set; }
public virtual string Cname{ get; set; }
}
MapperClass for Class C
public class C: ClassMap<C> {
public C() {
Table("C");
Id(x => x.CID).GeneratedBy.Identity().Column("CID");
Map(x => x.CName).Column("Cname").Length(64);
}
}
Now many might argue that the table designs are not proper. But nothing can be done at this point and definitely this kind of design may degrade the performance but I guess we have to live with this for a little while.
Now I want some help in writing the mapping for all the three tables. Also can i use Criteria API to write a query for this to join all the 3 tables.
In my Application I get the ZID from another function. Now depending on the ZID i need to grab AFirstName, ALastName, Field1,Field2, CName. How can this be possible?
Please help. Also let me know if the problem is not clear.
Thanks in Advance.
replace the ids with references to the original properties so you can navigate them
public class B
{
public virtual A A { get; set; }
public virtual C C { get; set; }
public virtual string Field1 {get; set; }
public virtual string Field2 {get; set; }
}
public class BMap : ClassMap<B>
{
public BMap()
{
Table("B");
CompositeId()
.KeyReference(x => x.A, "AID", "Date")
.KeyReference(x => x.C, "CID");
Map(x => x.Field1, "Field1").Not.Nullable();
Map(x => x.Field2, "Field2").Not.Nullable();
}
}
// query for it
var results = session.CreateCritera<B>()
.JoinAlias("A", "a")
.JoinAlias("C", "c")
.SetProjection(Projections.List()
.Add(Projections.Property("Field1"), "Field1")
.Add(Projections.Property("Field2"), "Field2")
.Add(Projections.Property("a.CreatedDate"), "Date")
.Add(Projections.Property("c.Name"), "CName"))
.SetResulttransformer(Transformers.AliasToBean<YourDto>())
.List<YourDto>();

Categories

Resources