13

So I am surprised that doing a search on google and stackoverflow doesn't return more results.

In OO programming (I'm using java), how do you correctly implement a one-to-many relationship?

I have a class Customer and class Job. My application is for a fictious company that completes jobs for customers. My current implementation is so that the Job class doesn't have anything to do with the Customer class, there is no reference to it at all. The Customer class uses a collection and methods to hold, retrieve and modify information about the Jobs that have been assigned by and/or completed for a customer.

The question is, what if I'd want to find out for which customer a particular Job has been done? I've only found this article that's relevant: http://www.ibm.com/developerworks/webservices/library/ws-tip-objrel3/index.html.

According to the implementation of the author, I would let the Job constructor take a Customer parameter, and store it so I can retrieve it. However, I see no guarantee at all that this model can be consistent. There are no restirctions to set the related customer for a job as a customer that the job was not for, and add jobs to customers that were done for someone else. Any help on this would be appreciated.

8 Answers 8

13

There's no 100% surefire way to maintain the integrity.

The approach which is usually taken is to use one method to construct the relationship, and construct the other direction in that same method. But, as you say, this doesn't keep anyone from messing with it.

The next step would be to make some of the methods package-accessible, so that at least code which has nothing to do with yours can't break it:

class Parent {

  private Collection<Child> children;

  //note the default accessibility modifiers
  void addChild(Child) {
    children.add(child);
  }

  void removeChild(Child) {
    children.remove(child);
  }
}

class Child {

   private Parent parent;
   public void setParent(Parent parent){
     if (this.parent != null)
       this.parent.removeChild(this);
     this.parent = parent;
     this.parent.addChild(this);
   }
}

In reality, you won't often model this relationship in your classes. Instead, you will look up all children for a parent in some kind of repository.

8
  • Thanks for giving a very good answer to the point. I'd accept but I'll leave the question open for a bit longer so more answers might come out.
    – MarioDS
    Apr 10, 2012 at 11:06
  • Isn't there a typo in Parent, it has two addChild() methods
    – powder366
    Jun 11, 2015 at 18:43
  • @JoeriHendrickx What would the objects in the repository look like? I suppose it'd be sth like ParentChild{Parent p, Collection<Child> c} and Repository{ Collection<ParentChild> pc; returnParents(Child c){...}; returnChildren(Parent p){...} }.
    – Slazer
    Jan 10, 2016 at 9:12
  • @Slazer that'd be something like Repository{ Collection<Children> getAllForParent(Parent parent)} Jan 11, 2016 at 11:20
  • I'll surprise to know if is redundancy to have references to another class in both classes? isn't enough to take reference in just one of classes? Just having reference to Parent class in Child class or list of Child classes in Parent class.
    – QMaster
    May 3, 2018 at 23:37
5

Maybe you didn't expect a complex (and zero-code) answer, but there is no solution to build your bombproof API the way you intend it. And it's not because the paradigm (OO) or the platform (Java), but only because you made a wrong analysis. In a transactional world (every system that models real life problems and their evolution over time is transactional) This code will ever break at some point:

// create
Job j1 = ...
Job j2 = ...
...
// modify
j1.doThis();
...

// access
j2.setSomeProperty(j1.someProperty);

because at the time j1.someProperty is accessed, j1 and j2 could not even exist :)

TL;DR

The long answer to this is immutability, and it also introduces the concepts of life cycle and transactions. All other answers tell you how to do it, instead I want to outline why. A one-to-many relationship has two sides

  1. has many
  2. belongs to

Your system is consistent as long as if Customer A has Job B, the Job B belongs to Customer A. You can implement this in a number of ways, but this must happen in a transaction, ie a complex action made of simple ones, and the system must be unavailble until the transaction has finished execution. Does this seem too abstract and unrelated to your question? No, it isn't :) A transactional system ensures that clients can access system's objects only if all these objects are in a valid state, hence only if the whole system is consistent. From other answers you see the amount of processing needed to solve some problems, so that guarantee comes at a cost: performance. This is the simple explanation why Java (and other general purpose OO languages) can't solve your problem out of the box.

Of course, an OO language can be used to both model a transactional world and accessing it, but special care must be taken, some constraints must be imposed and a special programming style be required to client developers. Usually a transactional system offers two commands: search (aka query) and lock. The result of the query is immutable: it's a photo (ie a copy) of the system at the very specific moment it was taken, and modifying the photo has obviously no effect on the real world. How can one modify the system? Usually

  1. lock the system (or parts of it) if/when needed
  2. locate an object: returns a copy (a photo) of the real object which can be read and written locally
  3. modify the local copy
  4. commit the modified object, ie let the system update its state based on provided input
  5. discard any reference to (now useless) local objects: the system has changed changed, so the local copy isn't up to date.

(BTW, can you see how the concept of life cycle is applied to local and remote objects?)

You can go with Sets, final modifiers and so on, but until you introduce transactions and immutability, your design will have a flaw. Usually Java applications are backed by a database, which provides transactional functionalities, and often the DB is coupled with an ORM (such as Hibernate) to write object oriented code.

2
  • 1
    I've read your answer entirely and I understand most of what you say. It's very interesting and I wish my school explained more things to us like you are doing now. Indeed normally applications do have databases, but we've only just been introduced to databases at school, and they load us up with so much work I don't find time to bury deeper into them / experiment with them at the moment. Thanks for your great contribution here.
    – MarioDS
    Apr 10, 2012 at 16:38
  • 1
    I just wanted to point out the importance of the design phase: you can model a system in different ways given a set of resources (time, $$$, knowlege), but each solution will have its limits and you have to make them explicit, because even the simplest one can be the best one, but you must carefully outline requirements, limits and scope :)
    – Raffaele
    Apr 10, 2012 at 16:46
2

You can ensure that there are no duplicates by using a Set implementation like HashSet instead of using other data-structure. And instead of adding Job to a customer, create an final inner class in Job class that has private constructor. That ensure that the wrapper inner class can only be created by a job object. Make you Job constructor take in jobID and customer as parameter. To maintain consistency -if customer is Null throw Exception as dummy jobs shouldn't be created .

In add method of Customer, check to see if the Job wrapped by JobUnit has the same customer ID as the its own id, if not throw Exception.

When replacing a customer in Job class remove the JobUnit using the method provided by Customer class and add itself to the new customer and change the customer reference to the newly passed customer. That way you can reason with your code better.

Here's what your customer class might look like.

public class Customer {

    Set<JobUnit> jobs=new HashSet<JobUnit>();    
    private Long id;
    public Customer(Long id){        
        this.id = id;
    }

    public boolean add(JobUnit unit) throws Exception{
       if(!unit.get().getCustomer().id.equals(id))
           throw new Exception(" cannot assign job to this customer");
        return jobs.add(unit);
    }

     public boolean remove(JobUnit unit){
        return jobs.remove(unit);
    }

    public Long getId() {
        return id;
    }

}

And the Job Class:

public class Job {
Customer customer;
private Long id;

final JobUnit unit;

public Job(Long id,Customer customer) throws Exception{
    if(customer==null)
        throw new Exception("Customer cannot be null");
    this.customer = customer; 
   unit= new JobUnit(this);       
    this.customer.add(unit);
}

public void replace(Customer c) throws Exception{      
    this.customer.remove(unit);
    c.add(unit);
    this.customer=c;
}

public Customer getCustomer(){
    return customer;
}

/**
 * @return the id
 */
public Long getId() {
    return id;
}

public final class JobUnit{
    private final Job j;


    private JobUnit(Job j){
        this.j = j;

    }
    public Job get(){
        return j;
    }
 }
}

But one thing I'm curious about is why do you even need to add jobs to a customer object? If all you want to check is to see which customer has been assigned to which job, simply inspecting a Job will give you that information. Generally I try not to create circular references unless unavoidable. Also if replacing a customer from a job once its been created is not necessary, simply make the customer field Final in the Job class and remove method to set or replace it.

The restriction for assigning customer for a job should be maintained in database and the database entry should be used as a checking point. As for adding jobs to customer that were done for someone else, you can either check for customer reference in a job to ensure that the customer to which a job is being added is the same one it holds or even better-simply remove any reference in customer for Job and it will simplify things for you.

1
  • Thanks for putting your time into this. It all makes sense but the implementation is a bit overkill. I have already decided to make the customer field final in the job class, and by doing that the rest became rather easy. I will use your answer for further reference though. Thanks.
    – MarioDS
    Apr 10, 2012 at 14:45
1

If the Customer object owns the relationship then you can possibly do it this way:

Job job = new Job();
job.setStuff(...);
customer.addJob(Job job) {
    this.jobs.add(job);
    job.setCustomer(this); //set/overwrite the customer for this job
}

//in the job class
public void setCustomer(Customer c) {
    if (this.customer==null) {
        this.customer = c;
    } // for the else{} you could throw a runtime exception
}

...if the ownership is the other way around, just substitute customer for job.

The idea is to have the owner of the relationship maintain consistency. Bi-directional relationships generally imply that the consistency management sits in both entities.

2
  • That's what I thought of, but it isn't a guarantee for consistency. There is still a public method for setting the customer, which can be changed after adding the job... Of course as a programmer you have to break the consistency yourself that way. But there must be a way to actually guarantee it? Edit: making the field final might be a solution...
    – MarioDS
    Apr 10, 2012 at 10:11
  • 1
    ...or you can make it explicit in job.setCustomer (see edited answer) Apr 10, 2012 at 10:28
0

