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.  [ 3 posts ] 
Author Message
 Post subject: Turn IList<Localized> to IDictionary<Language, Loca
PostPosted: Wed Jul 25, 2007 2:09 am 
Newbie

Joined: Tue Jul 24, 2007 3:27 pm
Posts: 3
Please help, i can't figure out how to put collection items to an IDictionary indexed by the third class, There is an example in documentation, but i can't figure out the complete setup: http://www.hibernate.org/hib_docs/nhibernate/html_single/#collections-ternary

I have three tables

Language(Id, ... )
GlobalString(Id, Name )
GlobalStringLocalized(GlobalStringId, LanguageId, Value )

mapping
Code:
<class name="GlobalString" table="GlobalString" >
   <id name="ID" type="System.Int32" column="ID">
      <generator class="identity"/>
   </id>
   <property name="Name" column="Name" type="System.String" not-null="true" length="200"/>
   <bag name="GlobalStringlocalizeds" table="GlobalStringlocalized" inverse="true" lazy="true" cascade="delete">
      <key column="GlobalStringID" />
      <one-to-many class="GlobalStringlocalized"/>
   </bag>
</class>


<class name="GlobalStringlocalized" table="GlobalStringlocalized" >
   <composite-id name="ID" class="GlobalApp.Core.Domain.GlobalStringlocalized+DomainObjectID">
      <key-property type="System.Int32" name="GlobalStringID" column="GlobalStringID" />
      <key-property type="System.Int32" name="LanguageID" column="LanguageID" />
   </composite-id>
   <property name="Value" column="Value" type="System.String" not-null="true" />
   <many-to-one name="GlobalStringIDGlobalString" column="GlobalStringID" class="GlobalString"  update="0"  insert="0" />
   <many-to-one name="LanguageIDLanguage" column="LanguageID" class="Language"  update="0"  insert="0" />
</class>


<class name="Language" table="Language" >
   <id name="ID" type="System.Int32" column="ID">
      <generator class="identity"/>
   </id>
   <property name="LanguageTag" column="LanguageTag" type="System.String" not-null="true" length="10"/>
   <property name="NativeName" column="NativeName" type="System.String" not-null="true" length="30"/>

   <bag name="GlobalStringlocalizeds" table="GlobalStringlocalized" inverse="true" lazy="true" cascade="delete">
      <key column="LanguageID" />
      <one-to-many class="GlobalStringlocalized"/>
   </bag>
   <bag name="StoreLocalizeds" table="StoreLocalized" inverse="true" lazy="true" cascade="delete">
      <key column="LanguageID" />
      <one-to-many class="StoreLocalized"/>
   </bag>
</class>


Domain
Code:
public class GlobalString : DomainObject<System.Int32>
{
    //...
    public virtual IList<GlobalStringlocalized> GlobalStringlocalizeds{
        get { return _GlobalStringlocalizeds; }
        set { _GlobalStringlocalizeds = value; }
    }
    //...
}


With this setup i have to use queries to get localized fields of a given language:

Dao
Code:
public class AbstractDaoLocalized<GlobalizedType, LocalizedType>
{
public IList<LocalizedType> GetLocalizedBy(GlobalizedType entity, Language language)
{
  ISession session = NHibernateSessionManager.Instance.GetSession();
  string name = typeof(GlobalizedType).Name;
  return session.CreateCriteria(typeof(LocalizedType))
  .Add(Expression.Eq(name + "ID" + name, entity))
  .Add(Expression.Eq("LanguageIDLanguage", language))
   .List<LocalizedType>();
}
}


How do i change the mapping to use IDictionary<Language, GlobalStringLocalized> instead of IList<GlobalStringLocalized>?
so i could avoid using multiple Daos?

This is how i want it to be:

Code:
... GetGlobalStrings(Language language)
...
foreach(GlobalString gs in session.CreateCriteria(typeof(GlobalString)).List<GlobalString>())
{
  string byLanguage = gs[language].Value;
  // or
  // string byLanguage = gs[language.Id].Value;
..
}

