|
From: JP P. <jp....@ti...> - 2003-08-11 18:03:22
|
-----Message d'origine----- De=A0: JP Pawlak [mailto:jp....@ti...]=20 Envoy=E9=A0: lundi 11 ao=FBt 2003 19:51 =C0=A0: 'SourceForge.net' Objet=A0: RE : [springframework - Help] Spring JDBC object usage - multiple queries Hi Jim, This is a common situation. Rod was using a technique approaching this done below. But in fact no of the two application demos uses it. In such case, we generally create a first abstract class subclassing the mapRow method which contains the common code and an incomplete constructor. Something like this: abstract class MyObjectSqlQuery extends MappingSqlQuery{ =20 protected MyObjectSqlQuery (DataSource ds, String sql) { super(ds, sql); // No compile in this class } =20 protected Object mapRow(ResultSet rs, int rownum) throws SQLException { MyObject myObject =3D ... // Here creating the object from the resultset // Assuming all the SQL orders calling for the needed fields. return myObject; } } And after all concrete Query objects will subclass it. class AllMyObjectSqlQuery extends MyObjectSqlQuery { protected AllMyObjectSqlQuery (DataSource ds) { super(ds, SQL_SELECT_ALL); compile(); } } class SomeMyObjectSqlQuery extends MyObjectSqlQuery { protected SomeMyObjectSqlQuery (DataSource ds) { super(ds, SQL_SELECT_SOME); // Related to the WHERE statement declareParameter(new SqlParameter(Types.INTEGER)); declareParameter(new SqlParameter(Types.CHAR)); declareParameter(new SqlParameter(Types.CHAR)); ... compile(); } } For these two classes you generally will use one of the execute() methods or add a method using them internally. For a single retrieval : class FindMyObjectSqlQuery extends MyObjectSqlQuery { protected FindMyObjectSqlQuery (DataSource ds) { super(ds, SQL_SELECT_ONE); // Generally just the id as integer or long like here declareParameter(new SqlParameter(Types.BIGINT)); compile(); } =09 public MyObject getMyObject(long id) { return (MyObject) this.findObject(id); } } Nevertheless, you must have a specific class for each SQL Select with a different parameters signature with this design. Otherwise, you will make tricky SQL select for optional elements in the WHERE Clause [(name =3D ? or isnull(?))]. Hope this answer to your question, Regards, Jean-Pierre -----Message d'origine----- De=A0: SourceForge.net [mailto:no...@so...]=20 Envoy=E9=A0: lundi 11 ao=FBt 2003 17:50 =C0=A0: no...@so... Objet=A0: [springframework - Help] Spring JDBC object usage - multiple queries Read and respond to this message at:=20 https://sourceforge.net/forum/message.php?msg_id=3D2145145 By: jnemesh Hi! I'm exploring the Spring framework a bit, and I have a question: I typically do 2 modes of queries for selects: Find a single object Range So, for example, the API might allow for someone to find a single datatype, or a list of all available datatypes. I'd like to bundle those 2 calls under the same object, so I can avoid repetition of code (for mapping the results.) What I'd like to do is be able to register multiple sql querries for the object, and call the correct one via methods with strong typing that internally call execute(<params>). One of the problems I'm running into with this is that different queries on the same object wind up having different parameters, and I can't undeclare params before I compile. Because of this, if methods are run in the wrong order it causes problems. If I were to subclass one of the classes so that the mapping code was reused, wouldn't I be exposing the other class's methods and causing this same problem? What's a good way to handle this? Or should I just implement a new class for each query, and re-use the mapping code? I guess I could implement a super class that just has the mapping code, and each subclass could extend it - but that leaves me with even more classes to deal with. And the API for a thousand classes with 1 method each is difficult to read. Any advice greatly appreciated. -Jim ______________________________________________________________________ You are receiving this email because you elected to monitor this forum. To stop monitoring this forum, login to SourceForge.net and visit:=20 https://sourceforge.net/forum/unmonitor.php?forum_id=3D250340 |