使用Hibernate基于连接的外键联接实体

时间:2019-03-19 14:31:43

标签: java sql hibernate jpa javax.persistence

我试图在我正在处理的Java程序中创建两个实体,并将它们连接在一起。我有两个表,我们称它们为SUB_AREALOCATION,它们相互连接。这两个表都是使用标准.findById(id)的{​​{1}}方法填充的,因此所有字段都与数据库中的列匹配。

JpaRepositoryBroker表连接到SUB_AREA表,其中:

LOCATION

所以如果:

LOCATION.LOCATION_ID = CONCAT(SUB_AREA.AREA_ID, SUB_AREA.SUB_AREA_ID)

然后:

LOCATION.LOCATION_ID = 1234

通常,我会加入这样的内容:

SUB_AREA.AREA_ID = 12

SUB_AREA.SUB_AREA_ID = 34

但是我需要更多类似的东西:

@JoinColumn(name = "ID", referencedColumnName = "LOCATION_ID")

有没有一种使用hibernate / javax来处理这种类型的外键联接的方法?我无法对数据库本身进行更改。

这里是我正在描述的代码的模拟:

SUB_AREA:

@JoinColumn(name = "DPOT_ID" + "SUB_DPOT_NBR", referencedColumnName = "ENTY_CDE")

SUB_AREA私钥:

@Builder
@Entity
@Table(name = "SUB_AREA", schema = "AREA")
@Data
@NoArgsConstructor
@AllArgsConstructor
public class SubAreaEntity {

    @EmbeddedId
    private SubAreaEntityPk pk;

    @ManyToOne
    @JoinColumn(name = "ID", referencedColumnName = "LOCATION_ID") // what to do here?
    @NotFound(action = NotFoundAction.IGNORE)
    private LocationEntity locationEntity;
}

位置:

@Builder
@Embeddable
@Data
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode
class SubAreaEntityPk implements Serializable {

    @Column(name = "AREA_ID", nullable = false, length = 2)
    private String areaId;

    @Column(name = "SUB_AREA_ID", nullable = false, length = 2)
    private String subAreaId;

}

0 个答案:

没有答案