1、主鍵生成策略設置為 identity
主鍵生成策略設置為 native時
打印 sql:
09:24:52.551 INFO (TaskFacadeImpl.java :43) : TaskFacadeImpl#add Hibernate: select nextval ('hibernate_sequence')
改為 identity
09:34:04.933 INFO (TaskFacadeImpl.java :43) : TaskFacadeImpl#add Hibernate: insert into dm.dm_task (task_name, task_height, data, last_update_time, task_desc) values (?, ?, ?, ?, ?)
注意: 插入記錄需要手動開啟事務和提交事務(最好不要設置自動提交事務)
2、ERROR: The RETURNING clause of the INSERT statement is not supported in this version of Greenplum Database
使用 postgresql-12.1-1 + hibernate 插入記錄時沒有問題。
改為使用 greenplum。gpstate 指令查看版本 Greenplum Database 5.19.0(PostgreSQL 8.3.23)
master Greenplum Version: 'PostgreSQL 8.3.23 (Greenplum Database 5.19.0 build commit:7f5d6a22614522e47fa1020933e0a231a122b00a) on x86_64-pc-linux-gnu, compiled by GCC gcc (GCC) 6.2.0, 64-bit compiled on May 15 2019 16:16:39'
Greenplum Database 5.19.0 + hibernate 報錯,日志:
Hibernate:
insert
into
dm.dm_task
(task_name, task_height, data, last_update_time, task_desc)
values
(?, ?, ?, ?, ?) 09:24:58.793 WARN (SqlExceptionHelper.java :137) : SQL Error: 0, SQLState: 0A000 09:24:58.794 ERROR (SqlExceptionHelper.java :142) : ERROR: The RETURNING clause of the INSERT statement is not supported in this version of Greenplum Database.
因為 插入操作使用的使用 hibernate 的 save() 方法,默認 添加了 returning 子句獲取生成的主鍵。PostgreSQL 8.3.23 不支持。
mybaties 插入操作默認返回影響的記錄數,同樣會出現 returning clause 不支持的問題,見 Greenplum+mybatis問題解析
/** * Persist the given transient instance, first assigning a generated identifier. (Or * using the current value of the identifier property if the <tt>assigned</tt> * generator is used.) This operation cascades to associated instances if the * association is mapped with {@code cascade="save-update"} * * @param entityName The entity name * @param object a transient instance of a persistent class * * @return the generated identifier */ Serializable save(String entityName, Object object);
解決:使用 hibernate 執行原生 sql
public void add(Task task) { Session session = getSession(); // greenplum5.19.0 不支持save()的returning子句, // 調用 save() 方法報錯 ERROR: The RETURNING clause of the INSERT statement is not supported in this version of Greenplum Database. // session.save(task); String sqlTemp = "insert into dm.dm_task(task_name, task_height, data, last_update_time, task_desc) values (''{0}'', {1}, ''{2}'', ''{3}'', ''{4}'')"; String sql = MessageFormat.format(sqlTemp, task.getTaskName(), task.getTaskHeight(), task.getData(), task.getLastUpdateTime(), task.getTaskDesc()); session.createSQLQuery(sql).executeUpdate(); }