外键的列数错误

时间:2013-12-12 10:11:34

标签: hibernate jpa

我无法弄清楚问题是什么。我希望有一个人可以帮助我。 我收到了这个例外:

Caused by: org.hibernate.AnnotationException: A Foreign key refering com.domain.LocaleJpaImpl from com.engine.i18n.domain.LanguageToLocaleJpaImpl has the wrong number of column. should be 2
at org.hibernate.cfg.annotations.TableBinder.bindFk(TableBinder.java:420) ~[hibernate-core-jar-4.1.8.Final.jar:4.1.8.Final]
    at org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:117) ~[hibernate-core-jar-4.1.8.Final.jar:4.1.8.Final]
    at org.hibernate.cfg.Configuration.processEndOfQueue(Configuration.java:1525) ~[hibernate-core-jar-4.1.8.Final.jar:4.1.8.Final]
    at org.hibernate.cfg.Configuration.processFkSecondPassInOrder(Configuration.java:1446) ~[hibernate-core-jar-4.1.8.Final.jar:4.1.8.Final]
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1351) ~[hibernate-core-jar-4.1.8.Final.jar:4.1.8.Final]
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1737) ~[hibernate-core-jar-4.1.8.Final.jar:4.1.8.Final]
    at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:94) ~[hibernate-entitymanager-jar-4.1.8.Final.jar:4.1.8.Final]
    at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:905) ~[hibernate-entitymanager-jar-4.1.8.Final.jar:4.1.8.Final]

我有两个实体,LocaleJpa和LanguageToLocaleJpa。 每个LanguageToLocaleJpa与两个不同的LocaleJpa有两个关系。 这两个关系受到两个外键的约束:

fk on LANGUAGE_TO_LOCALE table

LocaleJpa 具有以下代码:

    package com.engine.i18n.domain;

    import java.util.Date;
    import java.util.HashSet;
    import java.util.Set;

    import javax.persistence.Column;
    import javax.persistence.Entity;
    import javax.persistence.FetchType;
    import javax.persistence.GeneratedValue;
    import javax.persistence.GenerationType;
    import javax.persistence.Id;
    import javax.persistence.OneToMany;
    import javax.persistence.Table;

    import com.jpa.BaseEntityJpaSupport;

    @Entity
    @Table(name = "LOCALE")
    public class LocaleJpaImpl extends BaseEntityJpaSupport implements java.io.Serializable, LocaleJpa {

    private static final long serialVersionUID = -6405562403744775413L;

    @Id()   
    @GeneratedValue( strategy = GenerationType.AUTO)
    @Column(name="LOCALE_ID", unique=true, nullable=false)
    private int localeId;


    @Column(name="LANGUAGE_CODE", length=2)
    private String languageCode;

    @Column(name="COUNTRY_CODE", length=2)
    private String countryCode;

    @Column(name="VARIANT_CODE", length=10)
    private String variantCode;

    @Column(name="FLAG_ICON_URL", length=100)
    private String flagIconUrl;

    @OneToMany(fetch=FetchType.LAZY, mappedBy="localeByLocaleId")
    private Set <LanguageToLocaleJpaImpl> languageToLocalesForLocaleId = new HashSet <LanguageToLocaleJpaImpl> (0);

    @OneToMany(fetch=FetchType.LAZY, mappedBy="localeByLanguageId")
    private Set <LanguageToLocaleJpaImpl> languageToLocalesForLanguageId = new HashSet<LanguageToLocaleJpaImpl> (0);

    public LocaleJpaImpl() {
    }

   public LocaleJpaImpl(int localeId, Date createdDate) {
       this.createdDate = createdDate;
   }

   public LocaleJpaImpl(int localeId, Date createdDate, String languageCode, String countryCode, String variantCode, String flagIconUrl, Set <LanguageToLocaleJpaImpl> languageToLocalesForLocaleId,       Set <LanguageToLocaleJpaImpl> languageToLocalesForLanguageId) {
      this.createdDate = createdDate;
      this.languageCode = languageCode;
      this.countryCode = countryCode;
      this.variantCode = variantCode;
      this.flagIconUrl = flagIconUrl;
      this.languageToLocalesForLocaleId = languageToLocalesForLocaleId;
      this.languageToLocalesForLanguageId = languageToLocalesForLanguageId;
   }

    @Override
    public int getLocaleId() {
       return this.localeId;
    }

    @Override
    public void setLocaleId(int localeId) {
       this.localeId = localeId;
    }

    @Override
    public String getLanguageCode() {
       return this.languageCode;
    }

    @Override
    public void setLanguageCode(String languageCode) {
       this.languageCode = languageCode;
    }

    @Override
    public String getCountryCode() {
       return this.countryCode;
    }

    @Override
    public void setCountryCode(String countryCode) {
       this.countryCode = countryCode;
    }

    @Override
     public String getVariantCode() {
       return this.variantCode;
    }

    @Override
     public void setVariantCode(String variantCode) {
       this.variantCode = variantCode;
   }

    @Override
    public String getFlagIconUrl() {
       return this.flagIconUrl;
    }

    @Override
    public void setFlagIconUrl(String flagIconUrl) {
       this.flagIconUrl = flagIconUrl;
   }

    @Override
    public Set<LanguageToLocaleJpaImpl> getLanguageToLocalesForLocaleId() {
       return this.languageToLocalesForLocaleId;
   }

    @Override
    public void setLanguageToLocalesForLocaleId(Set <LanguageToLocaleJpaImpl> languageToLocalesForLocaleId) {
       this.languageToLocalesForLocaleId = languageToLocalesForLocaleId;
    }


    }

