2

I am using @JoinColumn Annotation in order to join two tables, but the linking results in a null value. What am I doing wrong? Is there any way to debug the linking process? Maybe it has something to do with sql database, I mean that no linking can take place? You could say I've tried everything when it comes to the code, so I'm just not sure whether the code could be the problem. I also use Postman to send the JSON string, maybe the problem is there, but that's just guesswork. Please help.

@Entity
@Data
public class Operator {


    @Id
    @GeneratedValue
    private int idOperator;

    @Column
    private String login;

    @Column
    private String password;


    @OneToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinColumn(name= "id_employee")
    private Employee employee;

@Entity
@Data
public class Employee {

    @Id
    @GeneratedValue
    private int id_employee;

    @Column
    private String firstName;

    @Column
    private String lastName;

    @OneToOne(mappedBy = "employee")
    private Operator operator;
    
}


@RestController
@RequiredArgsConstructor
public class EmployeeController {

    private final EmployeeRepository employeeRepository;


    @PostMapping("/employees")
    public Employee newEmployee(@RequestBody Employee newEmployee) {
        return employeeRepository.save(newEmployee);
    }


@RestController
@RequiredArgsConstructor
public class OperatorController {

    private final OperatorRepositry operatorRepositry;


    @PostMapping("/operators")
    public Operator newOperator(@RequestBody Operator newOperator) {
        return operatorRepositry.save(newOperator);
    }


The debug result: 

    2020-10-12 11:40:19.467 DEBUG 83385 --- [nio-8080-exec-2] org.hibernate.SQL                        : insert into operator (id_employee, login, password, id_operator) values (?, ?, ?, ?)
    2020-10-12 11:40:19.470 TRACE 83385 --- [nio-8080-exec-2] o.h.type.descriptor.sql.BasicBinder      : binding parameter [1] as [INTEGER] - [null]
    2020-10-12 11:40:19.471 TRACE 83385 --- [nio-8080-exec-2] o.h.type.descriptor.sql.BasicBinder      : binding parameter [2] as [VARCHAR] - [dbuser]
    2020-10-12 11:40:19.472 TRACE 83385 --- [nio-8080-exec-2] o.h.type.descriptor.sql.BasicBinder      : binding parameter [3] as [VARCHAR] - [skdbpassword]
    2020-10-12 11:40:19.472 TRACE 83385 --- [nio-8080-exec-2] o.h.type.descriptor.sql.BasicBinder      : binding parameter [4] as [INTEGER] - [7]
    select operator0_.id_operator as id_opera1_1_1_, operator0_.id_employee as id_emplo4_1_1_, operator0_.login as login2_1_1_, operator0_.password as password3_1_1_, employee1_.id_employee as id_emplo1_0_0_, employee1_.first_name as first_na2_0_0_, employee1_.last_name as last_nam3_0_0_ from operator operator0_ left outer join employee employee1_ on operator0_.id_employee=employee1_.id_employee where operator0_.id_operator=?
  • Is this `join employee employee1_ on perator0_.id_employee=employee1_.id_employee` a right join condition for your schema? – SternK Oct 12 '20 at 10:11
  • in your code, did you make sure you set BOTH sides of this bidirectional relationship? For clarification, does the operator have an employee and does the employee have the operator – PaulD Oct 12 '20 at 11:14
  • @SternK, I don't know, that is the debug output I got, I thought that it might somehow useful, how do you think? To PaulD,as you can read there I established a *OneToOne bidirectional relationship. But what makes me think is that when I create the very first employee, the operator in this employee is null. And after then I link (*JoinColumn) the operator to this employee - could that be a problem - I mean because the employee has a null value, there can be no link established. – KOalaDelYtaPLaYa Oct 12 '20 at 11:51

0 Answers0