I had a legacy database with 2 tables and a relashionship:
- NOTA (PK 3 fields = CODFORNECEDOR/NUMERO/SERIE)
- ITEMNOTA (PK 1 field = CODITEMNOTA, FK not specified in the database, but fields CODFORNECEDOR/NUMERO/SERIE in this table)
Then, after some years, it were created fields:
- NOTA.CODNOTA: just a field, not defined like PK, but it is a auto-increment field in this table
- ITEMNOTA.CODNOTA: just a field, no FK explicity
Then, the entities became like these:
Quote:
@Entity
@SequenceGenerator(name = "CODNOTA_GEN", sequenceName = "CODNOTA_GEN")
@DynamicUpdate
public class Nota implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator="CODNOTA_GEN")
private Long codNota;
...
}
@Entity
@SequenceGenerator(name = "CODITNOTA_GEN", sequenceName = "CODITNOTA_GEN")
@DynamicUpdate
public class ItemNota implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO, generator="CODITNOTA_GEN")
private Long codItemNota;
@ManyToOne
@JoinColumn(name = "CODNOTA", nullable = true)
private Nota nota;
...
}
Using Hibernate 3.5.2 + GlassFish 3.1.1 in a web/enterprise app, it never had problems.
Then, after upgrade to Hibernate 4.2.2 in the last week, it started to arise this error
when deploying the webapp:
Quote:
org.hibernate.MappingException:
Foreign key (FK_f03om5m4q04bbs42wunpha4r7:ITEMNOTA [codNota]))
must have same number of columns as the referenced primary key
(NOTA [CODFORNECEDOR,NUMERONOTA,serie])
Sometimes, after 2/3 attempts to redeploy the webapp, all works fine, GlassFish deploys
and runs the webapp without problems.
What do I have to do to explain for Hibernate 4.2.2 that it must ignore the "oficial"
NOTA PK and just look at NOTA.CODNOTA x ITEMNOTA.CODNOTA to define the relationship?