NHibernate using wrong column name for association - nhibernate

//Map for Url class
this.Table("Urls");
this.Id(x => x.ID).GeneratedBy.GuidComb().Access.BackingField();
this.Map(x => x.Address).Access.BackingField();
this.HasMany(x => x.Parameters)
.Inverse()
.AsList(col => col.Column("ParameterIndex"))
.Cascade.AllDeleteOrphan()
.Access.BackingField();
//Map for UrlParameter class
this.Table("UrlParameters");
this.Id(x => x.ID).GeneratedBy.GuidComb().Access.BackingField();
this.References(x => x.Url).Column("Url").Access.BackingField();
this.Map(x => x.Name).Access.BackingField();
//Code to execute the query
Session.Query<Url>().FetchMany(x => x.Parameters);
//The SQL generated
select url0_.ID as ID1_0_, parameters1_.ID as ID2_1_, url0_.Address as Address1_0_, url0_.Prohibited as Prohibited1_0_, url0_.CreatedOn as CreatedOn1_0_, url0_.LastUpdatedOn as LastUpda5_1_0_, url0_.TotalComments as TotalCom6_1_0_, url0_.TotalNegative as TotalNeg7_1_0_, url0_.TotalNeutral as TotalNeu8_1_0_, url0_.TotalPositive as TotalPos9_1_0_, url0_.CreatedBy as CreatedBy1_0_, parameters1_.Name as Name2_1_, parameters1_.Url as Url2_1_, parameters1_.Url_id as Url4_0__, parameters1_.ID as ID0__, parameters1_.ParameterIndex as Paramete5_0__ from Urls url0_ left outer join UrlParameters parameters1_ on url0_.ID=parameters1_.Url_id where url0_.Address=#p0
//Exception message
Message=Invalid column name 'Url_id'.
Why is NHibernate generating a column name "Url_id" when I have explicitly told it to use "Url" in my UrlParameter mapping?

You need to define the KeyColumn() column in the HasMany mapping.
It should match what you wrote on the References() side ("Url").

Related

Delete an item from many-to-many relationship

