-->
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.  [ 12 posts ] 
Author Message
 Post subject: populating dropdowns using hibernate
PostPosted: Thu Nov 11, 2004 5:06 pm 
Newbie

Joined: Thu Nov 11, 2004 3:24 pm
Posts: 17
Hi,
I am writing a class which is supposed to cache the data from all the master tables in my application in the form of an arraylist of value objects so that i can use this to populate the drop down in jsp using struts tag.
I am using hibernate to query the database and return me a arraylist.
But the problem is that i am required to create a seperate VO for each master table who data i want to cache.

eg CurrecyVO refers mst_currency which has code and desc.

All my master tables have just the code and desc. I want to know a way by which i can use the same value object for all the tables. This value object will store the code and desc by getting it from appropriate table.
I want to avoid the redundancy of create similar value objects for different master tables. Is it possible to do this with hibernate , please comment.

Awaiting ur response.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 11, 2004 5:14 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
select new ValueObject(o.code, o.desc) from MyObject o


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 11, 2004 5:25 pm 
Newbie

Joined: Thu Nov 11, 2004 3:24 pm
Posts: 17
sorry, I didnt understand your reply properly, can your please clarify

This is my ".hbm.xml" file

<hibernate-mapping>
<class name="com.statestr.fxmm.pbg.domain.RefCalcMethodVO" table="REF_CALC_METHOD">
<id name="code" column="CALC_METHOD_CD">
<generator class="hilo"/>
</id>
<property name="desc" column="CALC_METHOD_DESC"/>
</class>

</hibernate-mapping>

and my query is

Query query = session.createQuery("select calc from RefCalcMethodVO as calc");

Now as per your reply

"select new ValueObject(o.code, o.desc) from MyObject o"

what will be ValueObject and MyObject in my case.

How will i point my hbm.xml to different tables.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Nov 11, 2004 6:14 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
Use a ValuePair for the model when generating/populating the dropdown.
Michael is saying, convert this:
Quote:
Query query = session.createQuery("select calc from RefCalcMethodVO as calc");


to

Code:
Query query = session.createQuery("select new ValueObject(calc.code, calc.desc) from RefCalcMethodVO as calc");


This will create a list of ValueObjects populated with the value/key pair. This can be safely cached by your application. All you need to do is create the DTO object ValueObjects, eg, No changes to the mapping(s).


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 22, 2004 7:11 pm 
Newbie

Joined: Thu Nov 11, 2004 3:24 pm
Posts: 17
i tried the code above but gives me a error

net.sf.hibernate.QueryException: class not found: ListVO

[SELECT new ListVO(calc.calcMethodCd, calc.calcMethodDesc) FROM com.statestr.fxmm.pbg.domain.RefCalcMethod AS calc]

where ListVO is a normal class having code and desc as it members with getter and setter. Also it has a constructor accepting two object parameters.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 22, 2004 7:14 pm 
Newbie

Joined: Thu Nov 11, 2004 3:24 pm
Posts: 17
it is not able to find ListVO probably cause i have not mentioned it anywhere. Where do i specify it?

How do i get around this problem. ?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 22, 2004 7:21 pm 
Newbie

Joined: Thu Nov 11, 2004 3:24 pm
Posts: 17
It expects me to give the full path of the ListVO

SELECT new com.statestr.fxmm.pbg.domain.ListVO(calc.calcMethodCd, calc.calcMethodDesc FROM RefCalcMethod AS calc

is there a way to avoid giving the path.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 22, 2004 7:22 pm 
Regular
Regular

Joined: Tue Jan 27, 2004 12:22 pm
Posts: 103
Map ListVO in your mapping file. It's not necessary to map it to a table.

_________________
Dencel
- The sun has never seen a shadow -


Top
 Profile  
 
 Post subject:
PostPosted: Mon Nov 22, 2004 7:31 pm 
Newbie

Joined: Thu Nov 11, 2004 3:24 pm
Posts: 17
Do i have to create a ListVO.hbm.xml for this Value object (ListVO)

I have added a line in my hibernate.cfg.xml fiel

<mapping resource="com/statestr/fxmm/pbg/domain/ListVO.hbm.xml" />

and in the listVO.hbm.xml file i have defined
------------------------------------------------------------
<hibernate-mapping>

<class name="com.statestr.fxmm.pbg.domain.ListVO"/>
</hibernate-mapping>
------------------------------------------------------------

But it gives me error

Caused by: org.xml.sax.SAXParseException: The content of element type "class" is incomplete, it must match "(meta*,(cache|jcs-cache)?,(id|composite-id),discriminator?,(version|timestamp)?,(property|many-to-one|one-to-one|component|dynamic-component|any|map|set|list|bag|idbag|array|primitive-array)*,(subclass*|joined-subclass*))".


so i guess something else is required , which is mandatory and i am not providing it. please help


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 23, 2004 3:53 am 
Regular
Regular

Joined: Tue Jan 27, 2004 12:22 pm
Posts: 103
If you look at the DTD of the xml file you will see that the <id> tag is required.

Just put in :
Code:
      <id>
         <generator class="assigned"/>
      </id>

Or :
Code:
      <id>
         <meta attribute="gen-property">false</meta>
         <generator class="assigned"/>
      </id>

When using Hibernate Tools.

_________________
Dencel
- The sun has never seen a shadow -


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 23, 2004 11:23 am 
Newbie

Joined: Thu Nov 11, 2004 3:24 pm
Posts: 17
Thanks a lot


Top
 Profile  
 
 Post subject:
PostPosted: Tue Nov 23, 2004 4:42 pm 
Newbie

Joined: Wed Sep 22, 2004 12:18 pm
Posts: 14
or you could have just done something like this:


Code:
Query query = session.createQuery("select new " + ValueObject.class.getName() + "(calc.code, calc.desc) from RefCalcMethodVO as calc");


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