org.hibernate.MappingException:无法确定:java.util.Collection的类型,在表中

时间:2016-02-12 14:12:29

标签: java hibernate jpa oracle11g

我遇到的问题是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;
    }


}

2 个答案:

答案 0 :(得分:1)

将任何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;

答案 1 :(得分:0)

@Column
@ElementCollection(targetClass=String.class)
public Set<String> getOptions() {
    return options;
}

OR

@OneToMany(cascade=CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "....")
public Set<String> getOptions() {
    return options;
}