1

I have this TABLES:

1.- PARTICIPANTE

CREATE TABLE participante ( 
    id_participa    SMALLINT NOT NULL,
    nasi            INTEGER NOT NULL,
    PRIMARY KEY(id_participa,nasi),
    FOREIGN KEY(nasi) REFERENCES persona(nasi)
)

2.- PERSONA

CREATE TABLE persona ( 
    nasi        INTEGER NOT NULL,
    PRIMARY KEY(nasi)
)

3.- CITA

CREATE TABLE cita ( 
    id_participa    SMALLINT NOT NULL,
    nasi            INTEGER NOT NULL,
    idcita          INTEGER NOT NULL,
    PRIMARY KEY(id_participa,idcita,nasi),
    FOREIGN KEY(id_participa, nasi) REFERENCES participante(id_participa, nasi)
)

4.- FORMULARIO

CREATE TABLE formulario ( 
        id_participa        SMALLINT NOT NULL,
        nasi                INTEGER NOT NULL,
        idcita              INTEGER NOT NULL,
        PRIMARY KEY(id_participa,idcita,nasi),
        FOREIGN KEY(id_participa, nasi, idcita) REFERENCES cita(id_participa, nasi, idcita)
    )

(among other fields, I just show you the ones that are involved in the problem)

and here are the ENTITIES:

1.- PARTICIPANTE

@Entity
@Table(name = "participante")
public class Participante implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @Column(name = "id_participa")
    private Integer idParticipante;
    @Id
    @OneToOne
    @JoinColumn(name = "nasi")
    private Persona persona;

2.- PERSONA

@Entity
@Table(name = "persona")
public class Persona implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @Column(name = "nasi")
    private Integer nasi;

3.- CITA

@Entity
@Table(name = "cita")
public class Cita implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @Column(name = "idcita")
    private Integer idCita;
    @Id
    @ManyToOne
    @JoinColumns({
            @JoinColumn(name = "id_participa", referencedColumnName = "id_participa"),
            @JoinColumn(name = "nasi", referencedColumnName = "nasi") })
    private Participante participante;

4.- FORMULARIO

@Entity
@Table(name = "formulario")
public class Formulario implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @OneToOne
    @JoinColumns({
            @JoinColumn(name = "id_participa", referencedColumnName = "id_participa"),
            @JoinColumn(name = "nasi", referencedColumnName = "nasi"),
            @JoinColumn(name = "idcita", referencedColumnName = "idcita") })
    private Cita cita;

And when I try to start the server I got this exception:

Caused by: org.hibernate.MappingException: Unable to find column with logical name: nasi in participante
    at org.hibernate.cfg.Ejb3JoinColumn.checkReferencedColumnsType(Ejb3JoinColumn.java:575)
    at org.hibernate.cfg.BinderHelper.createSyntheticPropertyReference(BinderHelper.java:126)
    at org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:116)
    at org.hibernate.cfg.Configuration.processEndOfQueue(Configuration.java:1514)
    at org.hibernate.cfg.Configuration.processFkSecondPassInOrder(Configuration.java:1437)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1355)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1724)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1775)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:242)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:372)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:357)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)
    ... 73 more

EDIT: I just found out that if I remove the "referencedColumn" attribute from the annotations I get a different exception:

Caused by: org.hibernate.AnnotationException: A Foreign key refering es.myapp.modelo.datos.dominio.participante.Participante from es.myapp.modelo.datos.dominio.cita.Cita has the wrong number of column. should be 1
    at org.hibernate.cfg.annotations.TableBinder.bindFk(TableBinder.java:432)
    at org.hibernate.cfg.ToOneFkSecondPass.doSecondPass(ToOneFkSecondPass.java:117)
    at org.hibernate.cfg.Configuration.processEndOfQueue(Configuration.java:1514)
    at org.hibernate.cfg.Configuration.processFkSecondPassInOrder(Configuration.java:1437)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1355)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1724)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1775)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:242)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:372)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:357)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)
    ... 73 more
diminuta
  • 1,545
  • 8
  • 32
  • 55
  • I assume sicco_participante is an entity, which you haven't listed – Dave Richardson Nov 07 '12 at 16:32
  • What is `sicco_participante`? The error message indicates that object, but I can't find it in your code. – Johanna Nov 07 '12 at 16:33
  • Sorry, i renamed sicco_participante as participante to make it simpler, and forgot to rename it on the exception message... sorry... I edited the exception name too... sicco_participante is the same as participante... – diminuta Nov 07 '12 at 16:44
  • Small comment not really related to the question: When you use an ORM, try as much as you can to have simple, numerical keys for your entities, rather than composite keys. This will simplify your mappings considerably and is less error prone. From hibernate documentation: `You can also declare the identifier as a composite identifier. This allows access to legacy data with composite keys. Its use is strongly discouraged for anything else.` - http://docs.jboss.org/hibernate/orm/4.1/manual/en-US/html/ch05.html – Augusto Nov 08 '12 at 09:50
  • I'm not the one who decides the data model :( – diminuta Nov 08 '12 at 12:08

1 Answers1

1

If I create auxiliary PK classes it works...

For example:

@Embeddable
public class CitaPK implements Serializable{

    private static final long serialVersionUID = 1L;
    @ManyToOne
    @JoinColumns({
            @JoinColumn(name = "id_participa", referencedColumnName = "id_participa"),
            @JoinColumn(name = "nasi", referencedColumnName = "nasi") })
    private Participante participante;
    @Column(name = "idcita", columnDefinition="smallint")
    private Integer idCita;

And then in Cita class:

@Entity
@Table(name = "sicco_citas")
public class Cita implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    private CitaPK citaPK;

http://docs.jboss.org/hibernate/stable/annotations/reference/en/html/entity.html#d0e2177

diminuta
  • 1,545
  • 8
  • 32
  • 55