同じフィールドに注釈と注釈を付けた@Entity
クラスがあります。通常、これは問題にはなりませんが、これらの注釈を持つフィールドのエンティティークラスは複合キーを使用します。これは私が予想していたよりも多くの問題を引き起こしています。@Id
@OneToOne
問題を引き起こしているエンティティクラスは次のとおりです。
@Entity
public class ReportDetails implements Serializable {
@Id
@OneToOne
private MachineLine machineLine;
}
そしてMachineLine
、これはIDとして使用されているエンティティクラスですReportDetails
。
@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;
}
}
スペースを節約するために、これらのクラス定義から余分なフィールドとゲッターとセッターを省略しました。
アプリケーションを実行しようとすると、次の例外が発生します。
java.lang.IllegalArgumentException: This class [class ReportDetails] does not define an IdClass
私が入れたとき@IdClass
に注釈をReportDetails
それ、私はで定義どんなクラスの個々のフィールドの定義が必要です@IdClass
のように、MachineLine
。ただし、データベースからエンティティが取得さMachineLine
れるたびにReportDetails
エンティティ全体が返されるように、これを回避しようとしています。
内に追加のフィールドを定義する必要なしにMachineLine
、のIDフィールドとして持つ方法はありReportDetails
ますReportDetails
か?
これは、JPAが「派生ID」と呼んでいるものです。あなたはこのようなことを試すかもしれません:
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;
// ...
}
}
機械:
@Entity
public class Machine {
@Id
private long id;
@OneToMany(mappedBy = "machine")
private List<MachineLine> lines;
// ...
}
派生IDは、セクション2.4.1のJPA 2.2仕様で(例を使用して)議論されています。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加