I've following mapping for two tables having a Many-to-Many relationship between them. How do I delete an entry from the mapping table, which is 'TB_EMAIL_GRUPO_ARQUIVO' in my case? I just need to delete the data from this "joining" table and I donĀ“t need to delete it from the "parent tables".
GrupoModulo
public GrupoModuloMap()
{
Schema(Const.SCHEMA);
Table(Const.TB_EMAIL_GRUPO_MODULO);
CompositeId()
.KeyReference(x => x.Grupo, Const.ID_GRUPO)
.KeyReference(x => x.Modulo, Const.ID_MODULO);
Map(x => x.GrupoId).Column(Const.ID_GRUPO).ReadOnly();
Map(x => x.ModuloId).Column(Const.ID_MODULO).ReadOnly();
HasManyToMany(x => x.Arquivos)
.Table(Const.TB_EMAIL_GRUPO_ARQUIVO)
.ParentKeyColumns.Add(Const.ID_GRUPO, Const.ID_MODULO)
.ChildKeyColumn(Const.ID_ARQUIVO)
.Cascade.SaveUpdate()
.Not.LazyLoad();
}
ArquivoRetorno
public ArquivoRetornoMap()
{
Schema(Const.SCHEMA);
Table(Const.TB_EMAIL_ARQUIVO_RETORNO);
Id(x => x.Id)
.Column(Const.ID_ARQUIVO)
.GeneratedBy.Sequence("SEQ_TB_EMAIL_ARQUIVO_RETORNO")
.Length(7).CustomSqlType("number(7)")
.Not.Nullable();
Map(x => x.Nome)
.Column("NM_ARQUIVO_RETORNO")
.Length(50)
.Not.Nullable();
References(x => x.Modulo)
.Column(Const.ID_MODULO)
.Not.Nullable();
HasManyToMany(x => x.GrupoModulos)
.Table(Const.TB_EMAIL_GRUPO_ARQUIVO)
.ChildKeyColumns.Add(Const.ID_GRUPO, Const.ID_MODULO)
.ParentKeyColumn(Const.ID_ARQUIVO)
.Cascade.SaveUpdate()
.Not.LazyLoad();
}
Whenever I try to delete I'm getting the following error:
deleted object would be re-saved by cascade (remove deleted object from associations)[Domain.Entity.GrupoModulo#Domain.Entity.GrupoModulo]
Someone has any idea?
The answer is (I am really sure) here: NHibernate Deleted object would be re-saved by cascade
Let me re-phrase that for your case, what could happen:
we remove an GrupoArquivo from ArquivoRetorno.GrupoModulos collection.
During that transaction, unit of work, we also tuch and therefore load the GrupoModulo
GrupoModulo gets initiated - and NOW - collection of Arquivos is loaded. So the reference to removed GrupoArquivo is kept there
NHibernate MUST inform: Deleted object would be re-saved by cascade
Solution(s):
be sure that the GrupoModulo is never loaded (stays as proxy)
or (the way I use) Remove() the GrupoArquivo also from GrupoModulo.Arquivos
or do NOT use CASCADE mapping on GrupoArquivo side:
(do not use the cascade)
HasManyToMany(x => x.Arquivos)
.Table(Const.TB_EMAIL_GRUPO_ARQUIVO)
.ParentKeyColumns.Add(Const.ID_GRUPO, Const.ID_MODULO)
.ChildKeyColumn(Const.ID_ARQUIVO)
//.Cascade.SaveUpdate()
// here
.Cascade.None()
.Not.LazyLoad();

For Fluent NHibernate experts: Join with where condition

SQL Server syntax is:
select tableAColumn1, tableAColumn2, tableBColumn1
from tableA, tableB
where ISNUMERIC(tableAColumn1) = 1
and CONVERT(INT, tableAColumn1) = tableBColumn1
and tableAColumn2 = 'something'
What would be the best way to achieve this in Fluent NHibernate? How many classes would I need to help get the resulting ClassMap and how would it look like?
Edit:
public class BarausInfoMap : ClassMap<BarausInfo>
{
public BarausInfoMap()
{
Table("BARAUS");
Id(x => x.nr);
Map(x => x.betrag);
Join("BARAUSLANG", m =>
{
m.Fetch.Join();
m.KeyColumn("Ula");
m.Map(x => x.bezeichnung);
m.Map(x => x.sprache);
m.Map(x => x.la);
this.Where("m.la = 'SPE'");
});
}
}
nr column is int and ula column is string, but I need to join those 2. also, the this.where refers to the outer table I guess, it should however refer to the inner table.
Maybe it's better to use separate entities and separate mapping, than you build query like
queryOver<BarausInfo>.JoinQueryOver(x => x.BarauslangObject, barauslangAlias, JoinType.InnerJoin, conjunction)
and the conjunction will contain the ISNUMERIC(tableAColumn1) = 1 and tableAColumn2 = 'something'. And n the BarausInfo Mapping you can spefify something like References(v => v.BarauslangObject).Formula("CONVERT(INT, tableAColumn1) = tableBColumn1"). Just find out how to specify columns in formula properly.

Fluent NHibernate one of Primary Keys is also Foreign key - OutOfRangeException

Cart
{
CompositeId().KeyProperty(x => x.CART_ID, "CART_ID").KeyProperty(x => x.COMM_CD, "COMM_CD");
References(x => x.Product, "COMM_CD");
}
When I tried to save cart object into database I received out of range exception. I think it's because I have two COMM_CD properties. Is there a way to solve this problem ?
Thanks
You need to use KeyReference instead of KeyProperty
CompositeId()
.KeyProperty(x => x.CART_ID, "CART_ID")
.KeyReference(x => x.Product, "COMM_CD");

Optional many-to-one/References results in foreign key violations on insert

I currently have the following relationship: ProductUom -> ProductImage
They both have the same primary keys: PROD_ID and UOM_TYPE
I have them mapped like this:
public ProductUomMap()
{
Table("PROD_UOM");
CompositeId()
.KeyReference(x => x.Product, "PROD_ID")
.KeyProperty(x => x.UomType, "UOM_TYPE");
References(x => x.Image)
.Columns(new string[] { "PROD_ID", "UOM_TYPE" })
.Not.Update()
.Not.Insert()
.NotFound.Ignore()
.Cascade.All();
}
public ProductImageMap()
{
Table("PROD_UOM_IMAGE");
CompositeId()
.KeyReference(x => x.ProductUom, new string[] {"PROD_ID", "UOM_TYPE"});
Map(x => x.Image, "PROD_IMAGE").Length(2147483647);
}
Whenever I create a ProductUom object that has a ProductImage it tries to insert the ProductImage first which results in a foreign key violation. I swear this was working at one time with the mapping that I have but it doesn't now.
I need the ProductImage to be a Reference (many-to-one) because the relationship here is optional and I want to be able to lazy load product images. The inserts do work correctly if I use a HasOne (one-to-one) mapping but the I cannot lazy load when I do this and querying a ProductUom seems to cause issues.
Is there something that I'm missing here? How can this mapping be modified to get what I want?
can you use LazyLoaded Properties? Then you could use something like this
Join("PROD_UOM_IMAGE", join =>
{
join.KeyColumn("PROD_ID", "UOM_TYPE");
join.Optional();
join.Map(x => x.Image, "PROD_IMAGE").Length(2147483647).LazyLoad();
}
another option is:
Id().GeneratedBy.Foreign(x => x.ProductUom);
can't test it here though, i'm writing on Mobile

Duplicate Items Using Join in NHibernate Map

I am trying to retrieve the individual detail rows without having to create an object for the parent. I have a map which joins a parent table with the detail to achieve this:
Table("UdfTemplate");
Id(x => x.Id, "Template_Id");
Map(x => x.FieldCode, "Field_Code");
Map(x => x.ClientId, "Client_Id");
Join("UdfFields", join =>
{
join.KeyColumn("Template_Id");
join.Map(x => x.Name, "COLUMN_NAME");
join.Map(x => x.Label, "DISPLAY_NAME");
join.Map(x => x.IsRequired, "MANDATORY_FLAG")
.CustomType<YesNoType>();
join.Map(x => x.MaxLength, "DATA_LENGTH");
join.Map(x => x.Scale, "DATA_SCALE");
join.Map(x => x.Precision, "DATA_PRECISION");
join.Map(x => x.MinValue, "MIN_VALUE");
join.Map(x => x.MaxValue, "MAX_VALUE");
});
When I run the query in NH using:
Session.CreateCriteria(typeof(UserDefinedField))
.Add(Restrictions.Eq("FieldCode", code)).List<UserDefinedField>();
I get back the first row three times as opposed to the three individual rows it should return. Looking at the SQL trace in NH Profiler the query appears to be correct. The problem feels like it is in the mapping but I am unsure how to troubleshoot that process. I am about to turn on logging to see what I can find but I thought I would post here in case someone with experience mapping joins knows where I am going wrong.
Found aboutn about SetResults transformer here :
http://www.coderanch.com/t/216546/ORM/java/Hibernate-returns-duplicate-results
Which makes your code:
Session.CreateCriteria(typeof(UserDefinedField))
.Add(Restrictions.Eq("FieldCode", code))
.SetResultTransformer(CriteriaSpecification.DistinctRootEntity)
.List<UserDefinedField>();
Cheers
John