public class GlobalString : DomainObject<System.Int32>
{
    //...
   public virtual IDictionary<Language, GlobalStringLocalized> GlobalStringlocalizeds
     {
        get { return _GlobalStringlocalizeds; }
        set { _GlobalStringlocalizeds = value; }
     };
    //...
}

// or

public class GlobalString : DomainObject<System.Int32>
{
    //...
   public virtual IDictionary<Int32/*Language.Id*/, GlobalStringLocalized> GlobalStringlocalizeds
     {
        get { return _GlobalStringlocalizeds; }
        set { _GlobalStringlocalizeds = value; }
     };
    //...
}


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 25, 2007 4:17 am 
Beginner
Beginner

Joined: Wed Oct 04, 2006 8:57 am
Posts: 25
I am not too familiar with mapping dictionaries, but from a quick look at the documentation I think you need to use a <map> rather than a <bag>

Code:
<map name="Connections" lazy="true">
    <key column="node1_id"/>
    <index-many-to-many column="node2_id" class="Node"/>
    <many-to-many column="connection_id" class="Connection"/>
</map>


looks like the key thing is to tell it what the index is.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jul 26, 2007 1:04 pm 
Newbie

Joined: Tue Jul 24, 2007 3:27 pm
Posts: 3
I have finally figured out how to do it.

Code
Code:

    public class Language : DomainObject<System.Int32>
    {
        private System.String _LanguageTag;
        private System.String _NativeName;
        private IList<CityLocalized> _CityLocalizeds = new List<CityLocalized>();
        private IList<GlobalStringlocalized> _GlobalStringlocalizeds = new List<GlobalStringlocalized>();
        private IList<StoreLocalized> _StoreLocalizeds = new List<StoreLocalized>();

        public Language()
        {
        }

        public Language(System.Int32 id)
        {
            base.ID = id;
        }

         public virtual System.String LanguageTag {
             get { return _LanguageTag; }
             set { _LanguageTag = value;}
         }

         public virtual System.String NativeName {
             get { return _NativeName; }
             set { _NativeName = value;}
         }

         public virtual IList<CityLocalized> CityLocalizeds{
             get { return _CityLocalizeds; }
             set { _CityLocalizeds = value; }
         }

         public virtual IList<GlobalStringlocalized> GlobalStringlocalizeds{
             get { return _GlobalStringlocalizeds; }
             set { _GlobalStringlocalizeds = value; }
         }

         public virtual IList<StoreLocalized> StoreLocalizeds{
             get { return _StoreLocalizeds; }
             set { _StoreLocalizeds = value; }
         }


        public override int GetHashCode()
        {
            return ID.GetHashCode();
        }

     }


