JPA Error inserting a product object that has reference to an existing object

0

I'm having a doubt in the manytomany annotation, I know the same is meant to indicate to white that it needs to create a broker table and that in turn is located above an element that will be list

public class Produto {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;
private float preco;
private String nome;
private int qtd;
@ManyToMany
private List<Fornecedor> fornecedor;
@ManyToOne

private Category category; The idea is that I can create many suppliers on this list for example; Product chocolate, supplier toddy and nescau, in this product I was able to add two of the suppliers in this list I thought of my RN class (type a DAO ops working in packages)

public Produto inserir(Produto produto,List<Fornecedor> listaFornecedores ,Categoria categoria) { 
            listaFornecedores=new ArrayList<>();
            Conexao con = new Conexao();
            EntityManager em = con.getEntidade();
            em.getTransaction().begin();
            for(Fornecedor fornecedor : listaFornecedores) {
                em.merge(fornecedor);
            }
            em.merge(categoria);
            em.persist(produto); 
            em.getTransaction().commit();
            em.close();
            return produto;
        }

This method receives a list and sweeps the same to be able to use the merge in all objects already created the problem that at the time of testing I have to create a new object and persist, I tried using find to capture the existing objects but of the error, it must be by having one transation inside another

public static void main(String[] args) {


    Conexao con = new Conexao();
    EntityManager em = con.getEntidade();
    em.getTransaction().begin();
    ProdutoRN produtoRN =new ProdutoRN();
    Fornecedor forn = em.find(Fornecedor.class,1);
    List<Fornecedor>listaFornecedores=new ArrayList<>();
    listaFornecedores.add(forn);
    Categoria cat = em.find(Categoria.class,1);

    Produto produto = new Produto(200,"Achocolatado",20 ,listaFornecedores,cat);

    produtoRN.inserir(produto, listaFornecedores, cat);

    em.getTransaction().commit();           

em.close();

// Instantiate the product

} ERROR:

abr 24, 2018 7:50:13 PM org.hibernate.dialect.Dialect INFO: HHH000400: Using dialect: org.hibernate.dialect.MySQL5InnoDBDialect Exception in thread "main" java.lang.ExceptionInInitializerError at sistemavendas.TesteProduto.main(TesteProduto.java:22) Caused by: org.hibernate.AnnotationException: No identifier specified for entity: sistemavendas.entidade.Compra at org.hibernate.cfg.InheritanceState.determineDefaultAccessType(InheritanceState.java:266)
    
asked by anonymous 25.04.2018 / 12:38

1 answer

2

By analyzing only the stacktrace posted, we notice that the problem is in the mapping of the Compra entity, as shown in the following excerpt:

Caused by: org.hibernate.AnnotationException: No identifier specified for entity: sistemavendas.entidade.Compra

Following the message from Exception posted, the attribute that will receive annotation @Id on that entity is missing.

Add the annotation to the attribute that corresponds to the id of the table in the database and this Exception will disappear.

    
25.04.2018 / 12:50