Hibernate version:
2.0.0.3001
Mapping documents:
Currency.hbm.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="NHibernateTest.Currency,NHibernateTest" table="Currency" lazy="false">
<id name="IdCurrency" column="IDCurrency" type="int">
<generator class="native" />
</id>
<property type="string" not-null="true" length="5" name="Symbol" column="[Symbol]" />
<bag name="FkCurrencyName" inverse="true" lazy="true" cascade="all">
<key column="IDCurrency" />
<one-to-many class="NHibernateTest.CurrencyName,NHibernateTest" />
</bag>
</class>
</hibernate-mapping>
CurrencyName.hbm.xml
Code:
<?xml version="1.0" encoding="utf-8"?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2">
<class name="NHibernateTest.CurrencyName,NHibernateTest" table="CurrencyName" lazy="false">
<id name="IdCurrencyName" column="IDCurrencyName" type="int">
<generator class="native" />
</id>
<property type="string" not-null="true" length="5" name="Culture" column="[Culture]" />
<property type="string" not-null="true" length="50" name="Name" column="[Name]" />
<many-to-one name="IdCurrency" cascade="none" column="IDCurrency" not-null="true" />
</class>
</hibernate-mapping>
Code between sessionFactory.openSession() and session.close():ICurrency.cs
Code:
using System;
using System.Collections;
using System.Collections.Generic;
namespace NHibernateTest
{
public interface ICurrency
{
int IdCurrency { get ; set ; }
string Symbol { get ; set ; }
IList<ICurrencyName> FkCurrencyName { get ; set ; }
ICurrencyName DefaultName { get; }
}
}
ICurrencyName.cs
Code:
using System;
using System.Collections;
using System.Collections.Generic;
namespace NHibernateTest
{
public interface ICurrencyName
{
int IdCurrencyName { get ; set ; }
Currency IdCurrency { get ; set ; }
string Culture { get ; set ; }
string Name { get ; set ; }
}
}
Currency.cs and CurrencyName.cs have some simple code
Full stack trace of any exception that occurs:////
Name and version of the database you are using:Microsoft SQL Server Express Edition 9.00.3042.00
The generated SQL (show_sql=true):////
Debug level Hibernate log excerpt:////
test data in tables:
Currency:
Code:
IDCurrency Symbol
1 $
2 e
CurrencyName:
Code:
IDCurrencyName IDCurrency Culture Name
1 1 en-US Dollar
2 1 fr-Fr Doll..
3 1 mk-MK .о.ар
4 2 en-US Euro
Currency is the main table, Currencyname is table only has Names of the Currency in different cultures.
Currenxy has IList<ICurrencyName> FkCurrencyName that has all the names in all diferent cultures.
Most of the time FkCurrencyName will not be used. Instead we will need the CurrencyName of the default culture 'en-US' for ex.
Because of that i made new propery DefaultName in Currency.cs.
Code:
public ICurrencyName DefaultName
{
get
{
List<ICurrencyName> t = new List<ICurrencyName>(FkCurrencyName);
return t.Find(f => f.Culture == "en-US");
}
}
But i dont like this kind of approach because if i want to get the DefaultName first i must get all the names and then filter.
I tried to map the DefaultName property in hbm file with one-on-one but i got wrong results.
My question is. Can i somehow map the DefaultName in hbm file but the result sholud depend on culture parameter.
The SQL generated code for the first Currency should somehow look like this:DefaultName = SELECT * FROM CurrencyName WHERE IDCurrency = 1 AND Culture='en-US'