我在JPA中的本地查询有什么问题?
我正在向数据库发送非常简单的查询,但是我会遇到错误。感觉就像我缺少一些非常简单的东西。我想它不允许我创建它,因为单词顺序是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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您查看日志时,问题更容易识别。您会看到这样的条目:
因此,让我们看看执行了哪些SQL语句。因此,我们将以下内容添加到
application.properties
假设您让Spring Boot创建表格,您将看到以下内容:
并且当我们点击存储库方法时,我们会看到
那么,即使我们指定了
@Table(name =“'''''”)
,为什么要在小写中创建表?默认值是
org.springframework.boot.boot.orm.jpa.jpa.hibernate.springphysicalmangestrategy
spring.jpa.hibernate.Meaming.mysical-Strategy
但是我们希望它以
@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:
So let's see what SQL statements are executed. So we add the following to
application.properties
Assuming you let spring boot create your tables, you will see the following:
And when we hit the repository method we see
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
isorg.springframework.boot.orm.jpa.hibernate.SpringPhysicalNamingStrategy
, whichBut we want it to take the names we use in
@Table
. That works when setting the property tospring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
.Yor native query will need the matching casing though.
您需要使用索引参数
或命名参数
You need to use index parameters
or named parameters