简体   繁体   中英

Error in Hibernate Mapping “A Foreign key refering has the wrong number of column. should be 2”

TRANSFORMATION table having a composite primary key trans_id and version .
EXPRESSION table having foreign key reference from trans_id only, it doesn't have a version column.

Through JUnit when I try to save into TRANSFORMATION , I am getting below error.

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;
}

Can anyone suggest how to resolve this.

As your embedded PK has two columns, here you should have two joining columns as well:

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

BTW, you have bidirectional association Transformation-Expression, you should add mappedBy at one end.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM