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
这是我的关系模型数据库
anyone have any idea? of that does not work?
有谁有任何想法?这不起作用?
EDIT 30/11/2014 - RETURN TO LIST EMPTY ASSOCIATION this is my JUnit test
编辑30/11/2014 - 返回清单EMPTY ASSOCIATION这是我的JUnit测试
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
这是返回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
这我在数据库中
1 个解决方案
#1
19
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.
您的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;
You'll likely have the other issue w/ Ciudad
as well, please follow the pattern here to correct that one.
你可能还有其他问题w / Ciudad,请按照这里的模式纠正那个。
#1
19
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.
您的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;
You'll likely have the other issue w/ Ciudad
as well, please follow the pattern here to correct that one.
你可能还有其他问题w / Ciudad,请按照这里的模式纠正那个。