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.  [ 5 posts ] 
Author Message
 Post subject: hql question
PostPosted: Sat Aug 05, 2006 5:50 pm 
Regular
Regular

Joined: Tue Feb 07, 2006 4:27 pm
Posts: 69
I have the following object associations.

Project
ProjectClass (bi-directional many-to-many to Project)
WellProjectClass (subclass of ProjectClass)
GeologicalPrognosis (bidirectional association to WellProjectClass)

I want to develop an hql query that will return me the
GeologicalPrognosis instance based on the id of the Project class.

I have tried a few times, but I'm not sure how I can reference the subclass (WellProjectClass) through a join of the Project object.

Some examples here would be appreciated. Thanks..

Mapping files to follow:

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0"
assembly="WellAccelerator.Domain" namespace="WellAccelerator.Domain"
>

<class name="Project" table="PROJECT">

<id name="ProjectId" column="PROJECT_ID" type="Int64" unsaved-value="0">
<generator class="native"/>
</id>
<property name="ProjectName" column="PROJECT_NAME"/>

<many-to-one name="ProjectType" column="PROJECT_TYPE_ID" class="ProjectType" />

<bag name="ProjectClasses" table="PROJECT_CLASS_XREF" cascade="all" outer-join="true">
<key column="PROJECT_ID" />
<many-to-many column="PROJECT_CLASS_ID"
class="WellAccelerator.Domain.ProjectClass, WellAccelerator.Domain" />
</bag>

<many-to-one name="Strike" column="Strike_Id" class="Strike"/>
<many-to-one name="Jurisdiction" column="Jurisdiction_Id"
class="Jurisdiction"/>

</class>

</hibernate-mapping>

<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0"
assembly="WellAccelerator.Domain" namespace="WellAccelerator.Domain"
>

<class name="ProjectClass" table="PROJECT_CLASS" discriminator-value="PC">

<id name="ProjectClassId" column="PROJECT_CLASS_ID" type="Int64" unsaved-value="0">
<generator class="native"/>
</id>

<discriminator column="CLASS_CODE" type="String" length="4"/>

<bag name="Projects" table="PROJECT_CLASS_XREF" inverse="true" cascade="all" outer-join="true">
<key column="PROJECT_ID" />
<many-to-many column="PROJECT_CLASS_ID"
class="WellAccelerator.Domain.Project, WellAccelerator.Domain" />
</bag>

<bag name="Locations" table="PROJECT_CLASS_LOCATION" cascade="all" outer-join="true">
<key column="LOCATION_ID" />
<many-to-many column="PROJECT_CLASS_ID"
class="WellAccelerator.Domain.Location, WellAccelerator.Domain" />
</bag>

<property column="PROJECT_CLASS_TYPE"
type="WellAccelerator.Domain.ProjectClassCode, WellAccelerator.Domain"
name="ProjectClassCode" not-null="true"
/>

<many-to-one name="ProjectAfe" class="ProjectAfe" column="ProjectAfeId"/>
<property name="WorkingInterest" column="WorkingInterest"/>
<many-to-one name="Product" column="ProductId" class="Product"/>
<many-to-one name="OperatingTeam" column="OperatingTeamId" class="OperatingTeam"/>

</class>
</hibernate-mapping>


<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0"
assembly="WellAccelerator.Domain" namespace="WellAccelerator.Domain"
>

<subclass name="WellProjectClass" extends="ProjectClass" discriminator-value="WELL">
<one-to-one class="GeologicalPrognosis" name="GeologicalPrognosis" cascade="all"/>
</subclass>
</hibernate-mapping>


<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0"
assembly="WellAccelerator.Domain" namespace="WellAccelerator.Domain"
>

<class name="GeologicalPrognosis" table="PROGNOSIS" discriminator-value="0">

<id name="Id" column="PROGNOSIS_ID" type="Int64">
<generator class="foreign">
<param name="property">WellProjectClass</param>
</generator>
</id>

<one-to-one class="WellProjectClass" name="WellProjectClass" constrained="true"/>
<many-to-one class="PrognosisStatus" name="PrognosisStatus" />
<property column="Ground_Elevation" name="GroundElevation" not-null="false"/>
<property column="Kb_Elevation" name="KbElevation" not-null="false"/>
<property column="Metres_Penetrated" name="MetresPenetrated" not-null="false"/>
<property column="sample_requirements" name="SampleRequirements" not-null="false"/>
<property column="tight_hole" name="TightHole" not-null="false"/>
<property column="off_target" name="OffTarget" />
<property column="total_measured_depth" name="TotalMeasuredDepth"/>
<many-to-one class="ConfidentialStatus" name="ConfidentialStatus"/>
<property column="dpt_exploratory_depth" name="DptExploratoryDepth"/>
<property column="comments" name="PrognosisComments"/>

