-->
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.  [ 2 posts ] 
Author Message
 Post subject: Custom SQL for loading objects
PostPosted: Wed Aug 17, 2005 10:07 pm 
Newbie

Joined: Fri Mar 25, 2005 3:07 pm
Posts: 11
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.1 beta 1

Mapping documents:
<class name="com.tds.refdata.admin.Preference" table="T_PREFERENCE">
<id name="prefId" type="integer" column="id_pref_key" />
<version name="prefVersion" type="integer" column="id_pref_ver" />
<property name="active" type="boolean" column="is_actv" />
<property name="dsRegion" type="integer" column="id_ds_rgn" />
<property name="lastModifiedBy" type="string" column="id_lst_upd" />
<property name="strLastModifiedDate" type="string" column="dt_lst_upd_gmt" />
<property name="prefType" type="string" column="id_pref_typ" />
<property name="prefTypeId" type="integer" column="id_pref_typ_key" />
<property name="pathId" type="integer" column="id_path_key" />
<property name="prefName" type="string" column="nm_pref_key" />
<property name="prefValue" type="string" column="nm_pref_val" />
<property name="prefOrder" type="integer" column="nm_pref_ord" />
<property name="prefDescription" type="string" column="nm_pref_desc" />


<!--loader query-ref="LoadPref" /-->

<sql-insert callable="true">{call INS_UPD_pref_hbm (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)}</sql-insert>
<sql-update callable="true">{? = call INS_UPD_pref_hbm (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)}</sql-update>

<!--sql-delete callable="true">{? = call DEL_pref_hbm (?)}</sql-delete-->


</class>

<sql-query name="LoadUserPref" callable="true">
<return alias="usrPref" class="com.tds.refdata.admin.Preference">
<return-property name="prefId" column="id_pref_key" />
<return-property name="prefType" column="id_pref_typ" />
<return-property name="pathId" column="id_path_key" />
<return-property name="prefName" column="nm_pref_key" />
<return-property name="prefValue" column="nm_pref_val" />
<return-property name="prefVersion" column="id_pref_ver" />
</return>

{ call SEL_user_pref_hbm(?, ?) }
</sql-query>

Code between sessionFactory.openSession() and session.close():
try
{
Session session = HibernateUtils.openSession();
Transaction tx = session.beginTransaction();

Query q = session.getNamedQuery("LoadUserPref");
q.setInteger(0, userId);
q.setBoolean(1, true);
userPrefList = q.list();

tx.commit();
HibernateUtils.closeSession();
}
catch (Exception e)
{
log.error(e,e);
}

Full stack trace of any exception that occurs:

Name and version of the database you are using:
SQL Server 2000
The generated SQL (show_sql=true):

Debug level Hibernate log excerpt:


Not sure if this is a bug or if I am using hibernate incorrectly.

Basically, I have a class and its asscoaited table as shown in the mapping section above.
I want to use an SP to return only certain columns and populate the same class. However, I get errors saying it cannot find the fields that have been defined in the class-table mapping.

I thought that was the idea of the <return-property name="..." column="...."> attribute in the <sql-query>, ie it will call the setters of the class based on the returned column and not based on the table-class definition.

Also - if I wanted to populate an object for which there is no underlying table, I presume I have to execute the query and iterate through the result myself and call the appropriate setters?

Thanks in advance.

Naz


Top
 Profile  
 
 Post subject:
PostPosted: Wed Aug 17, 2005 10:46 pm 
Beginner
Beginner

Joined: Tue Aug 16, 2005 11:06 pm
Posts: 46
Quote:
if I wanted to populate an object for which there is no underlying table, I presume I have to execute the query and iterate through the result myself and call the appropriate setters?


You might be able to use queries to return Java object, some thing like:

select new MyClass(arg1, arg2)
from Arg1 as arg1
join arg1.arg2 as arg2

[/quote]

_________________
Jason Li
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.  [ 2 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.