简体   繁体   中英

Hibernate: a foreign key has the wrong number of column. should be 1

I'm trying to map to entities Report and ReportLookup:

@Entity
public class Report extends AbstractMigrationObject implements Serializable {
    @JsonIgnore
    @Id
    private Long reportId;
    private String reportName;
    private String appName;
    private Integer reportNum;
    private String issue;
    @JsonProperty
    @OneToMany(fetch = FetchType.EAGER)
    @JoinColumns({
            @JoinColumn(name = "reportName"),
            @JoinColumn(name = "appName"),
    })
    private Set<ReportLookup> lookupSet;

@Entity
public class ReportLookup implements Serializable {
    @JsonIgnore
    @Id
    private Long reportLookupId;
    private String parameterName;
    private String attributeName;
    private String lookupName;
    private Integer sequence;
    private String labelOverride;
    private String defaultValue;
    private Integer required;
    private Integer hidden;
    private String reportName;
    private String operator;
    private Integer multiLookup;
    private Integer reportNum;
    private String appName;
    @ManyToOne
    @JoinColumns({
            @JoinColumn(name = "appName", insertable = false, updatable = false),
            @JoinColumn(name = "reportName", insertable = false, updatable = false)
    })
    private Report report;

I don't know how to map composite foreign key. Always get this error:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [persistance-context.xml]: Invocation of init method failed; nested exception is org.hibernate.AnnotationException: A Foreign key refering ru.ocrv.ekasui.changemonitoring.maximo.entity.report.Report from ru.ocrv.ekasui.changemonitoring.maximo.entity.report.ReportLookup has the wrong number of column. should be 1

I am not sure but you should try to change your @JoinColumns structure with the two Ids of your Class

    @JoinColumns({
        @JoinColumn(name = "reportId", insertable = false, updatable = false),
        @JoinColumn(name = "reportLookupId", insertable = false, updatable = false)
})

Sorry but you can't add two columns with the same name, if you want to add a reportId in ReportLookup Table and ReportLookup in the Report Table delete the second @JoinColumn , in ReportLookup :

@ManyToOne
@JoinColumns({
        @JoinColumn(name = "appName", insertable = false, updatable = false)
})
private Report report; 

and in Report:

@OneToMany(fetch = FetchType.EAGER)
@JoinColumns({
        @JoinColumn(name = "reportName")
})
private Set<ReportLookup> lookupSet;

Second Solution you should add @JoinColumns in the principal entity

@JsonProperty
@OneToMany(fetch = FetchType.EAGER)
@JoinColumns({
        @JoinColumn(name = "reportName"),
        @JoinColumn(name = "appName"),
})
private Set<ReportLookup> lookupSet;

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