Reputation: 49
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?
Upvotes: 2
Views: 1961
Reputation: 16844
Use JPA. A popular implementation of JPA is Hibernate. There are plenty of tutorials to get you started.
Upvotes: 0
Reputation: 3505
you can use Hibernate
or study droidpersistence library's source, (the lib is written for android though)
Upvotes: 0
Reputation: 220832
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
Upvotes: 2