次のようなコンテナーとコンテナーの関係を表す 3 つのテーブルがあります。
create table Containee(id int, name varchar(100), primary key(id));
create table Container(id int, name varchar(100), sourceId int, sourceType varchar(100), primary key(id));
create table JoinTable(containeeId int, resourceId int, resourceType varchar(100), primary key(containeeId, resourceId, resourceType));
休止状態のエンティティは次のようにマッピングされます
@Entity
public class Containee {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Basic
private String name;
}
@Entity
public class Container implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int id;
@Basic
private String name;
@Embedded
@AttributeOverrides({
@AttributeOverride(name = "id", column = @Column(name = "sourceId")),
@AttributeOverride(name = "type", column = @Column(name = "sourceType"))
})
private DomainObject domainObject;
@OneToMany(fetch = FetchType.LAZY)
@JoinTable(name = "JoinTable",
joinColumns = {
@JoinColumn(name="resourceId", referencedColumnName = "sourceId"),
@JoinColumn(name="resourceType", referencedColumnName = "sourceType")
},
inverseJoinColumns = @JoinColumn(name = "containeeId")
)
private Collection<Containee> containees;
}
埋め込みクラスは次のように宣言されます
@Embeddable
public class DomainObject {
private int id;
private String type;
public int getId() {
return id;
}
public String getType() {
return type;
}
}
上記のコードは機能せず、次のエラーが発生します。
単一のプロパティにマップされていないコンテナを参照するコンテナの referencedColumnNames(sourceId, sourceType)。
ただし、@Embedded domainObject フィールドを削除し、2 つの @Basic sourceId および sourceType に置き換えると、同じコードが魅力的に機能します。私は多くのことを試しましたが、 @Embedded フィールドでも何も機能しないようです。どんな助けでも大歓迎です!