Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements

1

Good Night. I have the following error in Hibernate:

  

org.hibernate.AnnotationException: Illegal attempt to map a non collection as a @OneToMany, @ManyToMany or @CollectionOfElements: bestroute.modelo.Peca.grupoPeca

I can not display on my screen .xhtml the information that the database. This data comes from two different tables in the database, two entities in my project. I have to display this data in a <p:dataTable>

Entity Peca , which contains the column "code" that I have to display:

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;

@Entity
public class Peca {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name="idPeca")
    private int idPeca;

    @Column(nullable = false, length = 50)
    private String codigo;

    @OneToMany
    @JoinColumn(name = "idGrupoPeca")
    private GrupoPeca grupoPeca;

    public GrupoPeca getGrupoPeca() {
        return grupoPeca;
    }

    public void setGrupoPeca(GrupoPeca grupoPeca) {
        this.grupoPeca = grupoPeca;
    }

    public int getIdPeca() {
        return idPeca;
    }

    public void setIdPeca(int idPeca) {
        this.idPeca = idPeca;
    }

    public String getCodigo() {
        return codigo;
    }

    public void setCodigo(String codigo) {
        this.codigo = codigo;
    }

}

Entity GrupoPecas that contains the information of Peca , what the part is:

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class GrupoPeca {

    @Id
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    @Column(name = "idGrupoPeca")
    private int idGrupoPeca;

    @Column(nullable=false, length=50)
    private String Descricao;

    public int getIdGrupoPeca() {
        return idGrupoPeca;
    }
    public void setIdGrupoPeca(int idGrupoPeca) {
        this.idGrupoPeca = idGrupoPeca;
    }
    public String getDescricao() {
        return Descricao;
    }
    public void setDescricao(String descricao) {
        Descricao = descricao;
    }

}

In my controller page MelhorRota.xhtml looks like this:

@ManagedBean
public class MelhorRotaController {

  public MelhorRotaController() {
    System.out.println("CHAMOU O CONSTRUTOR");
  }

  public List<Peca> getListaPeca() {
    return  new CRUDBestRoute().buscarTodos(Peca.class);
}

In my class that connects to the bank and pulls the information:

public List buscarTodos(Class clase) {
    // estabele a conexao
    Session sessao = HibernateDAO.getSessao();
    Criteria c = sessao.createCriteria(clase);
    return c.list();
}

In the HibernateDAO class that establishes the connection:

public class HibernateDAO {
   private static SessionFactory fabrica;
    static {

        Configuration cfg = new Configuration();
        // depois adicionar as demais entidades
        // apos ter feito o mapeamento
        cfg.addAnnotatedClass(Usuario.class );
        cfg.addAnnotatedClass(Prateleira.class );
        cfg.addAnnotatedClass(Celula.class );
        cfg.addAnnotatedClass(GrupoPeca.class );
        cfg.addAnnotatedClass(Funcionario.class );

        cfg.addAnnotatedClass(Peca.class );
        cfg.configure();
        // forma mais facil de criar uma fabri
        fabrica = cfg.buildSessionFactory();
        getSessao();
    }

    public static Session getSessao(){
        return fabrica.openSession();
    }
}

On my page MelhorRota.xhtml , I display the table like this:

<p:dataTable var="pecas"
    rows="9"
    paginator="true"            
    value="#{melhorRotaController.getListaPeca()}">
    <f:facet name="header">
         Itens adicionados
    </f:facet>          
    <p:column headerText="Código">
        <h:outputText value="#{pecas.codigo}" />
    </p:column>         
    <p:column headerText="Descrição">
        <h:outputText value="#{pecas.descricao}" />
    </p:column>
</p:dataTable>      

If you can help me, thank you.

    
asked by anonymous 01.10.2017 / 03:17

1 answer

1

Your problem is here:

    @OneToMany
    @JoinColumn(name = "idGrupoPeca")
    private GrupoPeca grupoPeca;

You should use this:

    @ManyToOne
    @JoinColumn(name = "idGrupoPeca")
    private GrupoPeca grupoPeca;

That is, it is to change the @OneToMany to @ManyToOne . See more about this in this other answer of mine .

    
01.10.2017 / 06:19