在EmbeddedId中使用@JoinColumn会生成org.hibernate.MappingException

时间:2017-04-21 14:59:41

标签: java hibernate jpa

我有一个带有复合id的表,其中一个字段是另一个表的外键。

enter image description here

我试图用以下类实现这个 -

Tracking.java:

public class Tracking {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long trackingId;
    private String accountNumber;
    private TrackingType trackingType;
}

TrackingUseCaseDetails.java

public class UseCaseStatus implements Serializable {

    private static final long serialVersionUID = -7058248210575975131L;

    @EmbeddedId
    private ID id;
    private String status;
    private String resourceId;
}

ID.java

public class UseCaseStatusId implements Serializable {

    private static final long serialVersionUID = -7037374226692973832L;

    @JoinColumn(name = "trackingId", updatable = false, nullable = false)
    private Tracking tracking;
    private TrackingUseCase trackingUseCase;
}

但是我收到以下错误:

org.hibernate.MappingException: Could not determine type for: Tracking, at table: TrackingUseCaseDetails, for columns: [org.hibernate.mapping.Column(tracking)]

有关如何解决错误/改进代码的任何建议?

1 个答案:

答案 0 :(得分:1)

试试这个:

@Entity
public class UseCaseStatus implements Serializable {

    @EmbeddedId
    private UseCaseStatusId id;

    @MapsId("tracking")
    @ManyToOne
    @JoinColumn(name = "trackingId", updatable = false, nullable = false)
    private Tracking tracking;

    private String resourceId;
}


@Embeddable
public class UseCaseStatusId implements Serializable {

    //@JoinColumn(name = "trackingId", updatable = false, nullable = false) <- prob not required
    private long tracking;

    @Column(name = "status")
    private String status;

    //implement equals() and hashcode() : https://en.wikibooks.org/wiki/Java_Persistence/Identity_and_Sequencing#Embedded_Id
}