【发布时间】:2016-02-20 05:05:08
【问题描述】:
我在使用 JPA 1.0 注释保持上述关系时遇到问题。
我可以使用以下完美的 XML 映射它:
<class name="Exposure" table="Exposures">
<id name="id" column="Id"/>
<property name="name" column="name" not-null="true"/>
<set name="rules" table="ExposureRules" lazy="true" cascade="all, delete-orphan">
<key column="TypeId"/>
<one-to-many class="ExposureRule"/>
</set>
</class>
<class name="ExposureRule" table="ExposureRules">
<id name="id" column="Id"/>
<property name="inclusive" column="Inclusive" not-null="true"/>
<map name="attributes" table="ExposureRuleAttributes" lazy="true" cascade="all">
<key column="RuleId"/>
<index column="Attribute" type="string"/>
<element column="Value" type="string"/>
</map>
</class>
当我尝试使用注释将其持久化时,我收到以下错误:
Foreign key (FKA635AE7A4160E727:ExposureRuleAttributes [RuleId])) must have same number of
columns as the referenced primary key (ExposureRules [TypeId,rules_id])
我认为问题在于我如何在 ExposureRules 类中注释了 attributes 映射,但我想不出正确的方法?
暴露等级:
@Entity
@Table(name="Exposures")
public class Exposure {
@Id
private int id;
@Column(nullable=false)
private String name;
@OneToMany
@Cascade({CascadeType.ALL, CascadeType.DELETE_ORPHAN})
@JoinTable(name="ExposureRules", joinColumns={@JoinColumn(name="TypeId")})
private Set<ExposureRule> rules;
ExposureRule 类:
@Entity
@Table(name="ExposureRules")
public class ExposureRule {
@Id
private int id;
@Column(nullable=false)
private int inclusive;
@CollectionOfElements(fetch=FetchType.LAZY)
@Cascade(CascadeType.ALL)
@JoinTable(name="ExposureRuleAttributes",joinColumns=@JoinColumn(name="RuleId"))
@IndexColumn(name="Attribute")
@Column(name="Value")
private Map<String, String> attributes = new HashMap<String, String>();
【问题讨论】:
-
我删除了 Exposure 类作为测试,它可以工作。所以看起来这是我在 Exposure 类中设置“规则”的映射一定是问题所在。
标签: hibernate jpa collections annotations mapping