i'm new to nhibernate so maybe the response depends on my lack of knowledge.
I created these two tables:
(sorry for italian language, i hope you can understand withouth any problems).
Then, i have these object in my model:
[Serializable]
public class Profilo
{
public virtual int Id { get; set; }
public virtual string Matricola { get; set; }
public virtual string Ruolo { get; set; }
public virtual IList ListaSedi { get; set; }
public Profilo()
{
ListaSedi = new List();
}
}
[Serializable]
public class Sede
{
public virtual string CodiceSede { get; set; }
public virtual string DescrizioneSede { get; set; }
public virtual Profilo Profilo { get; set; }
}
This is the way i mapped entities using fluent nhibernate:
public class Map_Sede : FluentNHibernate.Mapping.ClassMap
{
public Map_Sede()
{
Table("TBA_Sede");
Id(x => x.CodiceSede).Column("codice_sede").GeneratedBy.Assigned();
Map(x => x.DescrizioneSede)
.Column("descrizione");
References(prof => prof.Profilo)
.Column("codice_sede");
}
}
public class Map_Profilo : FluentNHibernate.Mapping.ClassMap
{
public Map_Profilo()
{
Table("TBA_Profilo");
Id(x => x.Id).Column("id").GeneratedBy.Identity();
Map(x => x.Matricola)
.Column("matricola");
Map(x => x.Ruolo)
.Column("ruolo");
HasMany(x => x.ListaSedi)
.AsBag()
.KeyColumns.Add("codice_sede")
.Not.LazyLoad()
.Cascade.None();
}
}
Now, i'd like to insert a new Profilo instance on my. Everything seems to work but nhibernate does not insert values on TBA_Profilo.codice_sede column. I noticed that the insert statement is composed by two parameters (matricola, ruolo) - why does it forget the third parameter?
I read somewhere (on nhibernate mailing list) that's quite normal 'cause nhiberate insert values with null first and then update the same records with right values contained in the list property.
Is it right?
Am i doing any errors?
I hope to have clarified the situation.
thx guys
ps: I'm using Nhibernate 2.1 and fluent nhibernate 1.1
UPDATE: This is the code i use to save entity.
var sesionFactory = NHibernateHelper.createSessionFactory();
using (NHibernate.ISession session = sesionFactory.OpenSession())
{
using (var transaction = session.BeginTransaction())
{
try
{
session.SaveOrUpdate(entity);
transaction.Commit();
session.Flush();
}
catch (Exception)
{
transaction.Rollback();
throw;
}
finally
{
transaction.Dispose();
}
}
}
UPDATE 2: Following sly answer i slightly modified my solution. These are new model entities:
[Serializable]
public class Profilo
{
public virtual int Id { get; set; }
public virtual string Matricola { get; set; }
public virtual string Ruolo { get; set; }
public virtual IList ListaSedi { get; set; }
public Profilo()
{
ListaSedi = new List();
}
}
[Serializable]
public class Sede
{
public virtual string CodiceSede { get; set; }
public virtual string DescrizioneSede { get; set; }
public virtual IList ProfiliAssociati { get; set; }
}
And new mappings:
public class Map_Sede : FluentNHibernate.Mapping.ClassMap
{
public Map_Sede()
{
Table("TBA_Sede");
Id(x => x.CodiceSede).Column("codice_sede").GeneratedBy.Assigned();
Map(x => x.DescrizioneSede)
.Column("descrizione");
HasMany(x => x.ProfiliAssociati)
.AsBag()
.KeyColumns.Add("codice_sede")
.Cascade.All();
}
}
public class Map_Profilo : FluentNHibernate.Mapping.ClassMap
{
public Map_Profilo()
{
Table("TBA_Profilo");
Id(x => x.Id).Column("id").GeneratedBy.Identity();
Map(x => x.Matricola)
.Column("matricola");
Map(x => x.Ruolo)
.Column("ruolo");
HasMany(x => x.ListaSedi)
.AsBag()
.Inverse()
.KeyColumns.Add("codice_sede")
.Cascade.SaveUpdate();
}
}
Now it seems quite to work: i mean that nhibernate can write TBA_Profilo.codice_sede column even if it doesn't cicle on Profilo.IList (it inserts the last element of this List).
Any ideas?
KeyColumns mapping is applied only child table in many-to-one connection.
If you want to have connection you will need to use Id column from TBA_portfolio table and reference it from TBA_Sede.
Something like this:
Tba_portfolio
Id|matricola|ruolo
Tba_sede
Id|PorfolioId|descrizione
Your mapping is wrong, try this:
public class Map_Profilo : FluentNHibernate.Mapping.ClassMap
{
public Map_Profilo()
{
Table("TBA_Profilo");
Id(x => x.Id).Column("id").GeneratedBy.Identity();
Map(x => x.Matricola)
.Column("matricola");
Map(x => x.Ruolo)
.Column("ruolo");
HasMany(x => x.ListaSedi)
.AsBag()
.KeyColumns.Add("codice_sede")
.Not.LazyLoad()
.Cascade.SaveUpdate();
}
}
public class Map_Sede : FluentNHibernate.Mapping.ClassMap
{
public Map_Sede()
{
Table("TBA_Sede");
Id(x => x.CodiceSede).Column("codice_sede").GeneratedBy.Assigned();
Map(x => x.DescrizioneSede)
.Column("descrizione");
References(prof => prof.Profilo)
.Column("codice_sede")
.Cascade.None()
.Inverse();
}
}
The key is, you need the parent profilio to save its child items. Have the child items do nothing to its parent. Also, your table diagram looks wrong, profolio should not have a key to sede, but sede should have a fk to profolio.
Related
I have following domain classes:
public class News: EntityBase
{
public virtual DateTime CreationDate { get; set; }
public virtual IList<DomainNameToNews> DomainNameToNews { get; set; }
public News()
{
DomainNameToNews=new List<DomainNameToNews>();
}
}
public class DomainNameToNews : EntityBase
{
public virtual DomainName DomainName { get; set; }
public virtual News News { get; set; }
}
Mapping:
public class NewsMap : ClassMap<News>
{
public NewsMap()
{
Id(x => x.Id).GeneratedBy.Identity();
Map(x => x.CreationDate).Not.Nullable();
HasMany(x => x.DomainNameToNews).Cascade.AllDeleteOrphan();
}
}
public class DomainNameToNewsMap : ClassMap<DomainNameToNews>
{
public DomainNameToNewsMap()
{
Id(x => x.Id).GeneratedBy.Identity();
References(x => x.News).UniqueKey("UQ_DOMAIN_NEWS").Cascade.Delete();
References(x => x.DomainName).UniqueKey("UQ_DOMAIN_NEWS");
}
}
Explanation:
News can be used in different domains so DomainNameToNews is relationship between them. One news can be used by multiple domains.
Problem:
What I want is to delete and add DomainToNews objects through News repository.
On update of News Object this object will have a list of DomainNameToNews so When I will update News the row from DomainNameToNews that will not be in this list I want to delete at all from database .Now the row that is not in this List will have Null News ,but I want to delete at all. How I must to map my object to achieve this?
If I didn't explain myself enough clear please ask more details. Thnaks!!
You need to specify 'inverse' at the one-to-many collection association.
public class NewsMap : ClassMap<News>
{
public NewsMap()
{
Id(x => x.Id).GeneratedBy.Identity();
Map(x => x.CreationDate).Not.Nullable();
HasMany(x => x.DomainNameToNews).Inverse.Cascade.AllDeleteOrphan();
}
}
public class DomainNameToNewsMap : ClassMap<DomainNameToNews>
{
public DomainNameToNewsMap()
{
Id(x => x.Id).GeneratedBy.Identity();
References(x => x.News).UniqueKey("UQ_DOMAIN_NEWS");
References(x => x.DomainName).UniqueKey("UQ_DOMAIN_NEWS");
}
}
Explanation on 'inverse' in this blog post
I am using NHibernate. This is the employee class
public class Employee
{
public virtual int Id { get; protected set; }
public virtual string FirstName { get; set; }
public virtual string LastName { get; set; }
public virtual Store Store { get; set; }
}
This is the store class:
public class Store
{
public virtual int Id { get; protected set; }
public virtual string Name { get; set; }
public virtual IList<Employee> Staff { get; set; }
public Store()
{
Staff = new List<Employee>();
}
}
The following are the mapping classes. Employee Map:
public class EmployeeMap : ClassMap<Employee>
{
public EmployeeMap ()
{
Id(x => x.Id);
Map(x => x.FirstName);
Map(x => x.LastName);
References(x => x.Store);
}
}
Store Map:
public class StoreMap:ClassMap<Store>
{
public StoreMap()
{
Id(x => x.Id);
Map(x => x.Name);
HasMany(x => x.Staff);
// HasManyToMany(x => x.Products).Cascade.All();
//.Table("StoreProduct");
}
}
When I run this code:
using (session.BeginTransaction())
{
var stores = session.CreateCriteria(typeof(Store)).List<Store>();
//for (int i=0; i<stores.Count;)
//{
// Response.Write(st
//}
foreach (var item in stores)
{
Response.Write(item.Staff.ToList());
}
}
I receive the following error:
could not initialize a collection: [test.Models.Store.Staff#1][SQL:
SELECT staff0_.Store_id as Store4_1_, staff0_.Id as Id1_, staff0_.Id
as Id0_0_, staff0_.LastName as LastName0_0_, staff0_.FirstName as
FirstName0_0_, staff0_.Store_id as Store4_0_0_ FROM [Employee] staff0_
WHERE staff0_.Store_id=?]
You code seems to work fine for me. But I'm generating an empty scheme from scratch, don't know if you are maybe missing something or if the id references are set correctly.
If you do not specify the reference Id column for staff->Store it uses Sore_id as you can see in your query text. Does this column exist?
Anyways, if it is still not working for you, try to explicitly define a name for the .KeyColumn for your HasMany(x => x.Staff); mapping
:edit:
You need to change StoreMap to have HasMany(x => x.Staff).KeyColumn("store");
And you need to change EmployeeMap to have References(x => x.Store).Columns("store");
So that both sides link to the same reference column...
I'm a newbie in MVC apps, and I've encountered a very specific problem. Thing is, I have 2 classes: "Paciente" and "Analises".
public class Paciente
{
public virtual Guid PacienteID { get; set; }
public virtual string Nome { get; set; }
public virtual string Sexo { get; set; }
public virtual DateTime DataDeNasc { get; set; }
public virtual int IdadeDiag { get; set; }
public virtual IList<Analises> analises { get; set; }
}
public class Analises
{
public virtual Guid AnaliseID { get; set; }
// some analysiss values
public virtual decimal afp { get; set; }
public virtual decimal hemoglobina { get; set; }
public virtual DateTime DataDaAnalise { get; set; }
public virtual Paciente pac { get; set; }
}
So, a Patient has many Analysis, and each Analysis has one Patient (on-to-many).
I have mapped this with NHIbernate and FluentNHibernate:
public PacienteMap()
{
Table("pacientes");
Id(x => x.PacienteID).GeneratedBy.Guid();
Map(x => x.Nome);
Map(x => x.Sexo);
Map(x => x.DataDeNasc).CustomType("Date");;
Map(x => x.IdadeDiag);
HasMany(m => m.analises).Not.KeyNullable().Fetch.Join().KeyColumn("pacienteid");
}
public AnalisesMap()
{
Table("analises");
Id(x => x.AnaliseID).GeneratedBy.Guid();
Map(x => x.afp);
Map(x => x.hemoglobina);
Map(x => x.DataDaAnalise).CustomType("Date");
References(x => x.pac).ForeignKey("pacienteid").Not.Nullable();
}
My problem is that I'm using jTable to show this. I want to see a list of Patients (and it works), and then, a list of analysis for each patient (doesn't work!)
My Controller goes like this:
[HttpPost]
public JsonResult AnalisesList(Guid pacienteId)
{
try
{
var list_analises = AnalisesRepository.GetPacienteAnalises(pacienteId);
var all_analises = Mapper.Map<IEnumerable<Analises>, IEnumerable<AnalisesView>>(list_analises);
List<AnalisesView> analises = all_analises.ToList();
return Json(new { Result = "OK", Records = analises });
}
catch (Exception ex)
{
return Json(new { Result = "ERROR", Message = ex.Message });
}
}
I am also using automapper to go from the Object Views from Database Objects.
So, rigth now, my analysis list does not show! I don't know why. The GetPacienteAnalises is like:
public IList<T> GetPacienteAnalises (Guid pacienteId)
{
using (ISession session = SessionManager.OpenSession())
{
using (ITransaction transaction = session.BeginTransaction())
{
return session.CreateCriteria(typeof(T)).Add(Restrictions.Eq("pacienteid", pacienteId)).List<T>();
}
}
}
So, I think everything is Okay... But I keep receiving a "could not resolve property: pacienteid of: Infraestrutura.Models.Analises".
Your problem is in this line:
return session.CreateCriteria(typeof(T))
.Add(Restrictions.Eq("pacienteid", pacienteId))
.List<T>();
I assume that T in this case is Analises. Analises does not have a property named pacienteid. Instead, it has a property named pac. Replace "pacienteid" with "pac.PacienteID". The fields that you use when creating the criteria should be the property names, not the column names.
return session.CreateCriteria(typeof(T))
.Add(Restrictions.Eq("pac.PacienteID", pacienteId))
.List<T>();
If you were filtering by some column other than Paciente's primary key, Nome for example, then a join would be necessary. In that case, you would do something like:
session.CreateCriteria(typeof(T))
.CreateAlias("pac", "p") // CreateAlias performs an inner join
.Add(Expression.Eq("p.Nome", name))
.List<T>()
But no join is needed to get to pac.PacienteID.
public class Category
{
public virtual int Id { set; get; }
public virtual string Name { set; get; }
public virtual int CategoryOrder { set; get; }
public virtual IEnumerable<News> LatestNews { set; get; }
}
public sealed class CategoryMap :ClassMap<Category>
{
public CategoryMap()
{
LazyLoad();
Id(x => x.Id);
Map(x => x.Name);
Map(x => x.CategoryOrder);
HasMany(x => x.LatestNews);
}
}
IRepository<Category> newsRepo = new NHibernateRepository<Category>();
using(var session = newsRepo.GetSessionFactory().OpenSession())
using(var transaction = session.BeginTransaction())
{
var result = session.Query<Category>().OrderBy(x => x.CategoryOrder);
transaction.Commit();
}
I have this category class Which I want to display a (only one) News per category. Is this correct mapping? or should i change it to Map
When i run this, it gets all the news per category. But i want the latest news per category (only one). I can get the latest news by querying News.DateUpdated.
How should i change the query to get one news per category?
or how do I get some of the News? ie: limit the number of news I can query?
I think you can have your cake and eat it too. You have a list of News items, so just leave that. But add in another property that gets just the News item you want.
public class Category
{
public virtual int Id { set; get; }
public virtual string Name { set; get; }
public virtual int CategoryOrder { set; get; }
public virtual IEnumerable<News> AllNews { set; get; }
public virtual News LatestNews
{
get
{
// probably needs some work
return this.AllNews.OrderByDescending(n => n.SomeDateField).Take(1);
}
}
}
public sealed class CategoryMap :ClassMap<Category>
{
public CategoryMap()
{
LazyLoad();
Id(x => x.Id);
Map(x => x.Name);
Map(x => x.CategoryOrder);
HasMany(x => x.AllNews);
}
}
you can use lazy mode extra to achieve this,
public News GetLatestNews(Category cat)
{
var session = SessionFactory.CurrentSession;
var news = session.CreateFilter(cat.LatestNews , "order by categoryorder").SetFirstResult((page - 1) * pageSize).SetFirstResult(0).SetMaxResults(1).List().FirstOrDefault();
return news;
}
however if you try to access the LatestNews collection directly, this won't be of any use as it will load all the news objects associated with Category from db which would lower the performance.
Is there a way I can setup mapping for a table that doesn't have a direct reference to another table? It actually gets it's reference from another table that I do have a direct reference from.
This is what I have so far, but I'm not sure how to map the "LookupValue" in my MetaData model. It would need to map to MetaData if the [mdd].DefinitionType equals the [mdl].LookupType and the [md].DataValue equals the [mdl].LookupKey.
public class MetaData {
public virtual long TableID { get; set; }
public virtual MetaDataDefinition Definition { get; set; }
public virtual int DefinitionID { get; set; }
public virtual String DataValue { get; set; }
public virtual MetaDataLookup LookupValue { get; set; }
public override bool Equals(object obj) { ... }
public over int GetHashCode() { ... }
}
public class MetaDataDefinition {
public virtual long ID { get; set; }
public virtual string DefinitionName { get; set; }
public virtual string DefinitionType { get; set; }
}
public class MetaDataLookup {
public virtual string Type { get; set; }
public virtual string LookupKey { get; set; }
public virtual string LookupValue { get; set; }
public override bool Equals(object obj) { ... }
public over int GetHashCode() { ... }
}
public class MetaDataMap : ClassMap<MetaData> {
public MetaDataMap() {
Table("PPOMetaData");
CompositeId()
.KeyProperty(x => x.TableID, "TableID")
.KeyProperty(x => x.DefinitionID, "DefinitionID");
References(x => x.Defintion, "DefinitionID").Not.LazyLoad().Cascade.All().Fetch.Join();
Map(x => x.TableID);
Map(x => x.DataValue);
}
}
public class MetaDataDefinitionMap : ClassMap<MetaDataDefinition> {
public MetaDataDefinitionMap() {
Table("MetaDataDefinitions");
Id(x => x.ID);
Map(x => x.DefinitionName);
Map(x => x.Type);
}
}
public class MetaDataLookupMap : ClassMap<MetaDataLookup> {
public MetaDataLookupMap() {
CompositeId()
.KeyProperty(x => x.LookupType)
.KeyProperty(x => x.LookupKey);
Map(x => x.LookupValue);
}
}
Ideally, I want to have it run a query similar to this:
SELECT data.TableID, data.DefinitionID, def.DefinitionName, data.DataValue,lu.LookupValue AS DataValue
FROM dbo.PPOMetadata AS data
INNER JOIN dbo.MetaDataDefinitions AS def ON def.ID = data.DefinitionID
LEFT OUTER JOIN dbo.MetaDataLookup AS lu ON lu.LookupType = def.Type AND lu.LookupKey = data.DataValue
WHERE data.TableID = 1
In terms of update ability, the only thing I would ever create, update or delete would be in the MetaData table. The definitions and Lookup values would never change (at least from this part of the application). Is mapping the "MetaDataLookup" directly to the MetaData model possible? If so, can someone point me in the right direction of what I should be looking at?
Thanks!
I came up with a workaround that seems to be working and might take some of the complexity out. Instead of trying to handle the complex joins in a ClassMap, I built a view in Sql Server that does this for me. In my application, I built a new Model and ClassMap for the view. I haven't implemented any update logic yet, but I think I'll have the update logic work directly on the MetaData model, while the read logic (which needs everything joined together) will use the new MetaDataView model.
I'm still curious if complex joins like this are possible in Fluent NHibernate, but for now this solution seems to be working for me.