Error in hibernation mapping "Foreign key with wrong column number must have 2"

TRANSFORMATION

a table that has a composite primary key trans_id

and version

.
EXPRESSION

a table that has a foreign key reference only from trans_id

, it has no column version

.

Through JUnit, when I try to save in TRANSFORMATION

, I get 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 solve this.

+3


source to share


1 answer


Since your inline PK has two columns, there should be two columns here as well:

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

      



By the way, you have a Transformation-Expression bi-directional association, you have to add mappedBy at one end.

+3


source







All Articles