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

Asked

Viewed 95 times

0

I am doubtful in the note manytomany ,I know that it serves to indicate to white that he needs to create an intermediate table and that in turn is located on top of 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 in this list for example; Chocolate product, Toddy supplier and Nescau,in this product I managed to add two suppliers in this list thought in my product class)

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;
        }

With this method receive a list and scanning 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,must be by possessing a transation within 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();

//instance 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)

1 answer

2


Analyzing only the stacktrace posted, it is perceived that the problem lies in the mapping of the entity Compra, as shown in the following excerpt:

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

Following the message of Exception launched, it is missing to define the attribute that will receive the Annotation @Id in that entity.

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

Browser other questions tagged

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