误用SQL Server关键字导致的问题
刚才在练习使用Hibernate的复合主键例子时,自己很自信的认为已经领悟了其中的原理,于是开始制作例子程序。恩,想起前段时间确实有个可以利用复合主键的地方,那就是公寓寝室表,于是开始建立数据库表:
利用Eclipse自动生成机制,很快就完成了映射文件等的生成,测试一下,却发觉出现错误
1
WARN No appenders could be found
for
logger (org.hibernate.cfg.Environment).
2
3 log4j:WARN Please initialize the log4j system properly.
4
5 Hibernate: insert into HAIING.dbo.DORMROOM (desc, dorm, room) values ( ? , ? , ? )
6
7 org.hibernate.exception.GenericJDBCException: could not insert: [com.haiing.hibernate.fuhezhujian.DormRoom]
8
9 at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java: 82 )
10
11 at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java: 70 )
12
13 at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java: 43 )
14
15 at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java: 1869 )
16
17 at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java: 2200 )
18
19 at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java: 46 )
20
21 at org.hibernate.engine.ActionQueue.execute(ActionQueue.java: 239 )
22
23 at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java: 223 )
24
25 at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java: 136 )
26
27 at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java: 274 )
28
29 at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java: 27 )
30
31 at org.hibernate.impl.SessionImpl.flush(SessionImpl.java: 730 )
32
33 at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java: 324 )
34
35 at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java: 86 )
36
37 at com.haiing.hibernate.fuhezhujian.DormRoomOperate.insert(DormRoomOperate.java: 26 )
38
39 at com.haiing.hibernate.fuhezhujian.Test2PK.main(Test2PK.java: 19 )
40
41 Caused by: java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]在关键字 ' desc ' 附近有语法错误。
42
43 at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
44
45 at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
46
47 at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processErrorToken(Unknown Source)
48
49 at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReplyToken(Unknown Source)
50
51 at com.microsoft.jdbc.sqlserver.tds.TDSRPCRequest.processReplyToken(Unknown Source)
52
53 at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReply(Unknown Source)
54
55 at com.microsoft.jdbc.sqlserver.SQLServerImplStatement.getNextResultType(Unknown Source)
56
57 at com.microsoft.jdbc.base.BaseStatement.commonTransitionToState(Unknown Source)
58
59 at com.microsoft.jdbc.base.BaseStatement.postImplExecute(Unknown Source)
60
61 at com.microsoft.jdbc.base.BasePreparedStatement.postImplExecute(Unknown Source)
62
63 at com.microsoft.jdbc.base.BaseStatement.commonExecute(Unknown Source)
64
65 at com.microsoft.jdbc.base.BaseStatement.executeUpdateInternal(Unknown Source)
66
67 at com.microsoft.jdbc.base.BasePreparedStatement.executeUpdate(Unknown Source)
68
69 at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java: 22 )
70
71 at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java: 1853 )
72
73 12 more
74
2
3 log4j:WARN Please initialize the log4j system properly.
4
5 Hibernate: insert into HAIING.dbo.DORMROOM (desc, dorm, room) values ( ? , ? , ? )
6
7 org.hibernate.exception.GenericJDBCException: could not insert: [com.haiing.hibernate.fuhezhujian.DormRoom]
8
9 at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java: 82 )
10
11 at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java: 70 )
12
13 at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java: 43 )
14
15 at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java: 1869 )
16
17 at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java: 2200 )
18
19 at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java: 46 )
20
21 at org.hibernate.engine.ActionQueue.execute(ActionQueue.java: 239 )
22
23 at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java: 223 )
24
25 at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java: 136 )
26
27 at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java: 274 )
28
29 at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java: 27 )
30
31 at org.hibernate.impl.SessionImpl.flush(SessionImpl.java: 730 )
32
33 at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java: 324 )
34
35 at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java: 86 )
36
37 at com.haiing.hibernate.fuhezhujian.DormRoomOperate.insert(DormRoomOperate.java: 26 )
38
39 at com.haiing.hibernate.fuhezhujian.Test2PK.main(Test2PK.java: 19 )
40
41 Caused by: java.sql.SQLException: [Microsoft][SQLServer 2000 Driver for JDBC][SQLServer]在关键字 ' desc ' 附近有语法错误。
42
43 at com.microsoft.jdbc.base.BaseExceptions.createException(Unknown Source)
44
45 at com.microsoft.jdbc.base.BaseExceptions.getException(Unknown Source)
46
47 at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processErrorToken(Unknown Source)
48
49 at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReplyToken(Unknown Source)
50
51 at com.microsoft.jdbc.sqlserver.tds.TDSRPCRequest.processReplyToken(Unknown Source)
52
53 at com.microsoft.jdbc.sqlserver.tds.TDSRequest.processReply(Unknown Source)
54
55 at com.microsoft.jdbc.sqlserver.SQLServerImplStatement.getNextResultType(Unknown Source)
56
57 at com.microsoft.jdbc.base.BaseStatement.commonTransitionToState(Unknown Source)
58
59 at com.microsoft.jdbc.base.BaseStatement.postImplExecute(Unknown Source)
60
61 at com.microsoft.jdbc.base.BasePreparedStatement.postImplExecute(Unknown Source)
62
63 at com.microsoft.jdbc.base.BaseStatement.commonExecute(Unknown Source)
64
65 at com.microsoft.jdbc.base.BaseStatement.executeUpdateInternal(Unknown Source)
66
67 at com.microsoft.jdbc.base.BasePreparedStatement.executeUpdate(Unknown Source)
68
69 at org.hibernate.jdbc.NonBatchingBatcher.addToBatch(NonBatchingBatcher.java: 22 )
70
71 at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java: 1853 )
72
73 12 more
74
郁闷了很长时间,开始以为是主键类复写的hash类和equals类有错误,于是使用org.apache.commons.lang中的对应方法重写,可还是不行。端详了很长时间,每一种可能的错误都考虑了,感觉没啥错误了呀!
又看了一遍错误的代码,发觉已经生成了HQL语句,而且错误代码几乎全是数据库的问题。数据库的驱动包我已经加进来了,而且在其他的例子中运行很正常呀!莫非是提交语句的错误,看了一下看不出什么来,莫非是数据库的问题?去查看表,发觉有点怪怪的!
这个desc怎么成介个样子了,莫非设置成复合主键之后就成了这个样子,加进一个新的字段没有呀。嘿嘿,那我就删了这个字段,在重新加进来。结果还是不行!
这时我又调整了思路,把生成的SQL语句复制到查询分析器中执行,还是显示“在关键字 'desc' 附近有语法错误”,我晕!看来就是数据库的问题了。等等,我突然意识到什么了!
关键字?
desc?
莫非?不是莫非,就是这样呀,我怎么忘得一干二净了,desc这么好的词汇早就被前辈们定义成关键字了,怎么会留给我来使用呢!
改掉desc,一切问题全部解决了,数据成功的插入到数据库之中!
唉,惭愧呀~