leeoo / rapid-framework

Automatically exported from code.google.com/p/rapid-framework
0 stars 0 forks source link

当 IDENTITY_INSERT 设置为 OFF 时,不能为表 'user_info' 中的标识列插入显式值 #54

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
rapid版本:   3.5.1.3

使用的相关插件组合是: Spring-rest+spring-jdbc + sql server 2005

新增一条记录时,出现:

【java.sql.SQLException: 当 IDENTITY_INSERT 设置为 OFF 时,不能为表 
'user_info' 中的标识列插入显式值。 】

出错代码在dao中,如下:
【
String sql = getSqlGenerator().getInsertSql();
insertWithIdentity(entity,sql); //for sqlserver and mysql
】

我user_info 表如下:
【
CREATE TABLE [dbo].[user_info](
    [id] [numeric](18, 0) IDENTITY(1,1) NOT NULL,  --自增主键
........
】

也就是,自增主键id,插入了值,导致错误:
[INSERT INTO user_info (age,birth,id,name,password,sex ) VALUES ( ?,?,?,?,?,? ) 
]; 

请问怎么解决这个问题?期待您的回复,谢谢!

Original issue reported on code.google.com by gjh...@gmail.com on 29 Jun 2010 at 2:29

GoogleCodeExporter commented 9 years ago
我搞不明白你,既然你自己故意设置IDENTITY_INSERT 为 
OFF了.那你就别用insertWithIdentity(entity,sql)啊.
自己生成一个主键再插入不就得了.

Original comment by bad...@gmail.com on 29 Jun 2010 at 3:12

GoogleCodeExporter commented 9 years ago
错了,你查看这个:
http://hi.baidu.com/ebiyuan/blog/item/6b511987c030fe2bc75cc360.html

如果说是不能插入的话,你可以使用annocation
@Column(insertable=false)不插入这一列

Original comment by bad...@gmail.com on 29 Jun 2010 at 3:16

GoogleCodeExporter commented 9 years ago
我看关于 IDENTITY_INSERT 内容我看了,我想 
insertWithIdentity(entity,sql) 生成的sql 应该不包括下面这样一列:
【
[id] [numeric](18, 0) IDENTITY(1,1) NOT NULL,  --自增主键
】

如这样:  [INSERT INTO user_info (age,birth,name,password,sex ) VALUES ( 
?,?,?,?,? ) ]; 

Original comment by gjh...@gmail.com on 30 Jun 2010 at 12:54

GoogleCodeExporter commented 9 years ago
由于要支持 insertWithOracle,insertWithAssign
这些情况是需要id出现在insert语句中的。
所以如果你是使用sqlserver 

可以为id增加
@Column(insertable=false)
public Long getId() {
    return false;
}

Original comment by bad...@gmail.com on 30 Jun 2010 at 4:06

GoogleCodeExporter commented 9 years ago
谢谢你的回复,按照你的方法,问题解决!

Original comment by gjh...@gmail.com on 30 Jun 2010 at 6:33

GoogleCodeExporter commented 9 years ago
加了@Column(insertable=false) 这个后,删除时,出现下面错误:
【
BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar [DELETE FROM 
user_info WHERE  = ?]; nested exception is java.sql.SQLException: '=' 
附近有语法错误。
】

where 条件中id没有了

Original comment by gjh...@gmail.com on 30 Jun 2010 at 12:20

GoogleCodeExporter commented 9 years ago
这应该是个bug,你现在可以使用@Column(insertable=false,name="user_id")
即现在使用@Column你要指定name属性. 不然就报错了. 
下一版本会修复.

或者你可以下载在这里下载新的MetadataCreateUtils.java,现在已经�
��复这个问题.

http://rapid-framework.googlecode.com/svn/trunk/rapid-framework/src/rapid_framew
ork_common/cn/org/rapid_framework/jdbc/sqlgenerator/metadata/MetadataCreateUtils
.java

Original comment by bad...@gmail.com on 30 Jun 2010 at 2:45

GoogleCodeExporter commented 9 years ago
rapid-framework 新版本发布在即,我等几天吧。
谢谢,加油

Original comment by gjh...@gmail.com on 2 Jul 2010 at 2:28