db-torque-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From luca.fo...@sorma.com
Subject Re: Dynamically Accessing Torque Objects
Date Tue, 29 Jul 2003 07:51:30 GMT

Using Java Reflection:
               BasePeer tbl = (BasePeer) Class.forName(tabelName+"Peer"
).newInstance();
               String currentDB    =     (String) Class.forName(tabelName+
"Peer").getField("DATABASE_NAME").get(tbl);
               String currentTable =     (String) Class.forName(tabelName+
"Peer").getField("TABLE_NAME").get(tbl);

               Criteria criteria = new Criteria();
               criteria.setDbName(currentDB);
               // .. criteria.add(..) ...

               Class[] o = new Class[1];
               o[0] = criteria.getClass();
               Method doSelect = Class.forName(tabelName+"Peer").getMethod(
"doSelect",o);
               Object[] obs = new Object[1];
               obs[0] = (Object) criteria;
               ArrayList results = (ArrayList) doSelect.invoke(tbl, obs);

Hope this help you!
Bye

Ing. Luca Forni

SORMA S.p.a.
Corso Vinzaglio, 4 - 10121
TORINO (ITALY)
Tel. +39 011 53 33 22
http://www.sorma.com


                                                                                         
                                   
                    "Cameron                                                             
                                   
                    Hickey"              To:     "'Apache Torque Users List'" <torque-user@db.apache.org>
                   
                    <cam@panoramat       cc:                                          
                                      
                    os.com>              Subject:     Dynamically Accessing Torque Objects
                                  
                                                                                         
                                   
                    29/07/2003                                                           
                                   
                    09.22                                                                
                                   
                    Please respond                                                       
                                   
                    to "Apache                                                           
                                   
                    Torque Users                                                         
                                   
                    List"                                                                
                                   
                                                                                         
                                   
                                                                                         
                                   




I realize that this may seem both strange, and naïve, but I want to be
able to choose which Torque Data object to access by passing in a String
value.  Has anyone else attempted something similar to this before?
What would be a logical syntax for it?

This obviously shows my lack of fluency with Java, but here is what I am
trying:

           String theObjStr = (String)request.getParameter("ObjType");
           String objectPeerStr = "RentalApp.torque.om." + theObjStr +
"Peer";
           Class c = Class.forName(objectPeerStr);
           BasePeer objectRef = (BasePeer)c.newInstance();

           Criteria crit = new Criteria();

           List v = objectRef.doSelect(crit);

This does not actually work properly, although it does manage to load
the proper class, because I don't know what class it is ahead of time, I
cast the class to to BasePeer, but the BasePeer object cannot manage the
connection to the database properly, because it does not know which
table it should be connected to.

Is there some other way of doing this?  This is a java question, but: Is
there a way to cast my newly created object to the appropriate class
without knowing what it is ahead of time?

Sadly, because this seems a little futile, I may end up going back to
regurlar old SQL in my servlets, which really seems like too many steps
backward, unless I can get something like this working.

Thanks for any help

Cameron


---------------------------------------------------------------------
To unsubscribe, e-mail: torque-user-unsubscribe@db.apache.org
For additional commands, e-mail: torque-user-help@db.apache.org






---------------------------------------------------------------------
To unsubscribe, e-mail: torque-user-unsubscribe@db.apache.org
For additional commands, e-mail: torque-user-help@db.apache.org


Mime
View raw message