Custom queries in Spring with JPA

大城市里の小女人 提交于 2019-12-08 12:24:54

问题


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

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 :)

My google results suggested my to add this to my class

@Autowired EntityManager entityManager;

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.

EDIT

My problem was too little knowledge of Spring.

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;
}

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;
}

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.

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.


回答1:


You don't need to autowire the EntityManager, Spring takes care of all this for you.

Instead in your repository you need to define a new method and annotate it with @Query.

Example:

@Query(value = "select current_timestamp from #{#entityName} u")
String findTimestamp();

You can find furhter information about using @Query in the spring docs: https://docs.spring.io/spring-data/jpa/docs/current/reference/html/#jpa.query-methods.at-query.




回答2:


The final solution was:

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

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.



来源:https://stackoverflow.com/questions/42530667/custom-queries-in-spring-with-jpa

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!