Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

318
Views
Is it possible to create a view using the EntityManager with Spring Data JPA?

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

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!