使用JPA在Spring中进行自定义查询 [英] Custom queries in Spring with JPA

查看:141
本文介绍了使用JPA在Spring中进行自定义查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在此处创建了与本教程类似的webapp: https://spring.io/guides/tutorials/react-and -spring-data-rest/.

I have created a webapp similiar to the tutorial here: https://spring.io/guides/tutorials/react-and-spring-data-rest/ .

我添加了postgresql db,一切正常.我的存储库中有一个基本的查询findByUsername(String name),可以正常工作.我的问题是,由于某种原因,我无法创建自定义查询,例如

I have added postgresql db and everything works fine. I have a basic query findByUsername(String name) in my repository which works fine. My problem is, I am for some reason unable to create custom queries e.g

"SELECT CURRENT_TIMESTAMP".

让我们说我要在哪里获取此语句的值进行测试.通过无法做到,我的意思是我不知道:)

Lets say i make a test where I just want to get the value of this statement. And by unable I mean I don't know how :)

我的Google搜索结果建议我将其添加到我的课程中

My google results suggested my to add this to my class

@Autowired EntityManager entityManager;

@Autowired EntityManager entityManager;

,然后在其上创建查询.但是entityManager出于某种原因从未初始化,总是返回null.这是正确的方法,我只是想念一些东西吗?还尝试添加spring-boot-starter-jdbc并使用JdbcTemplate,但也为null.

and then create queries on that. But entityManager is never initialized for some reason, return null always. Is this the right way and I'm just missing something? Also tried to add spring-boot-starter-jdbc and use JdbcTemplate but was also null.

编辑

我的问题是对Spring的了解太少了.

My problem was too little knowledge of Spring.

我有一个像这样的课程,并试图在那里使用Autowire.

I had a class like this and tried to use Autowire in there.

public class Person {
@Autowire
MyRepo myRepo;
private String p;
public Person(String p) {
    this.p = p;
}

正如我稍后所了解的那样,Spring并没有对此进行介绍.我用Person per = new Person("string");在RestController中调出了此类. 我所做的解决方案是在我的RestController类中为MyRepo自动接线,而我的Person构造函数看起来像这样

As I understood later, Spring doesn't pick this up. I called out this class in a RestController with Person per = new Person("string");. The solution I made was to Autowire MyRepo in my RestController class and my Person contstructor looked like this

public Person(String p, MyRepo myRepo) {
this.p = p;
this.myRepo = myRepo;
}

在Person类中删除了自动装配.这样,我设法在Person类中初始化myRepo并在那里使用它.这不是解决我问题的最好方法.

Removed the autowire in Person class. This way I managed to get myRepo initialized in Person class and use it there. This isn't the best soution for my problem.

我的第二个问题.完成这项工作后,我尝试将EntityManager相同的电线自动连接到我的Person类.刚刚用EntityManager替换了MyRepo,它也被初始化了.现在问题出在我的下一个代码行 Integer i = (Integer) em.createNativeQuery("select 1", Integer.class).getSingleResult();上.在这里,我得到一个错误 javax.persistence.PersistenceException:org.hibernate.MappingException:未知实体:java.lang.Integer . 该语句的目的是对我的数据库进行简单查询,以查询是否响应.

My second problem. After i got this working, I tried to autowire EntityManager the same to my Person class. Just replaced MyRepo with EntityManager and it was also initialized. Now the problem is with my next code line Integer i = (Integer) em.createNativeQuery("select 1", Integer.class).getSingleResult();. Here I get an error javax.persistence.PersistenceException: org.hibernate.MappingException: Unknown entity: java.lang.Integer. The purpose of this statement is to make a simple query to my DB, to if it responds.

推荐答案

最终的解决方案是:

Integer i = (Integer) em.createNativeQuery("select 1").getSingleResult();

只需从NatviveQuert中删除Integer.class部分.我想,如果我希望它能够存在,那我应该为Integer制作一个新的bean.
总的来说,这不是Spring的最佳实践,但它解决了我的问题.

Just removed the Integer.class part from NatviveQuert. I guess, if I had wanted it to be there, I should have made a new bean for Integer.
Overall this is not the best practice in Spring but it solved my problem.

这篇关于使用JPA在Spring中进行自定义查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