我在JPA中的本地查询有什么问题?

发布于 2025-01-26 15:50:19 字数 1128 浏览 1 评论 0原文

我正在向数据库发送非常简单的查询,但是我会遇到错误。感觉就像我缺少一些非常简单的东西。我想它不允许我创建它,因为单词顺序是H2 db上的关键字,因此我将其在表注释中以引号标记。

   @Query(value = "select * from `ORDER` o where o.basket_id= :basketId ", nativeQuery = true)
    Optional<Order> getOrderByBasketId(Long basketId);
@Entity
@Getter
@Setter
@Table(name = "`ORDER`")
public class Order extends BaseExtendedModel{
            private BigDecimal price;
            @Enumerated(EnumType.STRING)
            private OrderStatus orderStatus;
            @OneToOne
            private Customer customer;
            @OneToOne(cascade = CascadeType.MERGE)
            private Basket basket;
            @OneToOne(cascade = CascadeType.ALL, mappedBy = "order")
            private OrderAddress orderAddress;
}
{
    "errorMessage": "could not prepare statement; SQL [select * from `ORDER` o where o.basket_id= ? ]; nested exception is org.hibernate.exception.SQLGrammarException: could not prepare statement"
}

I'm sending a very simple query to the database, but I'm getting an error. It feels like I'm missing something very simple. I guess it wouldn't allow me to create it because the word order is a keyword on the h2 db, so I put it in quotation marks within the table annotation.

   @Query(value = "select * from `ORDER` o where o.basket_id= :basketId ", nativeQuery = true)
    Optional<Order> getOrderByBasketId(Long basketId);
@Entity
@Getter
@Setter
@Table(name = "`ORDER`")
public class Order extends BaseExtendedModel{
            private BigDecimal price;
            @Enumerated(EnumType.STRING)
            private OrderStatus orderStatus;
            @OneToOne
            private Customer customer;
            @OneToOne(cascade = CascadeType.MERGE)
            private Basket basket;
            @OneToOne(cascade = CascadeType.ALL, mappedBy = "order")
            private OrderAddress orderAddress;
}
{
    "errorMessage": "could not prepare statement; SQL [select * from `ORDER` o where o.basket_id= ? ]; nested exception is org.hibernate.exception.SQLGrammarException: could not prepare statement"
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

剑心龙吟 2025-02-02 15:50:19

当您查看日志时,问题更容易识别。您会看到这样的条目:

org.h2.jdbc.jdbcsqlsyntaxerrorexception:未找到表“顺序”; SQL语句:

因此,让我们看看执行了哪些SQL语句。因此,我们将以下内容添加到application.properties

spring.jpa.show-sql = true

假设您让Spring Boot创建表格,您将看到以下内容:

冬眠:如果存在“订单”级联
Hibernate:创建表“顺序” ...

并且当我们点击存储库方法时,我们会看到

从'o.name =? [42102-200]

那么,即使我们指定了@Table(name =“'''''”),为什么要在小写中创建表?

默认值是org.springframework.boot.boot.orm.jpa.jpa.hibernate.springphysicalmangestrategy

  • spring.jpa.hibernate.Meaming.mysical-Strategy
  • 的 snake_case
  • 较低式台表名称。

但是我们希望它以@Table中使用的名称。将属性设置为spring.jpa.hibernate.naming.meming.physical-strategy = org.hibernate.boot.model.model.model.model.model.model.model.physicalnamingstrategystrategystanctandimpl时。

不过,本地查询需要匹配的外壳。

The problem is easier to identidy when you have a look at the logs. You'll see an entry like this:

org.h2.jdbc.JdbcSQLSyntaxErrorException: Table "ORDER" not found; SQL statement:

So let's see what SQL statements are executed. So we add the following to application.properties

spring.jpa.show-sql=true

Assuming you let spring boot create your tables, you will see the following:

Hibernate: drop table if exists "order" CASCADE
Hibernate: create table "order" ...

And when we hit the repository method we see

select * from `ORDER` o where o.name= ? [42102-200]

So why did it create the table in lowercase, even though we specified @Table(name = "`ORDER`")?

The default for spring.jpa.hibernate.naming.physical-strategy is org.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy, which

  • replaces dots with underscores
  • changes CamelCase to snake_case
  • lower-cases table names.

But we want it to take the names we use in @Table. That works when setting the property to spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl.

Yor native query will need the matching casing though.

素染倾城色 2025-02-02 15:50:19

您需要使用索引参数

@Query(value = "select * from `ORDER` o where o.basket_id= ?1", nativeQuery = true)
Optional<Order> getOrderByBasketId(Long basketId);

或命名参数

@Query(value = "select * from `ORDER` o where o.basket_id= :basketId", nativeQuery = true)
Optional<Order> getOrderByBasketId(@Param("basketId") Long basketId);

You need to use index parameters

@Query(value = "select * from `ORDER` o where o.basket_id= ?1", nativeQuery = true)
Optional<Order> getOrderByBasketId(Long basketId);

or named parameters

@Query(value = "select * from `ORDER` o where o.basket_id= :basketId", nativeQuery = true)
Optional<Order> getOrderByBasketId(@Param("basketId") Long basketId);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文