AnnotationException:外键引用的列数错误。应该是2

时间:2014-11-30 16:46:37

标签: hibernate jpa orm persistence hibernate-mapping

我将我的类映射到我的数据库表,但是在运行测试时,我收到以下错误:

Caused by: org.hibernate.AnnotationException: A Foreign key refering com.modulos.pvs.acceso.datos.entity.Provincia from com.modulos.pvs.acceso.datos.entity.Ciudad has the wrong number of column. should be 2
    at org.hibernate.cfg.annotations.TableBinder.bindFk(TableBinder.java:429)
    at org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:115)
    at org.hibernate.cfg.Configuration.processEndOfQueue(Configuration.java:1550)
    at org.hibernate.cfg.Configuration.processFkSecondPassInOrder(Configuration.java:1473)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1389)
    at org.hibernate.cfg.Configuration.buildMappings(Configuration.java:1345)
    at org.springframework.orm.hibernate3.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:717)
    at org.springframework.orm.hibernate3.AbstractSessionFactoryBean.afterPropertiesSet(AbstractSessionFactoryBean.java:211)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1541)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1479)
    ... 40 more

这些是我的课程:

//    PROVINCIA TABLE
    @Entity
    @Table(name="provincia")
    @NamedQuery(name = "Provincia.findAll", query = "SELECT p FROM Provincia p")
    public class Provincia implements Serializable {

        private static final long serialVersionUID = 1L;

        @EmbeddedId
        private ProvinciaPK id;

        private String nombreProvincia;

        // bi-directional many-to-one association to Region
        @ManyToOne(fetch=FetchType.LAZY)
        @JoinColumn(name = "codRegion")
        private Region region;

        //bi-directional many-to-one association to Ciudad
        @OneToMany(mappedBy="provincia", fetch=FetchType.LAZY)
        private List<Ciudad> ciudad;

    //GETTER AND SETTER

//TABLE CIUDAD
/**
 * The persistent class for the city database table.
 * 
 */
@Entity
@Table(name="ciudad")
@NamedQuery(name = "Ciudad.findAll", query = "SELECT c FROM Ciudad c")
public class Ciudad implements Serializable {
    private static final long serialVersionUID = 1L;

    @EmbeddedId
    private CiudadPK id;

    private String nombreCiudad;

    // bi-directional many-to-one association to Provincia
    @ManyToOne(fetch=FetchType.LAZY)
    @JoinColumn(name = "codProvincia", insertable = false, updatable = false)
    private Provincia provincia;

    //bi-directional many-to-one association to Direcciones
    @OneToMany(mappedBy="ciudad", fetch=FetchType.LAZY)
    private List<Direcciones> direcciones;

//  GETTER AND SETTER

两者都将主键嵌入到类中。

//PROVINCIA PK
/**
 * The primary key class for the provincia database table.
 * 
 */
@Embeddable
public class ProvinciaPK implements Serializable {
    //default serial version id, required for serializable classes.
    private static final long serialVersionUID = 1L;

    private String codProvincia;

    private String codRegion;

    public ProvinciaPK() {
    }
    /getter and setter

//ciudad pk
/**
 * The primary key class for the region database table.
 * 
 */
@Embeddable
public class CiudadPK implements Serializable {
    //default serial version id, required for serializable classes.
    private static final long serialVersionUID = 1L;

    private String codCiudad;

    private String codProvincia;

    public CiudadPK() {
    }
//GETTER AND SETTER

这是我的关系模型数据库

enter image description here

谁有任何想法?这不起作用?

EDIT 30/11/2014 - 返回列出空闲协会 这是我的JUnit测试

Region region = new Region();
            region.setId(new RegionPK("RM","CHL"));

            region.setProvincias(regionDAO.getProvinciaRegion(region));

            System.out.println(region.getProvincias());

这是返回provincias

的关联的方法
@Transactional(readOnly=true)
@Override
public List<Provincia> getProvinciaRegion(Region region) {
    region = getRegionById(region);
    Hibernate.initialize(region.getProvincias());
    return region.getProvincias();
}

我在数据库中有这个

enter image description here

enter image description here

2 个答案:

答案 0 :(得分:27)

您的班级CiudadPK中有两列。您只使用@JoinColumn,但仅限于一列。您需要使用@JoinColumns并列出FK中的两列,例如

// bi-directional many-to-one association to Provincia
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumns({
  @JoinColumn(name = "codProvincia", insertable = false, updatable = false),
  @JoinColumn(name = "codRegion", insertable = false, updatable = false)
})
private Provincia provincia;

您可能还有其他问题w / Ciudad,请按照此处的模式更正该问题。

答案 1 :(得分:4)

从Java 8开始(引入@Repeatable),不再需要包装注释@JoinColumns

因此,您可以简单地执行以下操作:

// bi-directional many-to-one association to Provincia
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name = "codProvincia", insertable = false, updatable = false)
@JoinColumn(name = "codRegion", insertable = false, updatable = false)
private Provincia provincia;

参考:

Java 8's new Type Annotations

Repeating Annotations