-->
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.  [ 4 posts ] 
Author Message
 Post subject: Can I join against an unmapped table in HQL?
PostPosted: Tue Jun 10, 2014 10:07 pm 
Newbie

Joined: Sun Nov 23, 2008 4:09 pm
Posts: 3
Is it possible to write an HQL query (or barring that, a Criteria query) that joins hibernate-mapped classes against a table not mapped by hibernate?

My use case is, for analysis and reporting purposes, I will pre-compute the primary keys of entities I'm interested in for a particular analysis, and store these in a temporary table that I'd like to join against for many queries

So, can I do the equivalent of
Code:
select patientId from Patient
inner join my_temp_table on patientId = my_temp_table.id
where ...
-- or
select patientId from Patient
where patientId in (select id from my_temp_table)
  and ...


I assume that Restrictions.sqlRestriction will let me do this for a Criteria query, but I'd much prefer to be able to use HQL. Thoughts?


Top
 Profile  
 
 Post subject: Re: Can I join against an unmapped table in HQL?
PostPosted: Wed Jun 11, 2014 4:40 am 
Hibernate Team
Hibernate Team

Joined: Fri Sep 09, 2011 3:18 am
Posts: 295
Hi,
It is not possible to mix SQL and HQL in the same query: http://stackoverflow.com/questions/1538577/mix-hql-and-sql-in-the-same-query

If you don't want to run an SQL query, I think that you can map the temp table as immutable entities: https://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html_single/#readonly-api-immutable
or, maybe, you can use the @Formula annotation to store the value that you need in a property of an entity.

Cheers,
Davide


Top
 Profile  
 
 Post subject: Re: Can I join against an unmapped table in HQL?
PostPosted: Fri Jun 13, 2014 12:22 pm 
Newbie

Joined: Sun Nov 23, 2008 4:09 pm
Posts: 3
Davide,

Thanks for the response.

Would you mind expanding a bit more on how mapping the temp table as an immutable entity would work?

My intention was to create a temp table on the fly at run-time, and drop it after the analysis is complete. Are you suggesting that I could programmatically define a new entity mapping (as an immutable entity) while the system is running?

Thanks,
Darius


Top
 Profile  
 
 Post subject: Re: Can I join against an unmapped table in HQL?
PostPosted: Mon Jun 16, 2014 6:32 am 
Hibernate Team
Hibernate Team

Joined: Fri Sep 09, 2011 3:18 am
Posts: 295
> Are you suggesting that I could programmatically define a new entity mapping (as an immutable entity) while the system is running

I'm not sure about this but hibernate should see a temp table like any other table (as long as you already know in advance the name of the table and the columns it will have), the only difference is that you are only interested to read the values from it .
I think this might work as long as you execute the query when the table exists.

Currently, I don't think you can define on entity on the fly.


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