简体   繁体   English

在 MariaDB 上使用 Hibernate 的 JPQL 更新查询出错

[英]Error in JPQL Update Query using Hibernate on MariaDB

Hi你好

I am trying to update a row in MariaDB database table using hibernate.我正在尝试使用休眠更新 MariaDB 数据库表中的一行。 It is like this :是这样的:

    public static void updateJPQL() {
        EntityManager entmngr = JPAProvider.getEntityManager();
        EntityTransaction transaction = entmngr.getTransaction();
        transaction.begin();
        Query query = entmngr.createQuery("UPDATE users o set o.name = 'John' where o.id =: NID");
        query.setParameter("NID", 2L);

        query.executeUpdate();


        transaction.commit();
        entmngr.close();
    }

Hibernate gives this error : Hibernate 给出了这个错误:

Exception in thread "main" javax.persistence.PersistenceException: org.hibernate.sql.ast.SqlTreeCreationException: Could not locate TableGroup - NavigablePath[model.entity.Users(o)]

the other queries would execute fine.其他查询会执行得很好。 I could insert into my table, I could fetch all of the records but this update query is the problem.我可以插入到我的表中,我可以获取所有记录,但是这个更新查询是问题所在。

Several things to check and note:需要检查和注意的几件事:

  • The table name used in the UPDATE query should refer to the mapped Java class name rather than the database table name and it is case-sensitive. UPDATE 查询中使用的表名应该引用映射的 Java 类名而不是数据库表名,并且区分大小写。 So does the users in the query is the table name as we seldom name the Java class starting with the lower case ?那么查询中的users是否是表名,因为我们很少以小写开头命名Java类? If yes , please change it to the mapped Java class name.如果是,请将其更改为映射的 Java 类名。 (The same applied to the column too , refer them using java object field name rather than the actual table columns name) (同样适用于列,使用 java 对象字段名称而不是实际的表列名称来引用它们)

  • The point of using ORM is that we don't want to manually write SQL to update the DB record.使用 ORM 的重点是我们不想手动编写 SQL 来更新 DB 记录。 We want to treat the DB record as an Object such as we can use the OOP technique to implement the update logic.我们希望将数据库记录视为一个对象,例如我们可以使用 OOP 技术来实现更新逻辑。 so through changing the state of the object , JPA will automatically generate the SQL to update the corresponding DB records rather than writing it by ourself.所以通过改变对象的状态,JPA会自动生成SQL来更新对应的DB记录,而不是我们自己写。 So the correct way to update the record using JPA is to get the record that you want to update , change its state :因此,使用 JPA 更新记录的正确方法是获取要更新的记录,更改其状态:

User user = entmngr.find(User.class, 2L);
user.setName("John")

Had the same problem for this code, db is postgresql:这段代码有同样的问题,db是postgresql:

@Modifying
@Query("DELETE FROM Account a WHERE a.id = :id AND a.user.id = :userId")
int delete(@Param("id") int id, @Param("userId") int userId);

I used dependency我使用了依赖

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>6.0.0.Alpha5</version>
    <type>pom</type>
</dependency>

After only change to the following dependency, all works:仅更改为以下依赖项后,一切正常:

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

Maybe it's a bug in the new version, or maybe we need some magic for Hibernate 6.0.0.Alpha5 + Spring Data JPA 2.3.0.RELEASE.也许这是新版本中的一个错误,或者也许我们需要 Hibernate 6.0.0.Alpha5 + Spring Data JPA 2.3.0.RELEASE 的一些魔法。 I didn't find any information.我没有找到任何信息。 If you know something about that, let me know.如果您对此有所了解,请告诉我。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM