4

Let's assume i have two classes which look like this:

public class Human {
     private String name;
     private String id;
     private List<Bike> bikes;
}

public class Bike {
    private String model;
    private String id;
    private String type;
}

Now, is it a good idea to put String humanID into bike class? I can do pretty every operation with current structure, but if for example i get bike object and want to know his owner, i need to iterate over humans collection. On the other hand if i put humanID inside bike, i feel it would be kinda redundant information, because relation(agregation) itself tells who is the owner. So can someone explain which one is the right approach and why?

Bruno
  • 2,889
  • 1
  • 18
  • 25
peter Schiza
  • 387
  • 7
  • 23
  • 2
    https://stackoverflow.com/questions/1813866/managing-bidirectional-associations-in-my-java-model – shmosel Jun 20 '17 at 23:25

3 Answers3

4

It depends entirely on your usecase.

  1. underlying assumptions:
    For instance, if you are writing code for an insurance company, it makes sense that every bike has one registered owner. And it would make sense to store the owner for each vehicle. Whereas in other situations, you might not want to store the owner. Or there might even exist bikes without owners at all. Or multiple owners.
  2. speed: As you indicated already, the lookup time might differ depending on how you model your data. Think about what your program will be like, and what operations will occur a lot. If you're going to spend a lot of time looking up the owner of a certain bike, it makes sense to ensure this operation is a fast one.
  3. visibility: If all the lookups related to "who owns this particular bike" happen in the same class, you might be better off using a simple Map. This allows you to forego tying one class to another.
Joris Schellekens
  • 8,483
  • 2
  • 23
  • 54
1

First, lets think about the how it works in real life (to most cases):

  • A Human can have many bikes that he wants
  • A bike will have only one owner

How would it be mapped in a database?

In a database scenario, we would have three tables: Human, Bike and HumanBike, where this last one, will serve just to map the bike's owner.

Ex:

enter image description here

How would it be mapped in a Java?

The Human class, would have a Set of type Bike, mapping all the bikes owned by Human instance:

public class Human {
     private String name;
     private String id;
     private Set<Bike> bikes;
}

In the other hand, the Bike class could have a Human field, mapping its owner:

public class Bike {
    private String model;
    private String id;
    private String type;
    private Human owner;
}

This way, we implemented a bidirectional association, making it easy to access both sides of the relation.

But, what is best for my program?

We dont know!

This is just a suggestion, you may have an entire different problem and business inn your software, so, I recommend you to read oris Schellekens answer down here, and see what fits best for you.

Bruno
  • 2,889
  • 1
  • 18
  • 25
  • One normally will not find a join table in a relational database to model a one-to-many relationship, but a simple foreign-key relationship. The data model you propose would require extra work to suppress many-to-many behavior and is not good. Also, relational modeling is not object modeling, so isn't a good basis for such reasoning. In an object model, for example, we won't have an artificial "ID" key. (An ID from the domain is fine.) One should use the natural `equals` value semantics to relate the types. – Lew Bloch Jun 21 '17 at 01:04
  • What about a tandem bike? Do those have one owner? – OneCricketeer Jun 21 '17 at 01:08
0

You could add a field say, owner inside bike which would point to one of the humans. But you could also have a third class: class HumanHasBike{ Human human; List<Bike> bikes;}

ACV
  • 9,964
  • 5
  • 76
  • 81