Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
905 views
in Technique[技术] by (71.8m points)

hibernate - AnnotationException: A Foreign key refering has the wrong number of column. should be 2

I am mapping my classes with the tables of my database, but when running a test, I get the following error:

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

these are my classes:

//    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

both have primary keys are embedded into classes.

//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

and this is my relational model database

enter image description here

anyone have any idea? of that does not work?

EDIT 30/11/2014 - RETURN TO LIST EMPTY ASSOCIATION this is my JUnit test

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

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

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

This is the method that returns me the association of provincias

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

and this I have in the database

enter image description here

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your class CiudadPK has two columns in it. You're only using @JoinColumn which is limited to a single column. You need to use @JoinColumns and list out both columns in the FK, e.g.

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

You'll likely have the other issue w/ Ciudad as well, please follow the pattern here to correct that one.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...