Fluent NHibernate mapping failing when saving child objects - c#

I did try and use this question Setting up one to many relationship with Fluent Nhibernate as a basis but still having issues.
I have two classes - Contact and PostalAddress - A Contact can have many addresses. The thing is when it gets to a class a with PostalAddress it fails with
Invalid index 1 for this SqlParameterCollection with Count=1.
There is one address against that particular contact
Contact Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Threading.Tasks;
namespace YourLivesDataEngine.DataObjects
{
[DataContract()]
public class Contact
{
private String givenName;
private String surname;
private String middleName;
public Contact()
{
Emails = new List<EmailAddress>();
Addresses = new List<PostalAddress>();
TelephoneNumbers = new List<TelephoneNumber>();
givenName = String.Empty;
surname = String.Empty;
middleName = String.Empty;
Id = Guid.NewGuid().ToString();
}
[DataMember]
public virtual String Id { get; set; }
[DataMember]
public virtual DateTime LastUpdated { get; set; }
[DataMember]
public virtual String Network { get; set; }
[DataMember]
public virtual String NetworkId { get; set; }
[DataMember]
public virtual String Name { get; set; }
[DataMember]
public virtual String GivenName { get; set; }
[DataMember]
public virtual String Surname { get; set; }
[DataMember]
public virtual String MiddleName { get; set; }
[DataMember]
public virtual IList<EmailAddress> Emails { get; set; }
[DataMember(Name="Addresses")]
public virtual IList<PostalAddress> Addresses { get; set; }
[DataMember]
public virtual IList<TelephoneNumber> TelephoneNumbers { get; set; }
}
}
PostalAddress Class
public class PostalAddress
{
public PostalAddress()
{
Id = Guid.NewGuid().ToString();
}
[DataMember]
public virtual String Id { get; set; }
public virtual String Address { get; set; }
public virtual String Owner { get; set; }
public virtual String AddressType { get; set; }
public virtual Contact Contact { get; set; }
}
And my Mappings
ContactMap
public class ContactMap : ClassMap<Contact>
{
public ContactMap()
{
Table("tblContacts");
Id(x => x.Id);
Map(x => x.LastUpdated);
Map(x => x.Network);
Map(x => x.NetworkId);
Map(x => x.Name);
Map(x => x.GivenName);
Map(x => x.Surname);
Map(x => x.MiddleName);
HasMany<PostalAddress>(x => x.Addresses)
.Inverse()
.KeyColumns.Add("Id", mapping => mapping.Name("Id"))
.Cascade.SaveUpdate()
}
}
PostalAddressMap
public class PostalAddressMap
: ClassMap<PostalAddress>
{
public PostalAddressMap()
{
Table("tblPostalAddresses");
Id(x => x.Id);
Id(x => x.Address);
Id(x => x.AddressType);
Id(x => x.Owner);
References(x => x.Contact)
.Class<Contact>()
.Column("Owner");
}
}
The code to save the Contact is
public void Save ( Contact Contact )
{
try
{
using (ISessionFactory factory = CreateSessionFactory())
{
using (var session = factory.OpenSession())
{
using (ITransaction transaction = session.BeginTransaction())
{
// save the contact
session.Save(Contact);
transaction.Commit();
}
}
}
}
catch ( System.Exception e)
{
throw e;
}
finally
{
}
}
I'm sure it is something to do with the way the mapping is set-up but cannot see it, while there are some contacts that do not have an address - some do so I should have at least some addresses.

With some help from mxmissile have now fixed it.
Problems were all in the mapping of PostalAddress. First off I had made all the fields Id's - so changed the required properties to Map.
Then got the Invalid index 4 for this SqlParameterCollection with Count=4 error everytime I tried to save the addresses. This error is caused when a field is mapped twice. For this mapping it was the Owner which was mapped twice. It did not need to be listed in the Map - by commenting it out the application worked. The revised Mapping class is as follows;
public class PostalAddressMap
: ClassMap<PostalAddress>
{
public PostalAddressMap()
{
Table("tblPostalAddresses");
Id(x => x.Id);
Map(x => x.Address);
Map(x => x.AddressType);
References(x => x.Contact)
.Class<Contact>()
.Column("Owner");
}
}
Once those changes were made the application saved the Contacts and Addresses correctly.

