1

Is there a way to build a JavaBean dynamically based on the DB table column names and assoicated data? For example, if I get back of 5 columns in a db table, I need to create a javaBean with those 5 column names as variables and their getters/setters as well. Next time, if I get 7 columns, I can create a javaBean with 7 variables.

1
  • your means is: Java beans automatically synchronized with database tables? Commented Jul 23, 2012 at 7:41

5 Answers 5

1

I think that you can do this with byte code manipulation.

2 main libraries are:

Sign up to request clarification or add additional context in comments.

1 Comment

This is best solution but very complex;)
0

I don't believe you can have an object with variable fields, but you could use a Map to store the results. That might be the closest to what you want.

2 Comments

Thanks Eivind.I want this thing to be done without use of map .. I want to send these bean Objects to Adobe Flex framework
You can look at this SO answer to a similar question.
0

Well it doesn't have to be a "dynamic" bean, you can just use a Map variant, like HashMap. Nothing wrong with that.

Map<String,Object> results = new HashMap<String,Object>();
// .. fill results ...
// .. then
Object someData = results.get("someDataKey");

Comments

0

The Answer is no. You cannot ask for variables at run time. What you can do is.

Map<String, Map> results = new HashMap<String, Map>();
// Associate each column with a new map and put it in results.
// Take each variable from column and associate it with the corresponding column's hashmap.

Comments

0

You can implement the org.apache.commons.beanutils.DynaBean interface:

A DynaBean is a Java object that supports properties whose names and data types, as well as values, may be dynamically modified. To the maximum degree feasible, other components of the BeanUtils package will recognize such beans and treat them as standard JavaBeans for the purpose of retrieving and setting property values.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.