Search code examples
javahibernatehql

Don`t work HQL queries in PostgreSQL


I am using Java 9, Hibernate 5, PostgreSQL 10 and I have been trying to do some HQL queries with database.

This method works:

public List<MP3> getAll(String name) {
    query = session.createQuery( "from " + name);
    List mp3 = query.list();
    return mp3;
}

But, this method does not work:

public void deleteByName(String name) {
        String hql = "DELETE MP3  where name = :name";
        query = session.createQuery(hql);
        query.setParameter("name",name);
        query.executeUpdate();
    }

My StackTrace is as follows:

08:31:54.292 [main] INFO  org.hibernate.orm.connections.access - HHH10001501: Connection obtained from JdbcConnectionAccess [org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator$ConnectionProviderJdbcConnectionAccess@401317a0] for (non-JTA) DDL execution was not in auto-commit mode; the Connection 'local transaction' will be committed and the Connection will be set into auto-commit mode.
08:31:54.308 [main] INFO  org.hibernate.hql.internal.QueryTranslatorFactoryInitiator - HHH000397: Using ASTQueryTranslatorFactory
Hibernate: 
    delete 
    from
        MP3 
    where
        name=?
08:31:54.386 [main] INFO  org.hibernate.orm.connections.pooling - HHH10001008: Cleaning up connection pool [jdbc:postgresql://localhost:5432/postgres]

Please help me in solving the issue.


Solution

  • Add @Transactional to the method :

    @Transactional
    public void deleteByName(String name) {
        String hql = "DELETE MP3  where name = :name";
        query = session.createQuery(hql);
        query.setParameter("name",name);
        query.executeUpdate();
    }