1

I want to remove a person by his id, but gives an error:

Exception in thread "main" java.lang.IllegalArgumentException: Unknown entity: org.hibernate.query.internal.QueryImpl

Methods for finding the entity in the database work:

   public class MySQLPersonDAO implements PersonDAO {
     @Override
        public void deleteById(int id) {
            EntityManager em = FACTORY.createEntityManager();
            EntityTransaction transaction = em.getTransaction();
            transaction.begin();
            TypedQuery query = em.createQuery("SELECT p FROM Person p WHERE p.id = :id", Person.class);
            query.setParameter("id", id);
            em.remove(query); // hier mistake
            transaction.commit();
            em.close();
        }

Person

@Entity
public class Person {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "person_id")
    private int id;

    private String name;

    private String password;
//getters,setters,constructor

persistence Do I need to explicitly specify all entities?

    <persistence-unit name="myjpa">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <class> com.evghenii.domain.Person</class>
        <properties>
            <property name="hibernate.connection.driver_class" value="com.mysql.cj.jdbc.Driver"/>
            <property name="hibernate.connection.password" value="root"/>
            <property name="hibernate.connection.username" value="root"/>
            <property name="hibernate.connection.url" value="jdbc:mysql://localhost:3306/board_jpa?serverTimezone=Europe/Berlin"/>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL8Dialect"/>
            <!--<property name="hibernate.hbm2ddl.auto" value="create"/>-->
            <property name="hibernate.hbm2ddl.auto" value="update"/>
            <property name="hibernate.show_sql" value="true"/>
        </properties>
    </persistence-unit>
</persistence>

maven

    <dependencies>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>5.4.11.Final</version>
        </dependency>

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.19</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>5.4.12.Final</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>6.0.17.Final</version>
        </dependency>       
    </dependencies>

</project>
2
  • 1
    The remove method of the entity manager expects you to pass an entity as an argument, not a query. Which is pretty much the error message you are getting. Commented Mar 5, 2020 at 12:17
  • 1
    You need to remove Person instead of TypedQuery. Commented Mar 5, 2020 at 12:41

1 Answer 1

2

As the Message says, remove wants to remove an Entity, so you need a persisted Person to do this. A Person you get with

Person person = em.find(Person.class,id);

You execute an update or delete Query with

query.executeUpdate();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.