public class GlobalString : DomainObject<System.Int32>
    {
        private System.String _Name;
        private IDictionary<Int32, GlobalStringlocalized> _GlobalStringlocalizeds = new Dictionary<Int32,GlobalStringlocalized>();

        public GlobalString()
        {
        }

        public GlobalString(System.Int32 id)
        {
            base.ID = id;
        }

         public virtual System.String Name {
             get { return _Name; }
             set { _Name = value;}
         }

      public virtual IDictionary<Int32, GlobalStringlocalized> GlobalStringlocalizeds
      {
             get { return _GlobalStringlocalizeds; }
             set { _GlobalStringlocalizeds = value; }
         }

        public override int GetHashCode()
        {
            return ID.GetHashCode();
        }
     }

    public class GlobalStringlocalized : DomainObject<GlobalStringlocalized.DomainObjectID>
    {

        [Serializable]
        public class DomainObjectID
        {
            public DomainObjectID() {}

            private System.Int32 _GlobalStringID;
            private System.Int32 _LanguageID;

            public DomainObjectID(System.Int32 globalStringID, System.Int32 languageID)
            {
                _GlobalStringID = globalStringID;
                _LanguageID = languageID;
            }

         public System.Int32 GlobalStringID {
             get { return _GlobalStringID; }
             protected set { _GlobalStringID = value;}
         }

         public System.Int32 LanguageID {
             get { return _LanguageID; }
             protected set { _LanguageID = value;}
         }


         public override bool Equals(object obj)
         {
             if (obj == this) return true;
             if (obj == null) return false;

             DomainObjectID that = obj as DomainObjectID;
             if (that == null)
             {
                 return false;
             }
             else
             {
                 if (this.GlobalStringID != that.GlobalStringID) return false;
                 if (this.LanguageID != that.LanguageID) return false;

                 return true;
             }

         }

            public override int GetHashCode()
            {
                return GlobalStringID.GetHashCode() ^ LanguageID.GetHashCode();
            }

        }

        private System.String _Value;
        private GlobalString _GlobalStringIDGlobalString;
        private Language _LanguageIDLanguage;

        public GlobalStringlocalized()
        {
        }

        public GlobalStringlocalized(DomainObjectID id)
        {
            base.ID = id;
        }

         public virtual System.Int32 GlobalStringID {
             get { return base.id.GlobalStringID; }
         }

         public virtual System.Int32 LanguageID {
             get { return base.id.LanguageID; }
         }

         public virtual System.String Value {
             get { return _Value; }
             set { _Value = value;}
         }

         public virtual GlobalString GlobalStringIDGlobalString{
             get { return _GlobalStringIDGlobalString; }
             set { _GlobalStringIDGlobalString = value;}
         }

         public virtual Language LanguageIDLanguage{
             get { return _LanguageIDLanguage; }
             set { _LanguageIDLanguage = value;}
         }


        public override int GetHashCode()
        {
            return ID.GetHashCode();
        }

     }





Mapping


Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NVozrast.Core" namespace="NVozrast.Core.Domain">
    <class name="GlobalString" table="GlobalString" >
    <id name="ID" type="System.Int32" column="ID">
        <generator class="identity"/>
    </id>
    <property name="Name" column="Name" type="System.String" not-null="true" length="200"/>
    <map name="GlobalStringlocalizeds" table="GlobalStringlocalized" lazy="true">
      <key column="GlobalStringID" />
      <index column="LanguageID" type="Int32" />
      <one-to-many class="GlobalStringlocalized" />
    </map>
</class>
</hibernate-mapping>


<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NVozrast.Core" namespace="NVozrast.Core.Domain">
   <class name="GlobalStringlocalized" table="GlobalStringlocalized" >
      <composite-id name="ID" class="NVozrast.Core.Domain.GlobalStringlocalized+DomainObjectID">
         <key-property type="System.Int32" name="GlobalStringID" column="GlobalStringID" />
         <key-property type="System.Int32" name="LanguageID" column="LanguageID" />
      </composite-id>
      <property name="Value" column="Value" type="System.String" not-null="true" />
      <many-to-one name="GlobalStringIDGlobalString" column="GlobalStringID" class="GlobalString"  update="0"  insert="0" />
      <many-to-one name="LanguageIDLanguage" column="LanguageID" class="Language"  update="0"  insert="0" />
   </class>
</hibernate-mapping>

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NVozrast.Core" namespace="NVozrast.Core.Domain">
    <class name="Language" table="Language" >
    <id name="ID" type="System.Int32" column="ID">
        <generator class="identity"/>
    </id>
    <property name="LanguageTag" column="LanguageTag" type="System.String" not-null="true" length="10"/>
    <property name="NativeName" column="NativeName" type="System.String" not-null="true" length="30"/>
    <bag name="GlobalStringlocalizeds" table="GlobalStringlocalized" inverse="true" lazy="true" cascade="delete">
    <key column="LanguageID" />
    <one-to-many class="GlobalStringlocalized"/>
    </bag>
</class>
</hibernate-mapping>



Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 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.