简体   繁体   English

Spring-boot:JPA查询(JPQL)不起作用,但在更改为本机查询时有效

[英]Spring-boot: JPA query (JPQL) do not work but works when change to native query

I'm new on Spring-boot. 我是Spring-boot的新手。 I created a simple JPA app that use web and H2 and JPA dependency. 我创建了一个简单的JPA应用程序,它使用webH2JPA依赖。 I want to have embedded database with h2 and use JPA to connect with and have some requests. 我想用h2嵌入数据库并使用JPA连接并有一些请求。

I created the local table in schema.sql and put this code: 我在schema.sql创建了本地表并输入以下代码:

drop TABLE  if EXISTS  stock;
CREATE TABLE stock(stockId INT ,companyName VARCHAR(20),price DECIMAL (10,2)
);

INSERT INTO stock VALUES (1,'apple',345.01);
INSERT INTO stock VALUES (2,'google',8999.00);
INSERT INTO stock VALUES (3,'kashky',56.60);

I also created the stock like this: 我也创建了这样的股票:

@Entity
public class Stock {

    @Id
    private int stockId;

    private String companyName;

    private double price;

... with all setter and getters

}

and this is my application.properties 这是我的application.properties

spring.jpa.hibernate.ddl-auto=none
server.port=9090

Java Persistence Query language (JPQL) do not work but when I change to native query it it works fine. Java持久性查询语言(JPQL)不起作用,但当我更改为本机查询时,它工作正常。

this code that should return all my records and does not works: 这段代码应该返回我的所有记录并且不起作用:

@SpringBootApplication
@RestController
public class SpringDataTestApplication {

    @Autowired
    private EntityManager em;

    @RequestMapping("/stocks")
    public List<Stock> stocks(){
        return em.createQuery("select s from Stock s").getResultList();
    }

if I change query to Native mode it is works: 如果我将查询更改为本机模式它是有效的:

@RequestMapping("/stocks")
public List<Stock> stocks(){
    return em.createNativeQuery("select * from Stock").getResultList();
}

and this is the error: 这是错误:

This application has no explicit mapping for /error, so you are seeing this as a fallback.

Wed Jan 25 12:51:38 IRST 2017
There was an unexpected error (type=Internal Server Error, status=500).
org.hibernate.exception.SQLGrammarException: could not prepare statement

why this happen? 为什么会这样? I think I have some problem with JPA cause in later I can not even use StockRepository that i do not mention in code but i think all of this is chained to gather. 我认为我有一些问题与JPA原因在后来我甚StockRepository不能使用StockRepository ,我没有在代码中提到,但我认为所有这些都是链接收集。

I would guess that you would need to add dialect infos in your application.properties file, eg for hibernate: 我猜你需要在application.properties文件中添加方言信息 ,例如对于hibernate:

<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect"/>

I guess spring has its own knobs for that task. 我猜春天有自己的旋钮来完成这项任务。 Knowing the dialect, your program knows the "flavor" of the SQL to create. 知道了方言,你的程序就知道要创建的SQL的“味道”。 Native SQL works, because there is no translation to be done. 本机SQL有效,因为没有要完成的转换。

I finally find the answer. 我终于找到了答案。 It is an unwritten convention that exist and when you use camel-case properties you had to use under lined fields in schema.sql file. 这是一个不成文的约定 ,当你使用camel-case属性时,你必须使用schema.sql文件中的schema.sql字段。

so with my entity file like this: 所以我的实体文件是这样的:

@Entity
public class Stock {

    @Id
    private int stockId;

    private String companyName;

    private double price;

... with all setter and getters

}

I changed schema.sql like this and error gone and it's worked fine. 我像这样更改了schema.sql ,错误消失了,它运行正常。

drop TABLE  if EXISTS  stock;
CREATE TABLE stock(stock_id INT ,company_name VARCHAR(20),price DECIMAL (10,2)
);

INSERT INTO stock VALUES (1,'apple',345.01);
INSERT INTO stock VALUES (2,'google',8999.00);
INSERT INTO stock VALUES (3,'kashky',56.60);

I also check that even stock_Id and company_Name meet the JPA convention and works. 我还检查,即使stock_Idcompany_Name符合JPA约定并且有效。 so you just need to put Underline between camel-cased properties name. 所以你只需要在camel-cased属性名称之间加下下划线

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

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