我有一个问题,就是Hibernate无法在表Region上确定Collection的类型。我正在尝试通过一对多关系创建表Actels的外键。一个区域可以有很多actels。
详细:
我得到的错误是这样的:
Caused by: org.hibernate.MappingException: Could not determine type for: java.util.Collection, at table: Region, for columns: [org.hibernate.mapping.Column(collection_actels)]
at org.hibernate.mapping.SimpleValue.getType(SimpleValue.java:316)
at org.hibernate.mapping.SimpleValue.isValid(SimpleValue.java:294)
at org.hibernate.mapping.Property.isValid(Property.java:238)
at org.hibernate.mapping.PersistentClass.validate(PersistentClass.java:469)
at org.hibernate.mapping.RootClass.validate(RootClass.java:270)
at org.hibernate.cfg.Configuration.validate(Configuration.java:1294)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1742)
at org.hibernate.ejb.EntityManagerFactoryImpl.<init>(EntityManagerFactoryImpl.java:94)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:905)
... 20 more
Region.java:
package com.springJPA.domain;
import java.io.Serializable;
import java.lang.String;
import java.util.Collection;
import javax.persistence.*;
import com.springJPA.domain.Actels;
/**
* Entity implementation class for Entity: Reseau
*
*/
@Entity
public class Region implements Serializable {
private int id_region;
private String region;
private String ville;
private int codep;
private int num_region;
private int num_ville;
private Collection<Actels> collection_actels;
private static final long serialVersionUID = 1L;
private Collection<Actels> actels;
public Region() {
super();
}
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator = "id_Sequence_region")
@SequenceGenerator(name = "id_Sequence_region", sequenceName = "ID_SEQ_REGION")
public int getId_region() {
return id_region;
}
public void setId_region(int id_region) {
this.id_region = id_region;
}
public String getRegion() {
return this.region;
}
public void setRegion(String region) {
this.region = region;
}
public String getVille() {
return this.ville;
}
public void setVille(String ville) {
this.ville = ville;
}
public int getCodep() {
return this.codep;
}
public void setCodep(int codep) {
this.codep = codep;
}
public Collection<Actels> getCollection_actels() {
return collection_actels;
}
public void setCollection_actels(Collection<Actels> collection_actels) {
this.collection_actels = collection_actels;
}
public int getNum_region() {
return num_region;
}
public void setNum_region(int num_region) {
this.num_region = num_region;
}
public int getNum_ville() {
return num_ville;
}
public void setNum_ville(int num_ville) {
this.num_ville = num_ville;
}
@OneToMany
@JoinColumn(name = "Region_id_region", referencedColumnName = "id_region")
public Collection<Actels> getActels() {
return actels;
}
public void setActels(Collection<Actels> param) {
this.actels = param;
}
}
Actels.java:
package com.springJPA.domain;
import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.SequenceGenerator;
/**
* Entity implementation class for Entity: Actels
*
*/
@Entity
public class Actels implements Serializable {
private int id_actels;
private String nomActels;
private int num_actel;
private Region region;
private static final long serialVersionUID = 1L;
public Actels() {
super();
}
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator = "id_Sequence_actels")
@SequenceGenerator(name = "id_Sequence_actels", sequenceName = "ID_SEQ_ACTELS")
public int getId_actels() {
return id_actels;
}
public void setId_actels(int id_actels) {
this.id_actels = id_actels;
}
public String getNomActels() {
return this.nomActels;
}
public void setNomActels(String nomActels) {
this.nomActels = nomActels;
}
public Region getRegion() {
return region;
}
public void setRegion(Region region) {
this.region = region;
}
public int getNum_actel() {
return num_actel;
}
public void setNum_actel(int num_actel) {
this.num_actel = num_actel;
}
}
最佳答案
在每个字段上方放置任何JPA annotation
而不是getter
属性:
@SequenceGenerator(name = "id_Sequence_actels", sequenceName = "ID_SEQ_ACTELS")
private int id_actels;
和:
@JoinColumn(name = "Region_id_region", referencedColumnName = "id_region")
private Collection<Actels> actels;