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: Query an object based on a IList<string>
PostPosted: Fri Nov 23, 2007 5:09 pm 
Beginner
Beginner

Joined: Tue Sep 14, 2004 1:03 pm
Posts: 33
Location: Calgary, Alberta Canada
I have an object that has a IList<string> propery of aliases for the item.
What I want to do is get an object, or list of objects that have the provided alias.

The native sql query would look like:
Code:
select * from ANALYTES a
inner join ANALYTE_ALIASES aa
on aa.ANALYTE_ID = a.ANALYTE_ID
where aa.ALIAS = :the alias


what would the HQL query look like, or is there a criteria query that could be used? Projection on the alias column?

or do I have to use a subquery

Code:
from ANALYTE a where a.ID in (select ANALYTE_ID from ANALYTE_ALIAS where ALIAS = :alias)


There is no ANALYTE_ALIAS class, it's a table, so the previous query is kind of an HQL hybrid.

Mapping documents:
Code:
<class name="AMEC.EE.Sage.Domain.Persistence.Adm.Proxy.Analyte, Sage.Domain.Persistence.Adm"
         table="ANALYTES"
         schema="ADM"
         lazy="false">
    <id name="id"
        column="ANALYTE_ID"
        type="System.Int32"
        access="field"
        unsaved-value="0">
      <generator class="identity" />
    </id>
    <property name="CommonName"
              column="COMMON_NAME"
              type="System.String"
              length="150"
              not-null="true"
              unique="false" />
    <property name="ShortName"
              column="SHORT_NAME"
              type="System.String"
              length="35"
              not-null="false"
              unique="false" />
    <property name="RowGuid"
              column="ROWGUID"
              type="System.Guid"
              not-null="true"
              unique="true" />
    <bag name="Aliases"
         inverse="false"
         cascade="all-delete-orphan"
         lazy="true"
         table="ANALYTE_ALIAS"
         schema="ADM">
      <key column="ANALYTE_ID" />
      <element column="ALIAS"
               type="System.String"
               length="150"
               not-null="true"
               unique="true" />
    </bag>
    <!-- TODO: Map alternate IDs-->
    <map name="AlternateIDs"
         inverse="false"
         cascade="all-delete-orphan"
         lazy="true"
         schema="ADM"
         table="ANALYTE_IDS">
      <key column="ANALYTE_ID"/>
      <index-many-to-many column="ENTITY_ID"
                          class="AMEC.EE.Sage.Domain.Entities.Core.IEntity, Sage.Domain.Entities.Core"/>
      <element column="ALT_ID"
               type="System.String"
               length="50"
               not-null="true"
               unique="false"/>             
    </map>
  </class>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Nov 28, 2007 12:27 pm 
Beginner
Beginner

Joined: Tue Sep 14, 2004 1:03 pm
Posts: 33
Location: Calgary, Alberta Canada
OK, I've found a solution, not really what I was hoping for.. but it works.


Code:
public IAnalyte FindByAlias(string alias) {
            string query =
                "select * from ADM.ANALYTES a where a.ANALYTE_ID in "+
                "(select ANALYTE_ID from ADM.ANALYTE_ALIAS where ALIAS = :alias)";
            IList<IAnalyte> results = HibernateTemplate.ExecuteFind(new FindHibernateDelegate<IAnalyte>(
                                                     delegate(ISession session)
                                                         {
                                                             return session.CreateSQLQuery(query)
                                                                 .AddEntity(typeof(Analyte))
                                                                 .SetParameter("alias", alias, NHibernateUtil.String)
                                                                 .List<IAnalyte>();
                                                         }));
            if (results != null && results.Count == 1)
                return results[0];
            return null;
        }


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.