Illegal Attempt to map a non Collection as a @Onetomany, @Manytomany or @Collectionofelements

Asked

Viewed 3,293 times

1

Good night. I have the following mistake in Hibernate:

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

I can’t 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, that contains the "code" column 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 which contains the information of Peca, what the piece 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;
    }

}

On my page controller MelhorRota.xhtml that’s how it is:

@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 class HibernateDAO establishing 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, I’d appreciate it.

  • A groupPeca may have only one piece?

  • Yes, one piece for having only one groupPeca, but one groupPeca can have several pieces.

  • Read here to learn how to accept an answer.

1 answer

1


Your problem is here:

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

You should wear this:

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

I mean, it’s to change the @OneToMany for @ManyToOne. See more about this in this other answer of mine.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.