I'm working on a project with Spring Data JPA. and I want to add some customized behavior to the repository.
At the moment I'm trying to create a view by executing the method
The repository class is as follows.
public interface MyQueryRepository extends JpaRepository<MyQuery, Long>, MyQueryRepositoryCustom {
public MyQuery findById(long id);
public list executeMyQuery();
}
This is the customization:
public class MyQueryRepositoryImpl implements MyQueryRepositoryCustom {
@PersistenceContext
private EntityManager entityManager;
public List executeMyQuery() {
return entityManager.createQuery("CREATE VIEW result_set AS select record FROM my_data").getResultList();
}
}
Howeve, I get the following error.
[ERROR] [http-bio-8080-exec-10] ErrorCounter - line 1:1: unexpected token: CREATE
antlr.NoViableAltException: unexpected token: CREATE
at org.hibernate.hql.internal.antlr.HqlBaseParser.statement(HqlBaseParser.java:198) [hibernate-core-5.1.1.Final.jar:5.1.1.Final]
The above code works for SELECT queries.
Is there any other way, that I could CREATE VIEW using entity managers. Thanks in advance
To work with getResultList()
, you have to make Select and not CREATE? UPDATE? Or DELETE.
So if you want to CREATE a view then you have to execute your query, and not getResultList()
, to create a view i suggest to use CreateNativeQuery for example :
Query q = entityManager.createNativeQuery("CREATE VIEW result_set AS
select record FROM my_data");
q.executeUpdate();
Else if you want to get values from your query then you have to change your query to SELECT something FROM result_set
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.