【问题标题】:How to join repeated column JPA Hibernate如何加入重复列JPA Hibernate
【发布时间】:2021-07-28 06:21:42
【问题描述】:

我有 2 个字段和这样的连接:

@ManyToOne(targetEntity = Coordinate.class, fetch = FetchType.LAZY)
@JoinColumn(name="coordinate_id")
private Coordinate startRouteCoordinateId; 

@ManyToOne(targetEntity = Coordinate.class, fetch = FetchType.LAZY)
@JoinColumn(name="coordinate_id")
private Coordinate endRouteCoordinateId; 

但我收到错误:实体映射中的重复列

如何在 Hibernate 中使用同一类的 2 个字段?

谢谢

【问题讨论】:

    标签: hibernate jpa spring-data-jpa hibernate-mapping


    【解决方案1】:

    @ManyToOne 基本上意味着具有这两个字段的实体可以在多个坐标中呈现。

    例子:

    @Entity
    public class SomeEntity { 
       @ManyToOne(targetEntity = Coordinate.class, fetch = FetchType.LAZY)
       @JoinColumn(name="coordinate_id")
       private Coordinate startRouteCoordinateId; 
    }
    

    在此示例中,您在 SomeEntity 的表中有:MANY SomeEntity -> ONE Coodinate by coordinate_id column。

    这就是为什么只有通过coordinate_id 列将其映射到单个(ONE)坐标才有意义。 可能您对在您的情况下应该将哪个 ONE 映射到哪个 MANy 有误解。

    【讨论】:

    • 不,我需要两个坐标起点和终点。我只是错误地指定了 JoinColumn 的名称。 @JoinColumn(name="start_route_coordinate_id") @JoinColumn(name="end_route_coordinate_id")
    猜你喜欢
    • 2015-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-26
    • 2010-09-20
    • 1970-01-01
    • 2015-03-03
    • 2016-07-07
    相关资源
    最近更新 更多