今週はNHibernateを使い始めました(苦労しています)。私は3つのテーブルを持つ小さなアプリケーションを持っています(私は現在2つ使用しています)。テーブルcurrency
とテーブルcountry
は、マッピングファイルです。NHibernate 2.0.1で子テーブルに挿入
<class name="dataprovider.Country,dataprovider" table="country">
<id name="CountryId" column="country_id" unsaved-value="0">
<generator class="native"/>
</id>
<!--<bag name="BatchList" inverse="true" lazy="true" >
<key column="country_id" />
<one-to-many class="Batch" />
</bag>
<bag name="PrinterList" inverse="true" lazy="true" >
<key column="country_id" />
<one-to-many class="Printer" />
</bag>-->
<many-to-one name="CurrencyId" column="currency_id" class="Currency" cascade="save-update"/>
<!--<property column="currency_id" name="Currency_Id"/>-->
<property column="name" name="Name"/>
<property column="region" name="Region" />
<property column="comments" name="Comments"/>
</class>
通貨マッピングファイル:
<class name="dataprovider.Currency, dataprovider" table="currency">
<id name="CurrencyId" column="currency_id" >
<generator class="native"/>
</id>
<bag name="CountryList" inverse="true" lazy="true" >
<key column="currency_id" />
<one-to-many class="Country" />
</bag>
<!--<bag name="DenominationList" inverse="true" lazy="true" >
<key column="currency_id" />
<one-to-many class="Denomination" />
</bag>-->
<property column="name" name="Name"/>
<property column="authorizer" name="Authorizer" />
<property column="date_created" name="DateCreated" type="DateTime" not-null="true" />
<property column="comments" name="Comments" />
country
がcountry
永続クラスのタイプCurrency
の属性を作成保持することを1人の関係に多くの。私のテストcan_add_currency
とcan_add_country
が成功しました(私はスキーマをエクスポートしました)、フィールドcurrency_id
のcountry
テーブルにNULL値があります。ここで
は、テストコードです:
[Test]
public void can_add_new_country()
{
CountryManager cm = new CountryManager();
Country country = new Country();
//country.CurrencyId = CurrencyManager.GetCurrencyById(1);
country.CurrencyId = new CurrencyManager().GetCurrencyById(1);
country.Name = "POUND";
country.Region = "ENGLAND";
country.Comments = "the comment";
cm.Add(country);
using(ISession session = _sessionFactory.OpenSession())
{
Country fromdb = session.Get<Country>(country.CountryId);
Assert.IsNotNull(fromdb);
Assert.AreNotSame(fromdb, country);
}
}
public Currency GetCurrencyById(int currency_id)
{//GetCurrencyById from CurrencyManger class
try
{
using(ISession session = NHibernateHelper.OpenSession())
{
return session.Get<Currency>(currency_id);
}
} catch (Exception ex)
{
return null;
}
}
質問です:テーブル通貨から既存currency_idのcurrency_idとテーブル国に挿入する方法?
あなたはどのようにしていますか?私は真剣に立ち往生しています.2日間の小規模なプロジェクトで今一週間服用しています。
私は自分の答えを更新しました。うまくいけば、あなたが求めている質問に答えます。 –
私はそれを読んでくれてありがとう。ちょっとはっきりしています。私は、Jbossが冬眠を買ったように見えるので、何が起きるのだろうかと思っています。 –