Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

315
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda