在spring中以双向一对一关系添加子条目

hgqdbh6s  于 2023-08-02  发布在  Spring
关注(0)|答案(1)|浏览(111)

我是spring jpa的新手,学习实体关系OneToOne双向
我创造了这两个实体
Account.java

@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@Builder
@Entity
@Table(name = "account")
public class Account {

    @Id
    private int id;
    private String password;
    @OneToOne(cascade = CascadeType.ALL)
    private Employee employee;
}

字符串
Employee.java

@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
@Builder
@Entity
@Table(name = "employee")
public class Employee {
    @Id
    private int id;
    private String name;
    private String email;
    @OneToOne(cascade = CascadeType.ALL,mappedBy = "employee")
    @JsonBackReference
    private Account account;
}


保存帐户实体的帐户服务

public ResponseEntity<?> addAccount( Account account) {
        try {
            Account response = accountDAO.save(account);
    
            if (response != null) {
                return ResponseEntity.status(HttpStatus.CREATED).body(response);
            } else {
                return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).build();
            }
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
        }

    }


在员工服务中保存员工时

private ResponseEntity<?> addEmployee(Employee employee) {
        try {
            Employee response = employeeDAO.save(employee);
            if (response != null) {
                return ResponseEntity.status(HttpStatus.CREATED).body(response);
            } else {
                return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).build();
            }
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getMessage());
        }

    }


当我用下面的json添加帐户时,我可以在帐户中添加员工及其引用

{
    "id": 1001,
    "password": "Amit47@123",
    "employee": {
        "id": 1,
        "name": "Amit Kumar",
        "email": "Amit@Naskay.in"
    }
}


在数据库中检查

mysql> select * from account;
+------+------------+-------------+
| id   | password   | employee_id |
+------+------------+-------------+
| 1001 | Amit47@123 |           1 |
+------+------------+-------------+
1 row in set (0.00 sec)

mysql> select * from employee;
+----+----------------+------------+
| id | email          | name       |
+----+----------------+------------+
|  1 | Amit@Naskay.in | Amit Kumar |
+----+----------------+------------+
1 row in set (0.00 sec)

mysql>


但是当我尝试在下面添加Employee时

{
        "id": 2,
        "name": "Amit Kumar",
        "email": "Amit@Naskay.in",
        "account":{
    "id": 1002,
    "password": "Amit47@123"
    }
}


并在数据库中登记

mysql> select * from account;
+------+------------+-------------+
| id   | password   | employee_id |
+------+------------+-------------+
| 1001 | Amit47@123 |           1 |
| 1002 | Amit47@123 |        NULL |
+------+------------+-------------+
2 rows in set (0.00 sec)

mysql> select * from employee;
+----+----------------+------------+
| id | email          | name       |
+----+----------------+------------+
|  1 | Amit@Naskay.in | Amit Kumar |
|  2 | Amit@Naskay.in | Amit Kumar |
+----+----------------+------------+
2 rows in set (0.00 sec)


这不是预期的,因为帐户1002的employee_id为空,我期望由于我们的关系是双向的一对一,它应该更新帐户1002的employee_id为2时保存实体在数据库。请帮助我理解双向关系是如何工作的

zbsbpyhn

zbsbpyhn1#

只需在Employee类中添加一个注解到Account字段中,就像这样:

@JoinColumn (name="account")

字符串
再想想你的知识:

一对一表示单个实体与另一实体的单个示例相关联。源实体的一个示例最多可以Map到目标实体的一个示例。
已更新

实体Map看起来如下:

Account

    @OneToOne(cascade = CascadeType.ALL, mappedBy = "account")
    private Employee employee;

Employee 

   @OneToOne(cascade = CascadeType.ALL)
   @JoinColumn(name = "account_id", referencedColumnName = "id")
   private Account account;

更新

更多的细节请检查这篇文章它真的很好它会帮助你,Vlad Mihalcea: onetoone-relationship

相关问题