shx
shx

Reputation: 1138

Populate Java object model from relational database

I need help with example from book "Spring Recipes" by Josh Long. Trying to extend code example from chapter Spring @MVC with persistence layer. Author hardcoded all data. I'm searching the best-practice solution for model like this, database schema design and DAO based population of POJO data. I don't want to use Hibernate or JPA, just Spring.

Here's the model which I'm practicing with (getters and setters are omitted to reduce space):

public class Reservation {

    private String courtName;
    private Date date;
    private int hour;
    private Player player;
    private SportType sportType;

}

public class Player {

    private String name;
    private String phone;

}

public class SportType {

    private int id;
    private String name;

}

I made 3 DB tables: reservation, player, sport_type. For every single table I have DAO class: ReservationDao, PlayerDao, SportTypeDao. Is that correct way?

What kind of relations in database do I need for these kind of object design?

How do I populate Reservation object in my service layer? Do I have to use one query with some joins or call ReservationDao, PlayerDao and SportTypeDao methods one by one to get single Reservation row from database?

Upvotes: 0

Views: 1524

Answers (1)

Adam Gent
Adam Gent

Reputation: 49085

I'll give you some ideas that are perhaps controversial but this is a design question so its going to be opinionated.

I would not make DAOs and only make a Service layer and query all three tables with a multiple inner join if possible (that is join Player and SportType). The reason is if your going to use straight SQL you might as well leverage the database (besides most ORM's would most likely eagerly load also).

Because in manual SQL webapps you need to create many composite like objects based on different queries and views I find the DAO indirection a complete waste of time. If you really want DAO OOP you should use an ORM instead. Otherwise think of manual SQL more of functional/procedural programming style where you are always transforming data (DTO). Thus you better like clean but tedious (and not so DRY) transformations over shoving domain objects all over the place.

You should also know Spring has easy ways to map JDBC ResultSet to Java Beans.

Upvotes: 2

Related Questions