0

Can someone please explain the best way to solve this problem.

Suppose I have three classes

1.Person

2.Venue

3.Vehicle

I have a DAO method that needs to return some or all of these attributes from each of the classes after doing a query.

How do I accomplish this ? It seems very wrong to make a class PersonVenueVehicle and return that as an object to get the instance field, values.

I was taught that the database entities must be reflected by classes, if this is case how is it implemented in such a situation

5
  • How do they relate to one another? Commented Apr 3, 2013 at 21:51
  • Do you already have the database behind it or only trying to establish it? Commented Apr 3, 2013 at 21:53
  • 1
    Create a new object that holds those types, it does not have to be an entity if it is not persisted. Commented Apr 3, 2013 at 21:56
  • 1
    Based on your wording in the question and the question itself I'd say you should start with reading a book or finding and learning some tutorials over the web about ORM (object relational mapping). Commented Apr 3, 2013 at 22:02
  • I have a database behind it. I am using servlet and jsp with tomct/ mysql . No frameworks, just my own MVC Commented Apr 3, 2013 at 22:08

1 Answer 1

2

Try the Spring-ish solution. Besides your three classes, you can have 3 DAO classes, one for each. But you have a task to perform; I don't know what it is; I'm just going to guess.

Suppose you are running a taxi service; Persons schedule through your company taxis to pick them up at a Venue, and you send them a Vehicle. Call this combination a Trip, and now you want a class that manages Trips in the database. Create a class called TripService. This should use your PersonDao, your VenueDao, and your VehicleDao to create if necessary person and venue records in the database, and should do the calculations needed to choose which Vehicle to use. When it does, it should use a new TripDao to persist a new Trip object. But, as the organizer, it should create and vend the database connection to all the DAOs, and should do the commit or rollback itself.

If you're using Hibernate or JPA, your classes could be modified. But the principle is the same. Even if I have your motivation wrong, you can write a service that coordinates the three DAOs and vends the connection. It can, if it has to, use the connection itself to do a SELECT on the three tables JOINed together.

You lose much of the benefits of a database if the only statements you write are simple SELECTs and UPDATEs and INSERTs

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

3 Comments

I'm using neither with one DAO. Not my idea , just the requirements I got
One DAO for all three business classes? How can that be a requirement? I'd split your one DAO into three, despite that requirement. Classes should have a single purpose. It won't hurt anything, and it will improve your program.
You need a more senior programmer to educate management. Sigh. You should take that problem to the Programmers SO site.

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.