<Effective STL>笔记--仿函数
2013-03-26 07:04
267 查看
使用hibernate框架对pojo数据进行save到数据库时(笔者碰到的问题是在操作oracle数据库),由于某些配置不当会引起一些错误,
如:org.hibernate.exception.SQLGrammarException: could not get next sequence value
这是因为数据库表中id作为主键设置自动增长,而pojo模型中的id(如 userid)没有设置产生的找不到序列问题;解决方法:
hibernate连接oracle有两种方式,一种是制定sequence的名字 如:
< id name="id" column="ID"> < generator class="sequence"> < param name="sequence">DEPARTMENT_ID_SEQ< /param> < /generator> < /id>
如果要使用native,则必须创建名为HIBERNATE_SEQUENCE 的sequence
CREATE SEQUENCE HIBERNATE_SEQUENCE MINVALUE 1 MAXVALUE 99999
< id name="id" column="ID"> < generator class="native" /> < /id>
另外如果采用上述的native主键生成策略需创建一个触发器对改序列进行引用(sequence暂无验证),否则可能会报org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update错误:
create or replace trigger 触发器名
before insert on 表名
for each row
declare
begin
select seq_user.nextval
into:new.主键名 from dual;
end trigger_user;
另外产生org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update错误也有可能是插入的数据长度超出数据库该字段设置的长度所致
如:org.hibernate.exception.SQLGrammarException: could not get next sequence value
这是因为数据库表中id作为主键设置自动增长,而pojo模型中的id(如 userid)没有设置产生的找不到序列问题;解决方法:
hibernate连接oracle有两种方式,一种是制定sequence的名字 如:
< id name="id" column="ID"> < generator class="sequence"> < param name="sequence">DEPARTMENT_ID_SEQ< /param> < /generator> < /id>
如果要使用native,则必须创建名为HIBERNATE_SEQUENCE 的sequence
CREATE SEQUENCE HIBERNATE_SEQUENCE MINVALUE 1 MAXVALUE 99999
< id name="id" column="ID"> < generator class="native" /> < /id>
另外如果采用上述的native主键生成策略需创建一个触发器对改序列进行引用(sequence暂无验证),否则可能会报org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update错误:
create or replace trigger 触发器名
before insert on 表名
for each row
declare
begin
select seq_user.nextval
into:new.主键名 from dual;
end trigger_user;
另外产生org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update错误也有可能是插入的数据长度超出数据库该字段设置的长度所致
相关文章推荐
- <Effective STL>笔记--vector和string
- &lt;Effective STL&gt;笔记--仿函数
- <Effective STL>笔记--算法
- &lt;Effective STL&gt;笔记--容器
- <More Effective C++>笔记--技巧
- <<Effective STL>> 条款31 容器排序笔记
- <STL学习笔记---Vector>删除与某值相等的元素
- <Effective Mordern C++>笔记:Item 3:Understand decltype .
- <Effective Mordern C++>笔记:Item 5:Prefer auto to explicit type declarations.
- <Effective STL>笔记--容器
- <<C++标准程序库>>中的STL简单学习笔记
- <Effective STL>笔记--关联容器
- <Effective STL>笔记--迭代器
- <Effective Mordern C++>笔记:Item 2:Understand auto type deduction.
- &lt;Effective STL&gt;笔记--算法
- &lt;Effective STL&gt;笔记--迭代器
- &lt;Effective STL&gt;笔记--关联容器
- <Effective Mordern C++>笔记:Item 8:prefer nullptr to 0 and NULL.
- &lt;Effective STL&gt;笔记--vector和string
- <Effective Mordern C++>笔记:Item 9:prefer alias declarations to typedefs.