我有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));
hibernate实体映射如下
@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字段。任何帮助表示赞赏!
答案 0 :(得分:0)
为sourceId和sourceType再添加2个@Basic字段修复了这个!