Related

Can't Insert data into database using NHibernate

I'm new in NHibernate I write code which is simple should insert into database information about users that's it
the code is below
UserClassModel
class Users
{
public virtual int id { get; set; }
public virtual string username { get; set; }
public virtual string password { get; set; }
public virtual string role { get; set; }
public virtual bool deleted { get; set; }
public virtual DateTime create_date { get; set; }
}
UserMapClass
class UserMap : ClassMap<Users>
{
public UserMap()
{
Id(x => x.id);
Map(x => x.username);
Map(x => x.password);
Map(x => x.deleted);
Map(x => x.role);
Map(x => x.create_date);
}
}
Connection where I try to insert data into database using NHibernate
class Connection
{
public Connection()
{
var sefact = createFactory();
using (var session = sefact.OpenSession())
{
using (var txt = session.BeginTransaction())
{
var users = new Users
{
username = "jakhongir",
password = "2356+"
};
session.Save(users);
}
}
}
private static ISessionFactory createFactory()
{
string connectionString = "Server=127.0.0.1; Port=5433; User Id=smartwarehouse; Password=$smart#2018;Database=warehouse;";
IPersistenceConfigurer config = PostgreSQLConfiguration.PostgreSQL82.ConnectionString(connectionString);
FluentConfiguration configuration = Fluently.Configure().Database(config).Mappings(m => m.FluentMappings.Add(typeof(UserMap)));
return configuration.BuildSessionFactory();
}
here is code I can't insert it into database it gives following errors
here is database part of
Rename your Model class to 'User' from 'Users' (Generally it's a good practice to use singular names for the model, as it represents a single entity)
class User
{
public virtual int id { get; set; }
public virtual string username { get; set; }
public virtual string password { get; set; }
public virtual string role { get; set; }
public virtual bool deleted { get; set; }
public virtual DateTime create_date { get; set; }
}
The mapping class must derive from ClassMap :
// Creating the map class
public UserMap:ClassMap<User>
{
// The constructor of Mapping class
public UserMap()
{
Id(x => x.id);
Map(x => x.username);
Map(x => x.password);
Map(x => x.deleted);
Map(x => x.role);
Map(x => x.create_date);
}
}

Fluent NHibernate: How to perform INNER JOIN?

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

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 one-to-one mapping not working (again.....)

The more messages I read about it the more I get confused! Maybe I simply understand something wrong... I have two entities: Computer and OperatingSystem, both have a few dozens properties.
The relation I like to map is one-to-one, one computer has one OS. To shorten I only show relevant properties:
public abstract class EntityBase
{
public virtual Guid Id { get; set; }
public virtual DateTime CDate { get; set; }
public virtual string CUser { get; set; }
protected EntityBase()
{
Initialize();
}
private void Initialize()
{
CDate = DateTime.Now;
var wi = System.Security.Principal.WindowsIdentity.GetCurrent();
if (wi != null)
CUser = wi.Name;
}
}
public class ComputerDb : EntityBase
{
public virtual string ComputerType { get; set; }
public virtual string Domain { get; set; }
public virtual string Name { get; set; }
/* ...... */
public virtual OsDb Os { get; set; }
public virtual void AddOs(OsDb os)
{
this.Os = os;
}
public class ComputerDbMap : ClassMap<ComputerDb>
{
public ComputerDbMap()
{
Id(x => x.Id).GeneratedBy.GuidComb();
Map(x => x.ComputerType).Nullable().Length(16);
Map(x => x.Domain).Nullable().Length(64);
Map(x => x.Name).Not.Nullable().Length(64);
/* .... */
HasOne(x => x.Os).Cascade.All().PropertyRef(x => x.Computer);
}
}
public class OsDb : EntityBase
{
public virtual string Name { get; set; }
public virtual string Description { get; set; }
public virtual bool? PartOfCluster { get; set; }
/* ... */
public virtual ComputerDb Computer { get; set; }
}
public class OsDbMap : ClassMap<OsDb>
{
public OsDbMap()
{
Id(x => x.Id).GeneratedBy.GuidComb();
Map(x => x.Name).Nullable().Length(128).Index("Idx_OsName");
Map(x => x.Description).Nullable().Length(128);
Map(x => x.PartOfCluster).Nullable();
/* .... */
References(x => x.Computer, "Computer_Id").Unique();
}
}
In the program I do:
var computer = getComputerDb(ps);
var os = getOsDb(ps);
computer.AddOs(os);
var o = new Operations();
o.AddComputerToDb(computer);
where
public void AddComputerToDb(ComputerDb cpDb)
{
using (var session = _sessionManager.OpenSession())
{
using (var transaction = session.BeginTransaction())
{
try
{
session.SaveOrUpdate(cpDb);
transaction.Commit();
}
catch (Exception e)
{
Log.Error($"Exception in AddComputerToDb(). Exception: {e}");
transaction.Rollback();
throw;
}
finally
{
session.Close();
}
}
}
Here the result of the select on the tblComputerDb table on SQLServer:
And here the result from the tblOsDb table:
Problem: Computer_Id is NULL. If I understand things right, it should contain the GUID of the computer record 053DFFDF-0A88-4270-8405-A5EE00FFD388. I do not understand, why it cannot fill this Id from the computer entity. Modelling as ONE-TO-MANY works just fine, but is NOT what I want...
What am I doing wrong?

TreeMenu with self Referencing and nested Menu

I have a need to create a TreeMenu using NHibernate.
Here is what i cameup with:
public class CategoryMap : ClassMap<Category>
{
public CategoryMap()
{
Id(x => x.Id);
Map(x => x.AppId);
Map(x => x.Title);
Map(x => x.MaxDepth).Default("3");
Map(x => x.IsActive);
References(x => x.Parent).LazyLoad().Column("ParentId");
HasMany(x => x.ChildrenNodes)
.LazyLoad()
.KeyColumn("ParentId")
.Cascade.All();
Table("Category");
}
}
public class Category : Node
{
public virtual string AppId { get; set; }
public override string Title { get; set; }
public override int MaxDepth { get; set; }
public virtual Category Parent { get; set; }
public virtual IList<Category> ChildrenNodes { get; set; }
public virtual bool IsActive { get; set; }
}
public abstract class Node
{
public virtual long Id { get; set; }
public abstract string Title { get; set; }
public abstract int MaxDepth { get; set; }
}
Here is my testing code:
[Test]
public void CreateTableInDb()
{
using (var db = new FileDatabase(DbFileLocation, true))
{
var categoryMenu = new Category
{
AppId = "1",
MaxDepth = 3,
IsActive = true,
Title = "Services"
};
db.Create(categoryMenu);
categoryMenu.ChildrenNodes = new List<Category>
{
new Category
{
Title = "SubService-Level1",
Parent = categoryMenu,
}
};
db.Update(categoryMenu);
}
}
When i look into the table it's creating i have only one row,
with empty ParentId column.
How can i fix it and what i'm doing wrong?
first you'll missing .Inverse() on the hasmany. As it stands it will save and update the parentId twice
all collections should be readonly. NHibernate will replace them with tracking and lazyloading collections which you are throwing away. Change the code to
public class Category : Node
{
public Category()
{
// best practice so that nobody has to null check the collection
ChildrenNodes = new List<Category>();
}
public virtual IList<Category> ChildrenNodes { get; private set; }
public void Add(Category subcategory)
{
subcategory.Parent = this;
ChildrenNodes.Add(subcategory);
}
}
// in testcode
categoryMenu.Add(new Category { Title = "SubService-Level1" });

Categories

Resources