有时,Hibernate 在持久化操作期间似乎随机执行查询:
select currval('MY_TABLE_NAME_id_seq');
实体:
@Entity
@Table(name = "MY_TABLE_NAME")
public class MyEntity {
@Id
@Column(name = "ID", unique = true, nullable = false)
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
}
代码:
@Transactional
public void persistMyEntity(String name) {
MyEntity entity= new MyEntity (name);
sessionFactory.getCurrentSession().persist(entity);
}
生成的sql:
insert into MY_TABLE_NAME(name) values ('xyz');
select currval('MY_TABLE_NAME_id_seq');
但通常select currval
不执行。对此有什么解释吗?
顺便说一句,我的问题与此非常相似,但问题中的解决方案对我不起作用。
笔记:
My_TABLE_NAME ddl sql:
CREATE TABLE my_table_name (
id bigserial NOT NULL,
name character varying(256) NOT NULL,
CONSTRAINT my_table_name_id PRIMARY KEY (id)
);
休眠属性:
Properties hibernateProperties = new Properties();
hibernateProperties.put("hibernate.dialect", HIBERNATE_DIALECT);
hibernateProperties.put("hibernate.show_sql", HIBERNATE_SHOW_SQL);
hibernateProperties.put("hibernate.hbm2ddl.auto", "none");
hibernateProperties.put("hibernate.connection.release_mode", "auto");
hibernateProperties.put("hibernate.archive.autodetection", ARCHIVE_AUTODETECTION);
hibernateProperties.put("hibernate.format_sql", true);
hibernateProperties.put("hibernate.use_sql_comments", true);
hibernateProperties.put("hibernate.generate_statistics", false);
hibernateProperties.put("hibernate.jdbc.use_scrollable_resultset", true);
hibernateProperties.put("hibernate.jdbc.use_streams_for_binary", true);
hibernateProperties.put("hibernate.jdbc.batch_size", 20);
hibernateProperties.put("hibernate.order_inserts", true);
hibernateProperties.put("hibernate.order_updates", true);
hibernateProperties.put("hibernate.jdbc.batch_versioned_data ", true);
hibernateProperties.put("hibernate.cache.region_prefix", "hibernate.cache");
hibernateProperties.put("hibernate.cache.use_query_cache", false);
hibernateProperties.put("hibernate.cache.use_second_level_cache", false);
sessionFactoryBean.setHibernateProperties(hibernateProperties);
- PostgreSQL 版本:11.5。
- hibernate.dialect:org.hibernate.dialect.PostgreSQL9Dialect
- 休眠版本:5.4.5.Final
- PostgreSQL JDBC 驱动程序:postgresql-9.4