Using Java Reflection saves a lot of coding time and makes it easy for you
to scale more beans, as long as you write good reflection code to begin
with, since debugging is a pain!

I use it in my architecture;  we have about 10 entity beans that represent
sql link tables: they only 2 columns, both foreign keys to other
tables/entity beans.  I created an "Associator" session bean that uses pure
reflection to accomplish its task:  Given an association funtion name, say
"X2Y", and 2 longs, this bean finds the correct EJBHome (X2YHome), checks if
row already exists (findByXAndY), generates the appropriate PKs (XKey and
YKey), and calls the appropriate create method (X2YHome.create(XKey, YKey).
The same goes for dissociating, listing, etc..

Gene Chuang
Kiko.com


-----Original Message-----
From: A mailing list for Enterprise JavaBeans development
[mailto:[EMAIL PROTECTED]]On Behalf Of Heiko Gottschling
Sent: Tuesday, January 09, 2001 5:27 AM
To: [EMAIL PROTECTED]
Subject: Please comment: Using java.lang.reflect to simplify database
access


Hi,

I have noticed that in my application I have lots of similarly structured
methods to read records from a database. For example, I have a session bean
called ProjectManager, which has methods like these:

  ProjectValue getProject(int id)
  TaskGroup getTaskGroup(int id)
  ProjectClass getProjectClass(int id)

and so on. The implementations of these methods are very similar, they all
issue a SELECT statement and fill the value objects (which are not much more
than struct-like entities, with public fields corresponding to all columns
of
the associated database table) with the obtained results, like

  resultSet = statement.executeQuery("SELECT name, creationDate [...] FROM
  Project WHERE id=" + id);

  projectValue.name = resultSet.getString("name");
  projectValue.creationDate = resultSet.getDate("creationDate");

Now, here is what I thought of, and I'd like to hear your comments on this:

I want to create a SessionBean, let's call it DatabaseUtil, with a method
like:

  Object getRecord(String tableName, int id)

this method would use the java reflection package to look up a class named
<tableName>Value, retrieve the names of all of its public fields, construct
a
SELECT statement from these field names, and construct a new
<tableName>Value
object from the returned result set.

This approach would save a lot of redundant coding. Can you think of any
potential drawbacks? What about performance overhead when using reflection?
(I gather J2EE uses it all the time anyway)?

thx
Heiko

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff EJB-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff EJB-INTEREST".  For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".

Reply via email to