user3235397
user3235397

Reputation: 49

java - map database tables into class

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

Answers (3)

BetaRide
BetaRide

Reputation: 16844

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

Upvotes: 0

Rupesh
Rupesh

Reputation: 3505

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

Upvotes: 0

Lukas Eder
Lukas Eder

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

Related Questions