我有 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;
}
}
上面的代码不起作用,我收到以下错误:
引用 Container 的容器的 referencedColumnNames(sourceId, sourceType) 未映射到单个属性。
但是,如果我删除 @Embedded domainObject 字段并将其替换为 2 个 @Basic sourceId 和 sourceType,则相同的代码就像一个魅力。我已经尝试了很多东西,但似乎也没有任何东西适用于 @Embedded 字段。任何帮助表示赞赏!