2

When retrieving data from the database in Java, how can I get a instance of an class for each database row dynamically?

Example: I've got two tables (Table1: id, name, Table2: id, name, age) and two classes with the same properties. For every row in Table1 I want an List-item (or something related) for this row.

Is there a class/default method to do this in Java?

1
  • 2
    Use Hibernate. What you're looking for is an ORM. Commented May 7, 2014 at 9:14

3 Answers 3

2

While you could implement your own mapper, Hibernate (and the other JPA implementations) will solve your problem quite thoroughly out of the box. If properly configured, loading records from Table1 will automatically materialise your @OneToMany relationship with Table2

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

Comments

0

you can use Hibernate or study droidpersistence library's source, (the lib is written for android though)

Comments

0

Use JPA. A popular implementation of JPA is Hibernate. There are plenty of tutorials to get you started.

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.