Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring boot 如何从使用@JoinTable的@OneToOne迁移到只使用@OneToOne?_Spring Boot_Hibernate_Kotlin_Jpa_Spring Data Jpa - Fatal编程技术网

Spring boot 如何从使用@JoinTable的@OneToOne迁移到只使用@OneToOne?

Spring boot 如何从使用@JoinTable的@OneToOne迁移到只使用@OneToOne?,spring-boot,hibernate,kotlin,jpa,spring-data-jpa,Spring Boot,Hibernate,Kotlin,Jpa,Spring Data Jpa,我们正在重组我们的服务和数据。我们有两个实体Employee,CostCenter @Entity @Table(name = "employees") data class Employee( @Id val id: Long? = null, @Column(name = "id_dept") val departmentId: Long, @Column(name = "business_emai

我们正在重组我们的服务和数据。我们有两个实体
Employee
CostCenter

@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
)
员工表

| 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  |

成本中心表

| id  | name                  | deleted |
|-----|-----------------------|---------|
| 10  | FinanceCostCenter     | false   |
| 20  | HRCostCenter          | false   |
| 30  | DevelopmentCostCenter | false   |
| 40  | MarketingCostCenter   | false   |
| cost_center_id | employee_id |
|----------------|-------------|
| 10             | 1           |
| 20             | 2           |
| 30             | 3           |
| 40             | 4           |
当我们在
Employee
实体中使用
@JoinTable
时,我们有一个第三个表,它通过名称
Employee\u cost\u centers
在员工和成本中心之间进行映射。 员工成本中心表

| id  | name                  | deleted |
|-----|-----------------------|---------|
| 10  | FinanceCostCenter     | false   |
| 20  | HRCostCenter          | false   |
| 30  | DevelopmentCostCenter | false   |
| 40  | MarketingCostCenter   | false   |
| cost_center_id | employee_id |
|----------------|-------------|
| 10             | 1           |
| 20             | 2           |
| 30             | 3           |
| 40             | 4           |
现在我们想把成本中心转移到它自己的微服务上。因此,
cost\u centers
表也将移动到新服务。 我想通过创建一个新实体,将
Employee
表中的
costCenter
引用直接映射到第三个映射表
Employee\u cost\u 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/Hibernate相当陌生。非常感谢专家的任何帮助。

替换

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

或者,根据您的设计,可以保持变量名称不变