These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 
Author Message
 Post subject: Looking some sollution for my problem
PostPosted: Wed Oct 08, 2008 9:53 am 
Newbie

Joined: Wed Oct 08, 2008 7:40 am
Posts: 1
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'


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 08, 2008 10:12 am 
Expert
Expert

Joined: Mon Nov 26, 2007 2:29 pm
Posts: 443
Apparently in your case, a one-to-one association would be the right way to go. Currency.getName()

I would make it a left join (so that currencies are returned even when no name is attached, and then I would resolve the default inside the accessor method of the class itself, (i.e., if the name is null, return a default according to the system's locale).

_________________
Gonzalo Díaz


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 2 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.