Make a proper setter-function that maintains consistency. For instance, whenever you create a job, you supply the customer in the constructor. The job constructor then adds itself to the customer's list of jobs. Or whenever you add a job to a customer, the add function has to check that the job's customer is the customer it's being added to. Or some combination of this and similar things to what suits your needs.

5
  • Won't work if I want to add a new job to the list... I'll get duplicates. Like customer.addJob(new Job(this, a, b, c)); will result in the job being added twice. And your solution creates the need for a public method like that, so...
    – MarioDS
    Apr 10, 2012 at 10:15
  • Then don't call it that way, just do new Job(this, ..)
    – Matsemann
    Apr 10, 2012 at 10:17
  • My concern is not that consistency is guaranteed when you don't break it yourself. You are right in every way of course, but I want to make my classes in such way that no one is programmatically able to break consistency, not just because they ignore a public method. Both methods and constructors need to be 100% usable in every way a programmer may want to use them.
    – MarioDS
    Apr 10, 2012 at 10:19
  • I cannot reply to your comment on the other answer as I'm new here, so here it goes: if you have a public function in job to change the customer, this function has to maintain consistency by removing itself from the old customer and adding itself to the new.
    – Matsemann
    Apr 10, 2012 at 10:20
  • I appreciate your help and understand what you're trying to explain, but I'm not sure you understand which problems might occur with that solution. I'm going to work a bit further on it, and see where I get.
    – MarioDS
    Apr 10, 2012 at 10:23
-1

Just implement some sort of collection in the object that has the other objects For example in customer you could say:

private List<Job> jobs; 

then by using getters and setters you can add values jobs to this list. This is basic OO stuff, I don't think you searched enough on the internet. there is a lot of info available on these subjects.

Btw, you can use all sort of collections (Sets, Lists, Maps)

3
  • That does not answer my question, you just repeat what I already have. Have you read my question?
    – MarioDS
    Apr 10, 2012 at 10:28
  • Apparntly I was to quick in responding because I missed the essence of the question. My bad, sincerest apologies:)
    – steelshark
    Apr 10, 2012 at 13:48
  • No problem. The down vote was not mine though, because your answer was not incorrect or irrelevant.
    – MarioDS
    Apr 10, 2012 at 13:58
-1

I know this is late but I think another way to this would be to look at the problem a bit differently. Since customer holds a collection of all jobs assigned by or completed for a customer, you could consider the job class to be a sub class of customer with extra information of having all the jobs completed by the customer. Then you would only have to maintain customer id in the main class and it would be inherited. This design would ensure that each job can be linked to a customer. Also if for a customer you want to find out how many jobs are present that too also would be got.

-1

I am sorry I know this is very late but I have come across a similar problem where I feel the best solution is to follow a inheritance model. Think of job as being jobs done/asisgned by a particular customer. So in that case the Customer would be a super class with the Job(Lets call is customer job) being a sub class since a Job cannot exists without a customer. A customer would also have a list of jobs primarily for ease of data fetching. Intutively this does not make sense since Job and Customer done seem to have any relation, however once you see that Job cannot exist without a customer, it just becomes an extension of customer.

8
  • Late answers are not a problem but this is a wacky idea. So if I call toString() on this what is it going to display? This is a violation of the single responsibility principle. In any case, you completely ignored the collection that is the source of potential inconsistency. Nov 8, 2016 at 16:12
  • Ok, firstly on calling toString() you could pring the current values and the super classes toString() and not the collection. The collection is only present to store multiple children, it need not be present in toString(). Regarding single responsibility then that is satisfied as the customerJob class has operations only related to customerJob and not anything else. The point here is since customerJob cannot exist without a customer I dont see any point in having a composition since a customerJob is an extention of customer. Please do clarify what is wrong instead of just downvoting this.
    – prashant
    Nov 8, 2016 at 19:01
  • You have a misconception about what inheritance is. I can't exist without food, water, or planet earth. I'm not an extension of any of them. But philosophical violations of the principle of least astonishment aside. If a job inherits from customer then the customer instance within the job instance have a 1 to 1 relationship. Sorry but this just makes no sense. Nov 8, 2016 at 19:30
  • Ok, when you say you cant exist without food and water, you are talking about living. But what I am saying is that if Tiger extends Animal and there is no animal then there are no tigers which is the case regaring Customer and Customer Job (i.e. there is no customer job without job). The second part of what you say I agree to however in this case if we establish a composition relationship then we miss the inheritance part which I believe should be captured, so if we keep the inheritance and maintain the many to one relation using a collection then IMO we could get the best of two worlds
    – prashant
    Nov 8, 2016 at 19:41
  • The inheritance would ensure that an entity contains all data is needs to exists and the collection would establish much needed many to one relationship. The class would still only have one responsiblity and for all its operations it would use the inehritance model and not the many to one relationship. However while being used as a DTO it could store a list of children thereby not requiring an additional structure to store a list of children
    – prashant
    Nov 8, 2016 at 19:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.