gpt4 book ai didi

hibernate - AnnotationException : A Foreign key refering has the wrong number of column. 应该是 2

转载 作者:行者123 更新时间:2023-12-03 13:46:05 34 4
gpt4 key购买 nike

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

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

这些是我的类(class):
//    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

任何人有任何想法?那个不起作用?

编辑 30/11/2014 - 返回列表空协会
这是我的 JUnit 测试
Region region = new Region();
region.setId(new RegionPK("RM","CHL"));

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

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

这是返回我省协会的方法
@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

最佳答案

您的类(class) 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;

您可能会遇到其他问题,包括 Ciudad同样,请按照此处的模式更正该模式。

关于hibernate - AnnotationException : A Foreign key refering has the wrong number of column. 应该是 2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27215607/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com