Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement many-to-many relationship in Java?

Tags:

java

I'm working on a project that calculates the fee for volunteers, who worked at a party, depending on their working hours.

The following is true:

  • an employee can work at many party's
  • a party has many employees
  • an employee can choose how many hours he wants to work at a particular party.

eg: person A decides to work 4 hours at party A and 5 hours at party B, person B decides to work 3 hours at party A and 6 hours at party B. - It's important to know how many employees worked at a party, because the budget will be divided among the employees.

I want to calculate the fee for each volunteer on a given party How do I know who worked how many hours at which party? I think the code I have written now isn't the best option, because the objects get stored twice. If I remove the list of employees in the Party class, how do I know how many employees worked at the party?

public class Party {

private List<employee> employees;    
//other attributes

//methods
}

public class Employee {

private List<Hours> hours;    
// other attributes

//methods
}

public class Hours{

private Party party;    
private double hours;    
//other attributes

//methods
}
like image 466
thomas Avatar asked Nov 30 '25 21:11

thomas


1 Answers

define class that represent the relation

class Employment{
    private Party party;
    private Employee employee;
    private Hours hours;

    // ...
}

create list of employments

List<Employment> employments;
like image 136
birneee Avatar answered Dec 03 '25 13:12

birneee



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!