</class>
</hibernate-mapping>[/b]


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 06, 2006 8:18 am 
Senior
Senior

Joined: Wed Jun 15, 2005 4:17 am
Posts: 156
try something like this:
Code:
from GeologicalPrognosis gp
inner join gp.WellProjectClass as wpc
inner join wpc.Project as proj
where proj.ProjectId=:id

HTH,
radu


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 07, 2006 10:38 am 
Regular
Regular

Joined: Tue Feb 07, 2006 4:27 pm
Posts: 69
radu wrote:
try something like this:
Code:
from GeologicalPrognosis gp
inner join gp.WellProjectClass as wpc
inner join wpc.Project as proj
where proj.ProjectId=:id

HTH,
radu


Thanks Radu,

I did try this:

Code:
   "FROM GeologicalPrognosis gp
            INNER JOIN gp.WellProjectClass as wpc 
            INNER JOIN wpc.Projects as proj 
WHERE proj.ProjectId=" + id;


Thanks for the example.

I think the only difference is that I concatenated the value of the id instead of using a paramater as your example. Not sure if that would make a difference. I haven't seen an example of passing in a parameter such as you have done. I'll check the documentation.

My query returns no results. It should have returned a single instance of the GeologicalPrognosis class.

I think I missed sending one of my mapping files. This is the parent to WellProjectClass. Not sure if it would make a difference to how the hql is written.:

Code:
<?xml version="1.0" encoding="utf-8" ?>
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.0"
                   assembly="MyDomain" namespace="MyDomain"
                   >
 
   <class name="ProjectClass" table="PROJECT_CLASS" discriminator-value="PC">

      <id name="ProjectClassId" column="PROJECT_CLASS_ID" type="Int64" unsaved-value="0">
         <generator class="native"/>
      </id>
   
    <discriminator column="CLASS_CODE" type="String" length="4"/>

    <bag name="Projects" table="PROJECT_CLASS_XREF"  inverse="true" cascade="all" outer-join="true">
      <key column="PROJECT_ID" />
      <many-to-many column="PROJECT_CLASS_ID"
                    class="MyDomain.Project, MyDomain"  />
    </bag>   
       
    <bag name="Locations" table="PROJECT_CLASS_LOCATION" cascade="all" outer-join="true">
      <key column="LOCATION_ID" />
      <many-to-many column="PROJECT_CLASS_ID"
                    class="MyDomain.Location, MyDomain"  />
    </bag>
   
      <property column="PROJECT_CLASS_TYPE"
            type="MyDomain.ProjectClassCode, MyDomain"
      name="ProjectClassCode" not-null="true"/>

    <many-to-one name="ProjectAfe" class="ProjectAfe" column="ProjectAfeId"/>
    <property name="WorkingInterest" column="WorkingInterest"/>
    <many-to-one name="Product" column="ProductId" class="Product"/>
    <many-to-one name="OperatingTeam" column="OperatingTeamId" class="OperatingTeam"/>
   
   </class>
</hibernate-mapping>


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 07, 2006 10:49 am 
Senior
Senior

Joined: Wed Jun 15, 2005 4:17 am
Posts: 156
you should check what happens by doing tests with the generated SQL.

To use params:

IQuery query=session.CreateQuery(....
query.SetInt32("id",projectId);

the first param is the name of the param used in the query, the second one is the value assigned to it.

cheers,
Radu


Top
 Profile  
 
 Post subject:
PostPosted: Mon Aug 07, 2006 1:54 pm 
Regular
Regular

Joined: Tue Feb 07, 2006 4:27 pm
Posts: 69
radu wrote:
you should check what happens by doing tests with the generated SQL.

To use params:

IQuery query=session.CreateQuery(....
query.SetInt32("id",projectId);

the first param is the name of the param used in the query, the second one is the value assigned to it.

cheers,
Radu


Thanks for the tips. I'll use that approach in the future while I'm learning NHibernate. I did discover an error in my mapping files which accounted for not having returned the expected row. I have fixed it and it is now working properly.

Thanks for the helpful tips.


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