Hi
I'm trying to get data out of the database with the following code:
myCOBDefProperty =
(COBDefProperty)NHSession.CreateCriteria(typeof(COBDefProperty))
.Add(Expression.Like("Id", 23))
.CreateCriteria("Names").Add(Expression.Like("LangCode", "e%"))
.UniqueResult();
I should get only 1 'Names' object (linked to a 'COBDefProperty' object) which contains the value 'e' in the field LangCode.
But I get 2 'Names' objects out of the database.
Both have the foreign key value 23 but one has the value 'en' in the field LangCode, the other one has the value 'am' in the field LangCode.
How is this possible as I define an expression (like 'e') in the LangCode field?
Below my mapping files:
----------------------------
COBDefProperty
Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="spikes.solutions.ccms.COBDefProperty.COBDefProperty, Test" table="COBDefProperty">
<id name="Id" column="Id" type="Int32" unsaved-value="0">
<generator class="sequence">
<param name="sequence">SQ_COBDEFPROPERTY</param>
<param name="schema">CCMS</param>
</generator>
</id>
<property name="DefaultValue" column="DefaultValue" type="String" />
<bag name="Names" cascade="all" lazy="false">
<key column="COBDefPropertyId" />
<one-to-many class="spikes.solutions.ccms.COBDefProperty.COBDefPropertyName, Test" />
</bag>
</class>
</hibernate-mapping>
COBDefPropertyNameCode:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="spikes.solutions.ccms.COBDefProperty.COBDefPropertyName, Test" table="COBDefPropertyName">
<id name="Id" column="Id" type="Int32" unsaved-value="0">
<generator class="sequence">
<param name="sequence">SQ_COBDEFPROPERTYNAME</param>
<param name="schema">CCMS</param>
</generator>
</id>
<property name="LangCode" column="LangCode" type="String" />
<property name="Value" column="Name" type="String" />
</class>
</hibernate-mapping>
Below the code:
------------------
Code:
public partial class COBDefProperty
{
private int idField;
private IList<COBDefPropertyName> namesField;
private string defaultValueField;
ISession NHSession;
ISessionFactory NHFactory;
public COBDefProperty()
{
NHibernate.Cfg.Configuration NHConfig = new NHibernate.Cfg.Configuration();
NHConfig.Configure("/NHibernate.cfg.xml");
NHFactory = NHConfig.BuildSessionFactory();
log4net.Config.XmlConfigurator.Configure();
}
public virtual int Id
{
get
{
return this.idField;
}
set
{
this.idField = value;
}
}
public virtual IList<COBDefPropertyName> Names
{
get
{
return this.namesField;
}
set
{
this.namesField = value;
}
}
public virtual string DefaultValue
{
get
{
return this.defaultValueField;
}
set
{
this.defaultValueField = value;
}
}
public virtual COBDefProperty Get(int myCOBDefPropertyId, string myLangCode)
{
COBDefProperty myCOBDefProperty = new COBDefProperty();
NHSession = NHFactory.OpenSession();
try
{
myCOBDefProperty = (COBDefProperty)NHSession.CreateCriteria(typeof(COBDefProperty)).Add(Expression.Like("Id", 23))
.CreateCriteria("Names").Add(Expression.Like("LangCode", "e%"))
.UniqueResult();
}
catch (Exception ex)
{
throw ex;
}
finally
{
NHSession.Close();
}
return myCOBDefProperty;
}
}
public partial class COBDefPropertyName
{
private string langCodeField;
private string valueField;
private int idField;
public virtual string LangCode
{
get
{
return this.langCodeField;
}
set
{
this.langCodeField = value;
}
}
public virtual string Value
{
get
{
return this.valueField;
}
set
{
this.valueField = value;
}
}
public virtual int Id
{
get
{
return this.idField;
}
set
{
this.idField = value;
}
}
}
In the logging, I found the following.
It looks like, after the right SQL statement, a new statement is executed with only 1 criteria namely the Id 23.
Code:
11:59:08.842 [9] DEBUG NHibernate.SQL - SELECT this_.Id as Id1_1_, this_.DefaultValue as DefaultV2_1_1_, cobdefprop1_.Id as Id0_0_, cobdefprop1_.LangCode as LangCode0_0_, cobdefprop1_.Name as Name0_0_ FROM COBDefProperty this_ inner join COBDefPropertyName cobdefprop1_ on this_.Id=cobdefprop1_.COBDefPropertyId WHERE this_.Id like :p0 and cobdefprop1_.LangCode like :p1; :p0 = '23', :p1 = 'e%'
A moment later, the following SQL statement is executed.
Code:
11:59:09.061 [9] DEBUG NHibernate.SQL - SELECT names0_.COBDefPropertyId as COBDefPr4___1_, names0_.Id as Id1_, names0_.Id as Id0_0_, names0_.LangCode as LangCode0_0_, names0_.Name as Name0_0_ FROM COBDefPropertyName names0_ WHERE names0_.COBDefPropertyId=:p0; :p0 = '23'
Thanks for the help!!!