I want to map a Java bean object to relational database table! But do not want to use EJB.
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Joe_Xiao
Posted On:   Tuesday, May 28, 2002 06:43 PM

There is a java bean named User.java, and it has some attibutes. e.g userName (String), birthday (Date), IdNumber (String). That is to say there are getter and setter methods of these 3 fields in User.java. now, in another java class--- Test.java, I want to retrieve User.java object's attributes and its object values. The object values maybe String and maybe Date type. I can get all methods and fields from User.java. But when I want to get the value of the field, i puzzled. for example, In Test.java, I can find an attribute of User.java named birthday, they are Method[] array, and can to String ---"public java.lang.String User.getBirthday()". now , i want to retrieve the relevant birthday object value which is a java.util.Date object. that is to say i wan   More>>

There is a java bean named User.java, and it has some attibutes. e.g userName (String), birthday (Date), IdNumber (String). That is to say there are getter and setter methods of these 3 fields in User.java. now, in another java class--- Test.java, I want to retrieve User.java object's attributes and its object values. The object values maybe String and maybe Date type. I can get all methods and fields from User.java. But when I want to get the value of the field, i puzzled. for example, In Test.java, I can find an attribute of User.java named birthday, they are Method[] array, and can to String ---"public java.lang.String User.getBirthday()". now , i want to retrieve the relevant birthday object value which is a java.util.Date object. that is to say i want to call the User.getBirthday() method and return a java.util.Date object. I do not want to hard code that, i need a versatile method--- retrieve an attribute name, and auto call the relevant getXXX method and return the object(maybe String, maybe Date in this example).

how can i do that? pls help me!

   <<Less

Re: I want to map a Java bean object to relational database table! But do not want to use EJB.

Posted By:   zhaohua_meng  
Posted On:   Tuesday, June 4, 2002 01:54 PM

All your objects have a java.lang.Class object(by calling getClass() on your object). Using Class you can get all kinds of info about your object. An example is call getDeclaredMethods() to get all methods that is defined in that object's class. Then you may come up with a generic solution for method calling. Look at java.lang.reflect pacakge for details. This is called reflection. A good starting point is as following:
http://java.sun.com/docs/books/tutorial/collections/index.html.

Usually a generic solution comes with performance penalty.

Hope this helps.
About | Sitemap | Contact