title: How to create relationships in entities correctly
body: <p>The entity describing the machine</p>
  <pre><code>@Entity
  @Table(name = &quot;machine&quot;)
  public class Machine {
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;
      private String name;      
      @ManyToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL)
      @JoinTable(name = &quot;machine_node&quot;, joinColumns = @JoinColumn(name = &quot;machine_id&quot;), inverseJoinColumns = @JoinColumn(name = &quot;node_id&quot;))
      private List&lt;NodeMachine&gt; nodeMachines = new ArrayList&lt;&gt;();
  }
  </code></pre>
  <p>The entity describing the part/node</p>
  <pre><code>@Entity
  @Table(name = &quot;node_machine&quot;)
  public class NodeMachine {
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;
      private String name;
  }
  </code></pre>
  <p>Several parts are installed on each machine (nodeMachines list), and each part can be installed on different machines, so ManyToMany was added.</p>
  <p>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.</p>
  <p>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.</p>
  <p>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</p>
  <pre><code>- machine_id
  - node_id
  - count
  </code></pre>
  <p>I understand that you will have to create a new entity.</p>
  <pre><code>@Entity
  @Table(name = &quot;node_machine&quot;)
  public class CountNodeMachine {
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long id;
      private Long machine_id;
      private Long node_id;
      private Integer count;
  }
  </code></pre>
  <p>But what connections do you need to register in these entities?</p>
  <p>How to link these three tables correctly?</p>
  <p>And do I need to create a CountNodeMachine entity?</p>