简体   繁体   English

带有复合键的 @OneToOne 实体上的 JPA @Id

[英]JPA @Id on @OneToOne Entity with a Composite Key

I have an @Entity class, with an @Id annotation and a @OneToOne annotation on the same field.我有一个@Entity类,在同一个字段上有一个@Id注释和一个@OneToOne注释。 Usually this would not be a problem, but the entity class of the field with these annotations uses a composite key.通常这不会有问题,但是带有这些注释的字段的实体类使用复合键。 This is causing more complications than I anticipated.这导致的并发症比我预期的要多。

Here is the entity class that is posing the problem:这是造成问题的实体类:

@Entity
public class ReportDetails implements Serializable {

    @Id
    @OneToOne
    private MachineLine machineLine;
}

And here is the MachineLine entity class that is being used as an ID in ReportDetails :这是在ReportDetails用作 ID 的MachineLine实体类:

@Entity
@IdClass(MachineLine.MachineLineKey.class)
public class MachineLine {

    @Id
    @ManyToOne
    private Machine machine;

    @Id
    private long lineIndex;

    public static class MachineLineKey implements Serializable {
        private Machine machine;
        private long lineIndex;
    }
}

I have left out any extra fields and the getters and setters from these class definitions, to save space.为了节省空间,我从这些类定义中省略了任何额外的字段以及 getter 和 setter。

When I try to run my application it gives the following exception:当我尝试运行我的应用程序时,它给出了以下异常:

java.lang.IllegalArgumentException: This class [class ReportDetails] does not define an IdClass

When I put an @IdClass annotation on ReportDetails it then requires defining the individual fields of whatever class I define in @IdClass , like in MachineLine .当我在ReportDetails上添加@IdClass注释时,它需要定义我在@IdClass定义的任何类的各个字段,就像在MachineLine However, I am trying to avoid doing this, in favour of having the whole MachineLine entity returned whenever a ReportDetails entity is retrieved from the database.但是,我试图避免这样做,以便在从数据库中检索ReportDetails实体时返回整个MachineLine实体。

Is there a way of having MachineLine as the ID field of ReportDetails , without having to define extra fields within ReportDetails ?有没有办法让MachineLine作为ReportDetails的 ID 字段,而不必在ReportDetails定义额外的字段?

This is what JPA calls a "derived identity".这就是 JPA 所说的“派生身份”。 You might try something like this:你可以尝试这样的事情:

ReportDetails:报告详情:

@Entity
public class ReportDetails implements Serializable {
    // all attributes map by the relationship: AttributeOverride is not allowed
    @EmbeddedId
    private MachineLine.Id id;

    @MapsId
    @JoinColumns({
        @JoinColumn(name="machineId", referencedColumnName="machineId"),
        @JoinColumn(name="machineLineIndex", referencedColumnName="index")
    })
    @OneToOne
    private MachineLine machineLine;

    // ...
}

MachineLine:机器线:

@Entity
public class MachineLine {

    @EmbeddedId
    private Id id;

    @MapsId("machineId") // maps machineId attribute of embedded id
    @ManyToOne
    private Machine machine;

    // ...

    @Embeddable
    public static class Id implements Serializable {
        private long machineId; // corresponds to PK type of Machine
        private long index;
        // ...
    }
}

Machine:机器:

@Entity
public class Machine {

    @Id
    private long id;

    @OneToMany(mappedBy = "machine")
    private List<MachineLine> lines;

    // ...
}

Derived identities are discussed (with examples) in the JPA 2.2 spec in section 2.4.1.派生身份在JPA 2.2 规范的 2.4.1 节中讨论(通过示例)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM