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: Polymorphic joins in HQL?
PostPosted: Fri Aug 19, 2005 3:48 pm 
Newbie

Joined: Fri Aug 19, 2005 3:31 pm
Posts: 3
I'm trying to create a simple project/task management application. A project contains tasks, which can be either assigned to a user (ProjectMember), or stored in a tray (TaskTray), it it's not assigned to any user.

I have a syntax question for the hibernate query language:

Here's what the classes would look like

class Task
{
long id;
TaskOwner assignedTo;
}

abstract class TaskOwner
{
long id;
}

class ProjectMember extends TaskOwner
{
User user;
}

class TaskTray extends TaskOwner
{
String trayName;
}

class User
{
long id;
String firstName;
String lastName;
}

So a task is assigned to a taskOwner, which is either a ProjectMember, or a TaskTray.

Now, I want to create a hibernate query to retrieve all the tasks for a particular user. The closest I've been able to come up with would be the following, but it's invalid syntax:

Query query = getSession().createQuery(
"from Task as task " +
"inner join ProjectMember as member " +
"where task.assignedTo.id = member.id " +
"and member.user = :user ");

Does anyone know how to express this query properly? I can't do "where task.assignedTo.user = :user", because I'd have to cast assignedTo to ProjectMember first, and I don't know how to do that.

Thanks.... Patrick Arnesen


Top
 Profile  
 
 Post subject: Re: Polymorphic joins in HQL?
PostPosted: Fri Aug 19, 2005 4:06 pm 
Expert
Expert

Joined: Mon Feb 14, 2005 12:32 pm
Posts: 609
Location: Atlanta, GA - USA
Patrick Arnesen wrote:
I'm trying to create a simple project/task management application. A project contains tasks, which can be either assigned to a user (ProjectMember), or stored in a tray (TaskTray), it it's not assigned to any user.

I have a syntax question for the hibernate query language:

Here's what the classes would look like

class Task
{
long id;
TaskOwner assignedTo;
}

abstract class TaskOwner
{
long id;
}

class ProjectMember extends TaskOwner
{
User user;
}

class TaskTray extends TaskOwner
{
String trayName;
}

class User
{
long id;
String firstName;
String lastName;
}

So a task is assigned to a taskOwner, which is either a ProjectMember, or a TaskTray.

Now, I want to create a hibernate query to retrieve all the tasks for a particular user. The closest I've been able to come up with would be the following, but it's invalid syntax:

Query query = getSession().createQuery(
"from Task as task " +
"inner join ProjectMember as member " +
"where task.assignedTo.id = member.id " +
"and member.user = :user ");

Does anyone know how to express this query properly? I can't do "where task.assignedTo.user = :user", because I'd have to cast assignedTo to ProjectMember first, and I don't know how to do that.

Thanks.... Patrick Arnesen


when you do joins in HQL, you're specifying Class attributes so it would be
.... inner join task.assignedTo as assignedTo


I might be able to give you more info if you post your mapping files.

_________________
Preston

Please don't forget to give credit if/when you get helpful information.


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 27, 2005 3:17 am 
Newbie

Joined: Fri Aug 19, 2005 3:31 pm
Posts: 3
The problem is that the Task.assignedTo property is of type TaskOwner, but that's the abstract subclass, which does not expose the property I want. I only want the tasks where Task.assignedTo joins to a TaskOwner of type ProjectMember, and once the join is complete, I want to access a property of the subclass.

(I got around this by collapsing the superclass and 2 subclasses into a single class, but it's a pretty ugly solution)


Top
 Profile  
 
 Post subject:
PostPosted: Sat Aug 27, 2005 3:18 am 
Newbie

Joined: Fri Aug 19, 2005 3:31 pm
Posts: 3
Sorry, my previous post should read:

he problem is that the Task.assignedTo property is of type TaskOwner, but that's the abstract SUPERCLASS


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 28, 2005 2:26 pm 
Newbie

Joined: Sun Aug 28, 2005 2:13 pm
Posts: 9
Try
Code:
Query query = getSession().createQuery(
"select task from ProjectMember as pm, Task as task inner join task.assignedTo as owner where owner = pm and pm.user = ?)
.setEntity(0, user);


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.