Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

312
Visualizações
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

about 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

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

about 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda