#  join two persisted entities with relationships ManyToOne bidirectional and jointable

**URL:** <https://discourse.hibernate.org/t/join-two-persisted-entities-with-relationships-manytoone-bidirectional-and-jointable/4177>\
**Category:** Hibernate ORM\
**Created:** [May 18, 2020, 6:09am UTC](https://discourse.hibernate.org/t/join-two-persisted-entities-with-relationships-manytoone-bidirectional-and-jointable/4177 "2020-05-18T06:09:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![pavloNP](https://avatars.discourse-cdn.com/v4/letter/p/76d3ee/32.png) [@pavloNP](https://discourse.hibernate.org/u/pavloNP)\
**Post date:** [May 18, 2020, 6:09am UTC](https://discourse.hibernate.org/t/join-two-persisted-entities-with-relationships-manytoone-bidirectional-and-jointable/4177/1 "2020-05-18T06:09:16Z")

</div>

My goal is to join two persisted entities with relationships @ManyToOne bidirectional and @JoinTable.  
Could it be done by one query (HQL, JPQL or SQL)?

```auto
 @Entity
 @Table(name = "position")
 public class Position {
 
     @Id
     @GeneratedValue(strategy = GenerationType.IDENTITY)
     @Column(name = "POSITION_ID", updatable = false)
     private Long id;
 
     @Column(name = "NAME")
     private String name;
 
     @JoinTable(name = "Position_Employee_JT")
     @OneToMany(fetch = LAZY, cascade = ALL)
     private Set<Employee> employeeSet = new HashSet<();
 }

 @Entity
 @Table(name = "employee")
 public class Employee {
 
     @Id
     @GeneratedValue(strategy = GenerationType.IDENTITY)
     @Column(name = "EMPLOYEE_ID", updatable = false)
     private Long employeeId;
 
     @ManyToOne(fetch = LAZY)
     private Position position;

The first attempt:

  @Transactional
     public void hireEmployee(Long employeeId, Long positionId) {
         Employee employee = employeeRepository.getOne(employeeId);
        Position position = positionRepository.getOne(positionId);
        employee.setPosition(position);
        position.addEmployee(employee);
     }

It makes: 
SELECT = 3
UPDATE = 1
INSERT = 1

The second:

     public Employee hireEmployee(Long employeeId, Long positionId) {
         EntityManager em = entityManagerFactory.createEntityManager();
         em.getTransaction().begin();
         Position position = em.getReference(Position.class, positionId);
         Employee employee = em.getReference(Employee.class, employeeId);
         position.addEmployee(employee);
         employee.setPosition(position);
         em.getTransaction().commit();
 }
```

---

<div class="post-metadata">

**Author:** ![pavloNP](https://avatars.discourse-cdn.com/v4/letter/p/76d3ee/32.png) [@pavloNP](https://discourse.hibernate.org/u/pavloNP)\
**Post date:** [May 18, 2020, 1:11pm UTC](https://discourse.hibernate.org/t/join-two-persisted-entities-with-relationships-manytoone-bidirectional-and-jointable/4177/2 "2020-05-18T13:11:28Z")

</div>

I updated the entities and the method.

Employee entity:

```auto
    public Employee setPosition(Position position) {
        this.position = position;
        return this;
    }

```

Position entity:

```auto
    public void addEmployee(Employee employee) {
        employee.setPosition(this);
        employeeSet.add(employee);
    }

```

The method:

```auto
 public void hireEmployee(Long employeeId, Long positionId) {
        EntityManager em = entityManagerFactory.createEntityManager();
        em.getTransaction().begin();

        Position position = em.getReference(Position.class, positionId);
        Employee employee = em.find(Employee.class, employeeId);
        position.addEmployee(employee);

        em.getTransaction().commit();
    }

```

But why I see the three SELECT query?  
Could someone clarify it?
