The entity describing the machine
@Entity
@Table(name = "machine")
public class Machine {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
@ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
@JoinTable(name = "machine_node", joinColumns = @JoinColumn(name = "machine_id"), inverseJoinColumns = @JoinColumn(name = "node_id"))
private List<NodeMachine> nodeMachines = new ArrayList<>();
}
The entity describing the part/node
@Entity
@Table(name = "node_machine")
public class NodeMachine {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
}
Several parts are installed on each machine (nodeMachines list), and each part can be installed on different machines, so ManyToMany was added.
For example, a wheel can be mounted on a motorcycle or a car. A motorcycle can have two wheels, and a car can have four.
I will describe in more detail what is in the tables. I must say right away that the example is not very successful, just for understanding. In the Machine table we have 100 M motorcycles (1-100) and 100 C cars (1-100). And there is only one entry in the NodeMachine table - the K1 wheel, which is suitable for all one hundred motorcycles and for all one hundred cars. From this, there is no way to determine how many wheels each motorcycle and each car should have. Therefore, I believe that there should be a third table where the number of wheels is indicated for each car and motorcycles. And I think it's too redundant to keep 200 records of wheels for motorcycles and 400 records for cars in the table.
Each part is installed on a specific machine in a certain number. I want to get the number of nodes installed in a particular machine by knowing the node and machine name. To do this, you will have to create another count_machine_node table with the fields
- machine_id
- node_id
- count
I understand that you will have to create a new entity.
@Entity
@Table(name = "node_machine")
public class CountNodeMachine {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private Long machine_id;
private Long node_id;
private Integer count;
}
But what connections do you need to register in these entities?
How to link these three tables correctly?
And do I need to create a CountNodeMachine entity?