LanguageToLocaleJpaImpl 具有以下代码:

package com.engine.i18n.domain;

import java.io.Serializable;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.hibernate.annotations.Type;

import com.domain.LanguageToLocale;
import com.jpa.BaseEntityJpaSupport;

@Entity
@Table(name = "LANGUAGE_TO_LOCALE")
public class LanguageToLocaleJpaImpl extends BaseEntityJpaSupport implements Serializable, LanguageToLocale {


private static final long serialVersionUID = 1L;

@Id 
@Column(name="LANGUAGE_TO_LOCALE_ID", unique=true, nullable=false)
private int languageToLocaleId;

@ManyToOne(fetch=FetchType.LAZY, cascade=CascadeType.ALL )
    @JoinColumn(name="LOCALE_ID", nullable=false)
private LocaleJpaImpl countryLocale;

@ManyToOne(fetch=FetchType.LAZY, cascade=CascadeType.ALL)
@JoinColumn(name="LANGUAGE_ID", nullable=false)
private LocaleJpaImpl languageLocale;

@Column(name = "ACTIVE_FLAG")
@Type(type="yes_no")
private boolean active;

@Column(name = "DEFAULT_FLAG")
@Type(type="yes_no")
private boolean defaultFlag;

@Override
public int getLanguageToLocaleId() {
    return languageToLocaleId;
}

@Override
public void setLanguageToLocaleId(int languageToLocaleId) {
    this.languageToLocaleId = languageToLocaleId;
}

@Override
public LocaleJpaImpl getCountryLocale() {
    return countryLocale;
}

@Override
public void setCountryLocale(LocaleJpaImpl countryLocale) {
    this.countryLocale = countryLocale;
}

@Override
public LocaleJpaImpl getLanguageLocale() {
    return languageLocale;
}

@Override
public void setLanguageLocale(LocaleJpaImpl languageLocale) {
    this.languageLocale = languageLocale;
}


@Override
public int getLocaleId() {      
    return this.countryLocale.getLocaleId();
}

@Override
public void setLocaleId(int localeId) {
    throw new UnsupportedOperationException();
}

@Override
public int getLanguageId() {
    return this.languageLocale.getLocaleId();
}

@Override
public void setLanguageId(int languageId) {
    throw new UnsupportedOperationException();
}

@Override
public String toString() {
    StringBuilder builder = new StringBuilder(super.toString());

    builder.append("localeId: ").append(getLocaleId()).append("\n");
    builder.append("languageId: ").append(getLanguageId()).append("\n");
    builder.append("default: ").append(isActive()).append("\n");
    builder.append("active: ").append(isDefaultFlag()).append("\n");

    return builder.toString();
}

@Override
public boolean isActive() {
    return active;
}

@Override
public void setActive(boolean active) {
    this.active = active;
}

@Override
public boolean isDefaultFlag() {

    return this.defaultFlag;
}

@Override
public void setDefaultFlag(boolean defaultFlag) {
    this.defaultFlag = defaultFlag;
}

}

任何想法? 提前谢谢!

1 个答案:

答案 0 :(得分:4)

实体映射错误: 在LocaleJpa中,您与LanguageToLocaleJpa实体具有相反的关系,您需要使用java属性名称而不是DB列名称。你也可以复制/粘贴它们。

应该是:

@OneToMany(fetch=FetchType.LAZY, mappedBy="countryLocale")
private Set <LanguageToLocaleJpaImpl> countryToLocalesForLocaleId = new HashSet <LanguageToLocaleJpaImpl> (0);

@OneToMany(fetch=FetchType.LAZY, mappedBy="languageLocale")
private Set <LanguageToLocaleJpaImpl> languageToLocalesForLanguageId = new HashSet<LanguageToLocaleJpaImpl> (0);