Hibernate映射中的错误“外键引用的列数错误。应为2”

时间:2014-08-28 06:30:44

标签: java spring hibernate-mapping spring-data-jpa composite-primary-key

具有复合主键TRANSFORMATIONtrans_id

versionEXPRESSION表仅包含来自trans_id的外键引用,但没有version列。

当我尝试保存到TRANSFORMATION时,通过JUnit,我得到以下错误。

caused by: org.hibernate.AnnotationException: A Foreign key refering org.persistence.entity.Transformation from org.persistence.entity.Expression has the wrong number of column. should be 2
            at org.hibernate.cfg.annotations.TableBinder.bindFk(TableBinder.java:420) ~[hibernate-core-4.2.8.Final.jar:4.2.8.Final]
            at org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:117) ~[hibernate-core-4.2.8.Final.jar:4.2.8.Final]
            at org.hibernate.cfg.Configuration.processEndOfQueue(Configuration.java:1560) ~[hibernate-core-4.2.8.Final.jar:4.2.8.Final]
            at org.hibernate.cfg.Configuration.processFkSecondPassInOrder(Configuration.java:1481) ~[hibernate-core-4.2.8.Final.jar:4.2.8.Final]
            ...
            at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:250) ~[spring-test-4.0.2.RELEASE.jar:4.0.2.RELEASE]
            at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContextInternal(CacheAwareContextLoaderDelegate.java:64) ~[spring-test-4.0.2.RELEASE.jar:4.0.2.RELEASE]
            at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:91) ~[spring-test-4.0.2.RELEASE.jar:4.0.2.RELEASE]
            ... 25 more

@Entity(name="Transformation")
@Table(name="Transformation")
public class Transformation extends BaseEntity implements Serializable{


    private static final long serialVersionUID = 1L;

    @EmbeddedId 
    private TransformationPK id;
          /*@OneToMany(mappedBy="transformation")
    private Set<Expression> expressions;*/

          @OneToMany
    @JoinColumn(name="TRANS_ID", nullable = false)
    @Fetch(FetchMode.SUBSELECT)
    private Set<Expression> expressions;
}

@Embeddable
public class TransformationPK implements Serializable{

    private static final long serialVersionUID = 1L;

    @Column(name="TRANS_ID")
    private String transId;

    @Column(name="VERSION", precision = 2, scale = 1)
    private Double transVersion;

    public TransformationPK() {
    }
}


@Entity(name="EXPRESSION")
@Table(name="EXPRESSION")
public class Expression extends BaseEntity implements Serializable{


    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "Expression_ID")
    @GeneratedValue(strategy=GenerationType.AUTO)
    private String expressionId;

    @Column(name="TRANS_ID")
    private String transId;

    /*@ManyToOne
    @JoinColumns ({
           @JoinColumn(name = "TRANS_ID", insertable = false, updatable = false),
           @JoinColumn(name = "VERSION", insertable = false, updatable = false)
          })
    private Transformation transformation;*/

    @ManyToOne
          @JoinColumn(name = "TRANS_ID", nullable = false, insertable = false, updatable = false)
    private Transformation transformation;
}

任何人都可以建议如何解决此问题。

1 个答案:

答案 0 :(得分:3)

由于您的嵌入式PK有两列,此处您还应该有两个连接列:

@ManyToOne
@JoinColumns({  
@JoinColumn(name = "TRANS_ID", nullable = false, insertable = false, updatable = false),
@JoinColumn(name = "VERSION", ....
})
private Transformation transformation;

顺便说一句,你有双向关联Transformation-Expression,你应该在一端添加mappedBy。