如何使用子类中的两个复合键和父

时间:2016-12-19 14:43:17

标签: java eclipse jpa eclipselink

我有以下错误说明

  

异常[EclipseLink-7220](Eclipse持久性服务 -   2.6.3.v20160428-59c81c5):org.eclipse.persistence.exceptions.ValidationException异常   描述:带注释元素的字段上的@JoinColumns [字段   specificationSubset]来自实体类[class   com.jlr.vista.business.rule.model.Rule]不完整。当源   实体类使用复合主键,必须是@JoinColumn   使用@JoinColumns为每个连接列指定。这两个名字   并且必须在每个元素中指定referencedColumnName元素   @JoinColumn。

我找到了很多关于这个错误的解决方案但是没有一个解决过这种问题,在每种情况下两个类都有相同数量的复合键,但在我的情况下,我在实体类中有一个PK,在子类中有一个FK但是那个子类还有一个PK,这使得它们成为复合键。

在这种情况下,应该如何实现@OneToOne映射? 我正在使用Eclipse链接版本2.6

Rule.java

 public class Rule
    {
        private static final long serialVersionUID = 1L;


        @Id
        @Column(name="RULE_ID")
        @GeneratedValue(strategy=GenerationType.SEQUENCE,generator="RUL_SEQ1")
        @SequenceGenerator(name="_RUL_SEQ1",sequenceName="RUL_SEQ1",allocationSize=1)
        protected Integer ruleId;

        @Column(name="PARENT_RULE_ID")
        protected Integer parentRuleId;

        @Column(name="RULE_TYPE_CODE")
        protected String ruleTypeCode;

        @Column(name="BRAND")
        protected String brand;

        @Column(name="RULE_DESCRIPTION")
        protected String ruleDesscription;

        @Column(name="RULE_REFERENCE")
        protected String ruleReference;

        @Column(name="CONSTRAINT_TYPE")
        protected String constraintType;

        @Column(name="ARCHIVE_STATUS")
        protected String archiveStatus;

        @ManyToMany(targetEntity=VistaRulePartner.class, fetch=FetchType.LAZY)
        @JoinColumns({
            @JoinColumn(name="RULE_ID", referencedColumnName="RULE_ID")
        })
        protected List<VistaRulePartner> partners;

        @OneToMany(targetEntity=OrderTypeSubset.class,fetch=FetchType.LAZY,cascade=CascadeType.ALL)
        @JoinColumn(name="RULE_ID", referencedColumnName="RULE_ID",insertable=false,updatable=false)
        protected List orderTypeSubsets;
                    @OneToOne(targetEntity=SpecificationSubset.class,cascade=CascadeType.ALL,fetch=FetchType.LAZY,optional=false)
        @JoinColumn(name="RULE_ID",referencedColumnName="RULE_ID",insertable=false,updatable=false, nullable=false)
private SpecificationSubset specificationSubset;
}

SpecificationSubset.java

@IdClass(SpecificationSubsetPK.class)
@Entity
@Table(name="RULE_SPECIFICATION")
@InstantiationCopyPolicy
@Cache( refreshOnlyIfNewer=true,
        coordinationType = CacheCoordinationType.SEND_NEW_OBJECTS_WITH_CHANGES)
public class SpecificationSubset extends ModelArchivable
{
    @Id
    @Column(name="RULE_ID")
    private Integer ruleId;

    @Id
    @Column(name="RULE_SPEC_ID")
    private Integer ruleSpecId = 1;

    @Column(name="SPEC_MARKET")
    private String specMarket;

    @Column(name="DESC_GROUP_ID")
    private String descGroupId;

    @OneToMany(targetEntity=FeatureSubset.class, fetch=FetchType.LAZY)
    @JoinColumns({
        @JoinColumn(name="RULE_ID", referencedColumnName="RULE_ID", insertable=false, updatable=false),
        @JoinColumn(name="RULE_SPEC_ID", referencedColumnName="RULE_SPEC_ID", insertable=false, updatable=false)
    })
    private List featureSubsets;

    @OneToOne(targetEntity=Rule.class, fetch=FetchType.LAZY)
    @JoinColumn(name="RULE_ID", referencedColumnName="RULE_ID")
    private Rule ruleHolder;
}

FeatureSubset.java

@Entity
@Table(name="RULE_FEATURE")
@NamedQuery(
        name="GetFeatureCodesForVehicleConfig",
        query="SELECT f FROM FeatureSubset f WHERE f.ruleId = :ruleId AND f.featureType = 'YEAR'")
@InstantiationCopyPolicy
@Cache( coordinationType = CacheCoordinationType.SEND_NEW_OBJECTS_WITH_CHANGES)
public class FeatureSubset
{
    @Id
    @Column(name="FEATURE_TYPE_NO")
    @GeneratedValue(strategy=GenerationType.SEQUENCE,generator="VISTA_RUF_SEQ1")
    @SequenceGenerator(name="RUF_SEQ1",sequenceName="RUF_SEQ1",allocationSize=1)
    private Integer featureTypeNo; //Sequence no for the vista_rule_feature table

    @Column(name="RULE_SPEC_ID")
    private Integer ruleSpecId = 1;

    @Column(name="RULE_ID")
    private Integer ruleId;

    @Column(name="FEATURE_TYPE_ID")
    private String featureType;

    @Column(name="FEATURE_CODE")
    private String featureCode;

    @OneToOne(targetEntity=SpecificationSubset.class, fetch=FetchType.LAZY)
    /*@JoinColumns({
        @JoinColumn(name="RULE_ID", referencedColumnName="RULE_ID"),
        @JoinColumn(name="RULE_SPEC_ID", referencedColumnName="RULE_SPEC_ID")
    })*/
    private SpecificationSubset specificationSubset;
}

1 个答案:

答案 0 :(得分:0)

您不需要在关系的两侧定义映射信息 - 一个就足够了,通常它是关联的所有者。

因此,由于您的protected void btnleft_Click(object sender, EventArgs e) { j = j - 4; k = k - 4; recomended(); } protected void btnright_Click(object sender, EventArgs e) { j = j + 4; k = k + 4; recomended(); } 包含到SpecificationSubset的映射信息,因此在Rule中您可以引用此关系:

Rule

您似乎也需要修复拥有方,因为您已经拥有一个名为@OneToOne(targetEntity=SpecificationSubset.class, cascade=CascadeType.ALL, fetch=FetchType.LAZY, optional=false, mappedBy="ruleHolder") // name of the property in an owning class private SpecificationSubset specificationSubset; 的@Column:

RULE_ID