我使用的是Java 9,Hibernate 5,PostgreSQL 10,我一直在尝试用数据库进行一些HQL查询。
此方法有效:
public List<MP3> getAll(String name) {
query = session.createQuery( "from " + name);
List mp3 = query.list();
return mp3;
}
但是,这种方法不起作用:
public void deleteByName(String name) {
String hql = "DELETE MP3 where name = :name";
query = session.createQuery(hql);
query.setParameter("name",name);
query.executeUpdate();
}
我的StackTrace如下:
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]
请帮助我解决问题。
答案 0 :(得分:0)
将@Transactional
添加到方法中:
@Transactional
public void deleteByName(String name) {
String hql = "DELETE MP3 where name = :name";
query = session.createQuery(hql);
query.setParameter("name",name);
query.executeUpdate();
}