-->
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.  [ 6 posts ] 
Author Message
 Post subject: HQL and RTTI
PostPosted: Fri Sep 17, 2004 9:36 am 
Newbie

Joined: Thu Aug 05, 2004 1:54 am
Posts: 10
Location: Novosibirsk, Russia
Hibernate version:
2.1.6 ( MySQL DB )
Mapping documents:
Code:

    <class name="com.fis.om.Task" discriminator-value="20">
        <id column="id" name="id">
            <generator class="native" />
        </id>

        <discriminator column="discriminator" type="byte" />

        <property name="completionDate">
            <column name="completionDate" sql-type="DATETIME" />
        </property>
        <property name="approvalDate">
            <column name="approvalDate" sql-type="DATETIME" />
        </property>
        <property name="name">
            <column name="name" sql-type="VARCHAR( 100 ) COLLATE utf8_general_ci" />
        </property>
        <property name="description">
            <column name="description" sql-type="TEXT COLLATE utf8_general_ci" />
        </property>
        <property name="type" type="com.fis.om.TaskType" />

        <many-to-one name="company" class="com.fis.om.Company" />
        <many-to-one name="originator" class="com.fis.om.Employee" />
        <many-to-one name="owner" class="com.fis.om.Employee" />

        <set name="history"
             lazy="true"
             order-by="creationDate desc"
             cascade="delete"
        >
            <key column="task" />
            <one-to-many class="com.fis.om.TaskHistory" />
        </set>

        <subclass name="com.fis.om.PointTask" discriminator-value="21">
            <property name="assignedDate">
                <column name="assignedDate" sql-type="DATE" />
            </property>
            <property name="assignedTime">
                <column name="assignedTime" sql-type="TIME" />
            </property>
        </subclass>

        <subclass name="com.fis.om.SliceTask" discriminator-value="22">
            <many-to-one name="slice" class="com.fis.om.TimeSlice" />
        </subclass>

    </class>


How can I do something like instanceof and dynamic casts in HQL? I need a query like

Code:
            "FROM Task AS t" +
            " WHERE" +
                " IF(" +
                    " t instanceof SliceTask," +
                    " ( ( SliceTask )t ).slice.date >= :start," +
                    " ( ( PointTask )t ).assignedDate >= :start" +
                " )" +
            " ORDER BY" +
                " t.completionDate IS NULL ASC"


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 17, 2004 10:41 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
No - HQL is Hibernates SQL with object focus and extensions.
You can build the query string using Java.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 18, 2004 12:52 am 
Newbie

Joined: Thu Aug 05, 2004 1:54 am
Posts: 10
Location: Novosibirsk, Russia
I don't want to build query in some special way. I want the query to return me instances of subclasses which are ordered by field that is subclass-dependent. If I have the instance of SliceTask I want the order column contain task.slice.date field and if it's PointTask, I want it to contain task.assignedDate field.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Sep 19, 2004 8:35 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
But you are - its just a matter of where you place your conditional.

You want to do it in HQL you will have to place the logic in a form that builds the query string appropriately. Otherwise, move it into your database's stored procedure and call it using JDBC from the connection collected from the session.


Top
 Profile  
 
 Post subject: Solved
PostPosted: Sun Sep 19, 2004 10:54 pm 
Newbie

Joined: Thu Aug 05, 2004 1:54 am
Posts: 10
Location: Novosibirsk, Russia
The problem is solved. I just wrote SQL for tasks retrieval, where I

1) Left join Slice to Task

2) Check for discriminator value

3) Put proper column in ORDER BY clause according to discriminator.

And then createSqlQuery() and it works.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 20, 2004 7:46 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Cool.


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