微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何从使用@JoinTable的@OneToOne迁移到仅@OneToOne?

如何解决如何从使用@JoinTable的@OneToOne迁移到仅@OneToOne?

我们正在重组我们的服务和数据。我们有2个实体EmployeeCostCenter

@Entity
@Table(name = "employees")
data class Employee(
    @Id
    val id: Long? = null,@Column(name = "id_dept")
    val departmentId: Long,@Column(name = "business_email_address")
    var businessEmailAddress: String? = null,@OnetoOne
    @JoinTable(
        name = "employee_cost_centers",joinColumns = [JoinColumn(
            name = "employee_id",referencedColumnName = "id"
        )]
    )
    @JoinColumn(name = "cost_center_id")
    var costCenter: CostCenter? = null
)
@Entity
@Table(name = "cost_centers")
data class CostCentre(
    @Id
    val id: Long,@Column
    var name: String? = null,@Column(name = "deleted")
    var isDeleted: Boolean = false
)

员工表

| id | id_dept     | business_email_address |
|----|-------------|------------------------|
| 1  | Finance     | finance@example.com    |
| 2  | HR          | hr@example.com         |
| 3  | Development | dev@example.com        |
| 4  | Marketing   | marketing@example.com  |

cost_centers表

| id  | name                  | deleted |
|-----|-----------------------|---------|
| 10  | FinanceCostCenter     | false   |
| 20  | HRCostCenter          | false   |
| 30  | DevelopmentCostCenter | false   |
| 40  | MarketingCostCenter   | false   |

由于我们在@JoinTable实体中使用Employee,所以我们有第三个表,该表具有名为employee_cost_centers的员工和成本中心之间的映射。 employee_cost_centers表

| cost_center_id | employee_id |
|----------------|-------------|
| 10             | 1           |
| 20             | 2           |
| 30             | 3           |
| 40             | 4           |

现在,我们希望将成本中心移至其自己的微服务。因此cost_centers表也将移至新服务。 我想通过创建类似

之类的新实体,将costCenter表中的Employee引用直接映射到第三映射表employee_cost_centers
@Entity
@Table(name = "employee_cost_centers")
data class EmployeeCostCenter(
    @Id
    @Column(name="cost_center_id")
    val costCenterid: Long,@Column(name="employee_id")
    val employeeId: Long
)

但是我应该如何在Employee实体中映射它? 对于JPA /休眠,我还很陌生。非常感谢专家的任何帮助。

解决方法

替换

@OneToOne
    @JoinTable(
        name = "employee_cost_centers",joinColumns = [JoinColumn(
            name = "employee_id",referencedColumnName = "id"
        )]
    )
    @JoinColumn(name = "cost_center_id")
    var costCenter: CostCenter? = null

作者

@OneToOne
    @JoinColumns([JoinColumn(
            name = "employee_id",referencedColumnName = "id"
        )])
    var employeeCostCenter: EmployeeCostCenter? = null

或者您可以根据设计将变量名保持不变

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。