java.lang.IllegalArgumentException:未知实体:Jpa
java.lang.IllegalArgumentException: Unknown entity: Jpa
我想通过他id
删除一个人,但是报错:
Exception in thread "main" java.lang.IllegalArgumentException: Unknown
entity: org.hibernate.query.internal.QueryImpl
在数据库工作中查找实体的方法:
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();
}
人
@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-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>
正如消息所说,remove 想要删除一个 Entity,因此您需要一个持久化的 Person 来执行此操作。
与你相处的人
Person person = em.find(Person.class,id);
您使用
执行更新或删除查询
query.executeUpdate();
我想通过他id
删除一个人,但是报错:
Exception in thread "main" java.lang.IllegalArgumentException: Unknown entity: org.hibernate.query.internal.QueryImpl
在数据库工作中查找实体的方法:
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();
}
人
@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-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>
正如消息所说,remove 想要删除一个 Entity,因此您需要一个持久化的 Person 来执行此操作。 与你相处的人
Person person = em.find(Person.class,id);
您使用
执行更新或删除查询query.executeUpdate();