0

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?

2
  • 1
    If you aren't going to use an ORM framework, you have to do it yourself. Commented Sep 28, 2013 at 16:25
  • Sotirios is correct, if you don't want to use a ORM, you're going to have to write everything yourself. There is an example of how to write an ORM in Applying UML and Patterns by Craig Larman. BUT, only in the older versions of the book, in the new version of the book he takes that chapter out with a statement to the effect of "Don't write this yourself, use a ORM library like hibernate or something similar." Commented Sep 28, 2013 at 16:38

1 Answer 1

2

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.

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

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.