3

I use Spring MVC and a regular JDBC.

I've just learned that I should separate business process into layers which are presentation layer, controller layer, service layer, and repository/DAO layer. Now suppose that I have an Entity called Person that can have multiple Jobs. Job itself is another entity which have its own properties. From what I gathered, the repository layer only manages one entity. Now I have one entity that contains another entity. Where do I "join" them? The service layer?

Suppose I want to get a person whose job isn't known yet (lazy loading). But the system might ask what the job of that particular person is later on. What is the role of each layer in this case?

Please let me know if I need to add any detail into this question.

William Wino
  • 3,599
  • 7
  • 38
  • 61

3 Answers3

0

In the typical OOP the relationship between objects are made by creating an association. If the association between a Job and Person is many-to-one. Then you should add a property of a Person to the Job. From the other hand the association between Person and Job is one-to-many, so you could add a set of Jobs to the Person. You can map this association for lazy loading if you don't want to load all associated jobs of the person. This is used by default in ORM and JPA.

class Person {
 Set<Job> jobs;
}

class Job {
 Person person;
}

Each layer used to separate and decouple the logic used to handle the same or different objects.

On the other hand the objects used to map your object model could be different on each layer and you have to transform data when you need to update the model. It depends on implementation of the persistence framework used for the persistence layer. Having a service layer you can abstract from the persistence layer implementation, and if you lately change the persistence framework the business logic that is encapsulated in the service layer wouldn't change. The presentation layer might also contain its own objects known as a view objects used to handle different aspects of the presentation layer. The logic of creating, manipulating, and presenting these objects belongs to a presentation layer which is obviously implemented by the presentation framework.

  • Job should not contain a person field, its a many to many relationship – Kevin Bowersox Oct 22 '13 at 09:55
  • @KevinBowersox OP didn't say that multiple persons can do the same job, so it can't be many-to-many relationship and the question is probably not to how to associate those objects but how different layers work together. –  Oct 22 '13 at 09:59
0

From what I could gather from the problem statement:-

  1. Person - can exists without job. Can have zero or more Job(s).
  2. Job - Can exist independent of Person(s) performing it.

A cleaner way would be to have a entity (and thus its table) that encapsulate this mapping:-

class Employment{
     private Person person;
     private Job job;
}

Now you can query from both ends. Like:-

  • SELECT FROM EMPLOYMENT WHERE PERSON.ID=xyz

This might give 0 or more rows.

Each row will have information of mapped Job too.

So in this case you will have your service/repository something like:-

interface EmployementRepository{

   //  CRUD methods on Employement.
}
Kumar Sambhav
  • 7,503
  • 15
  • 63
  • 86
  • I have the employment table in the database. But assume in my case I don't need to know who are having a particular job. So, I can only access the job properties through the person entity. Anyway, this is NOT a ER design problem, I just want to know the "right" way to "fetch" the data of a property of an entity. I need to understand how these layers work, that's all. – William Wino Oct 22 '13 at 10:19
0

It sounds like your dealing with a many to many relationship. If multiple people can have the same job you will need to create a junction table.

DATA Model

CREATE TABLE PERSON(PERSON_ID, OTHER_FIELDS);
CREATE TABLE JOB(JOB_ID, OTHER_FIELDS);
CREATE TABLE PERSON_JOB(PERSON_JOB_ID, PERSON_ID, JOB_ID, OTHER_FIELDS);

Entities

class Person{
    List<Job> jobs = new ArrayList<Job>();
}

class Job{
    List<Person> workers = new ArrayList<Person>();
}

In the repository/dao layer, you will need to create the logic to fill these associations between the two entities using the junction table.

Kevin Bowersox
  • 93,289
  • 19
  • 159
  • 189
  • I'm not having an entity relation design problem, it's a programming paradigm problem. And you haven't answered my question yet. I can join multiple table just fine. But I need to know the right way to do it in this particular programming paradigm. – William Wino Oct 22 '13 at 10:12
  • @William You asked where to join them, my answer is the repository/dao layer. – Kevin Bowersox Oct 22 '13 at 10:15
  • Sorry, I didn't realize that's my title. Anyway I want to understand the relationship between the layers as well. That's why I stated the `What is the role of each layer in this case?` question. So should I join them by query? What about the lazy loading? Where should I put the code that request to the database when I need the data? – William Wino Oct 22 '13 at 10:24