Java Web - Remove Method Caused by: org.hibernate.Mappingexception: Unknown Entity: java.lang.Integer

Asked

Viewed 98 times

0

Gent,and I’m trying to create a method in which I delete the database record. However, the following error is occurring:

Caused by: org.hibernate.MappingException: Unknown entity: java.lang.Integer

Code:

public void  delete(int user)  {
    Consultar consulta = new Consultar ();


    EntityManager deletar = consulta.getEntityManager();

    System.out.println("usuário deletado"+user);

        deletar.getTransaction().begin();
        deletar.remove(user);
        deletar.getTransaction().commit();
}

1 answer

1

user is a variable of the type int.

deletar is the name given to your EntityManager (bad name).

When you do that:

deletar.remove(user);

Are you asking the EntityManager to remove an integer from the database. This makes no sense. Remove from where? From which table?

The object you should pass to the method remove should be the User, Usuário or anything you have that holds the annotation @Entity.

  • I made this change but nothing happens. public void delete(User user) { Consult query = new Query (); //Long primaryKey = Long.parseLong(user); Entitymanager Entity = query.getEntityManager(); System.out.println("user deleted"+user); Entity.getTransaction(). Begin(); Entity.remove(user); Entity.getTransaction(). commit(); }

  • in deletar.remove you have to insert an entity and not its code, maybe you can use findById to find the entity and then delete it

Browser other questions tagged

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