-->
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.  [ 3 posts ] 
Author Message
 Post subject: SQLQuery and optional properties
PostPosted: Wed Aug 17, 2005 4:04 pm 
Newbie

Joined: Tue Dec 16, 2003 8:34 am
Posts: 15
Hibernate 3.0

I am using a single POJO, and single mapping file, with a SQLQuery to return results from many different raw SQL queries. The raw SQL queries use projection to map virtual result columns into entity properties. This technique is working pretty well except that not all queries should return all of the properties. Some queries only specify result columns for some of the properties defined on my entity, while some of the other queries might want to populate a different subset of the properties. Using more than one mapping file or more than one POJO would damage the integrity of the model, because conceptually, all queries are returning instances of this one entity. Concretely:

mapping file:


<class
name="example.Position"
>

<id
name="id"
type="example.UUIDUserType"
/>

<property
name="cusip"
type="string"
/>

<property
name="price"
type="double"
/>

<property
name="marketValue"
type="double"
/>



query1= "
SELECT 1 as {position.id}, cusip as {position.cusip}, price as {position.price}, marketValue as {position.marketValue}
.... oodles of SQL spam..."

SQLQuery query = session.createSQLQuery(query1);

query.addEntity("position", Position.class);
List loadObjects = query.list();

works fine.

But now query2 projects over a completely different set of tables and is not able to produce a column for "price".

query2= "
SELECT 1 as {position.id}, cusip as {position.cusip}, marketValue as {position.marketValue}
.... oodles of SQL spam..."

In this case, Hibernate still tries to load a value for the price column (because, I guess, it's mapped) and fails.

Is there some way to tell Hibernate that retrieving values for some properties is optional? It seems like the fundamental problem is that Hibernate insists there must be a ResultSet column for each mapped property, but what I really want Hibernate to do is to retrieve values for those column which are present and to ignore the others. Avoiding the complexities of dynamically mapping would be wonderful.

thanks for any pointers.

Joe


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 17, 2005 4:12 pm 
Senior
Senior

Joined: Wed Jul 13, 2005 4:31 pm
Posts: 142
Location: Seattle, WA
Different entity-names but, same class name might work for you -
Here is an example of what we used.

Code:
<class name="TermImpl" entity-name="Term">
   <id name="id"/>
   <property name="name"/>
   <property name="notes"/>
   <property name="nextone"/>
   <property name="andmore"/>
</class>

<class name="TermImpl" entity-name="TermSummary">
   <id name="id"/>
   <property name="name"/>
</class>

<sql-query name="termQuery">
   <return class="TermSummary"/>
   SELECT term.name AS name, term.termid AS id
   FROM term term
</sql-query>


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 17, 2005 4:56 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
here you should you probably use polymorphism=explicit to avoid getting the same entity loaded in multiple representations.

p.s. i don't think this is a good usage of entity-name...

_________________
Max
Don't forget to rate


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