hibernate 不能插入数据

1别 2013-05-15 06:36:30
数据库有一张表Subject,5个字段,主键是id,设为自动增长的建表语句
create table subject
(id int identity(1,1) primary key,
subID int not null,
subName varchar(20) not null,
subJoinTime date not null,
subDesc varchar(20))


hibernate映射表配置文件
<class name="com.ccsu.cxl.entity.Subject" table="Subject">    
<id name="id" >
<generator class="identity"></generator>
</id>
<property name="subID" />
<property name="subName" />
<property name="subJoinTime" />
<property name="subDesc" />

并且我已经把这个文件放到hibernate.cfg.xml文件中了<mapping resource="com/ccsu/cxl/action/Subject.hbm.xml" />

实体类Subject有5个属性 与表的5个字段一一对应,除了主键外,我给其他4个属性设了值,然后我就sava了对象。但是没有成功sava

下面是错误提示
Hibernate: insert into Subject (subID, subName, subJoinTime, subDesc, id) values (?, ?, ?, ?, ?)16:30:01,343  WARN JDBCExceptionReporter:77 - SQL Error: 0, SQLState: null16:30:01,359 ERROR JDBCExceptionReporter:78 - 没有为参数号 5 设置值。16:30:02,609 ERROR runtime:96 - Template processing error: "Method public java.lang.String org.hibernate.exception.NestableRuntimeException.getMessage(int) threw an exception when invoked on org.hibernate.exception.GenericJDBCException: could not insert: [com.ccsu.cxl.entity.Subject]"Method public java.lang.String org.hibernate.exception.NestableRuntimeException.getMessage(int) threw an exception when invoked on org.hibernate.exception.GenericJDBCException: could not insert: [com.ccsu.cxl.entity.Subject]The problematic instruction:----------==> ${msg[0]} [on line 68, column 29 in org/apache/struts2/dispatcher/error.ftl]----------Java backtrace for programmers:----------freemarker.template.TemplateModelException: Method public java.lang.String org.hibernate.exception.NestableRuntimeException.getMessage(int) threw an exception when invoked on org.hibernate.exception.GenericJDBCException: could not insert: [com.ccsu.cxl.entity.Subject] at freemarker.ext.beans.SimpleMethodModel.exec(SimpleMethodModel.java:130) at freemarker.ext.beans.SimpleMethodModel.get(SimpleMethodModel.java:138) at freemarker.core.DynamicKeyName.dealWithNumericalKey(DynamicKeyName.java:111) at freemarker.core.DynamicKeyName._getAsTemplateModel(DynamicKeyName.java:90) at freemarker.core.Expression.getAsTemplateModel(Expression.java:89) at freemarker.core.Expression.getStringValue(Expression.java:93) at freemarker.core.DollarVariable.accept(DollarVariable.java:76) at freemarker.core.Environment.visit(Environment.java:221)


求解为什么不能插入数据
...全文
215 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
wolf_19870802 2013-05-16
  • 打赏
  • 举报
回复
干嘛不用annotation?有时候觉得比xml好用···
whhmkj 2013-05-16
  • 打赏
  • 举报
回复
既然用identity 那你总要把数据库的序列名写上吧 不然它怎么知道是哪个
hexin20117 2013-05-16
  • 打赏
  • 举报
回复
<id name="id" type="java.lang.Long"> <column name="ID" precision="22" scale="0" /> <generator class="sequence"> <param name="sequence">SEQ_USERS</param> </generator> </id> 在数据库里见序列了吗?sequence就可以自增长,SEQ_USERS是序列名
头发凌乱 2013-05-16
  • 打赏
  • 举报
回复
id已经设置自动增长列,不需要在出入了。数据库会自动添加
insert into Subject (subID, subName, subJoinTime, subDesc) values (?, ?, ?, ?)
试试
坚持2012 2013-05-16
  • 打赏
  • 举报
回复
调用方法怎么写的 ?
头发凌乱 2013-05-16
  • 打赏
  • 举报
回复
楼主,你的映射表没配置对应的数据库表字段吧
<property name="subID" colum="subID"/>    
tfling2012 2013-05-16
  • 打赏
  • 举报
回复
id为主键 自增长的属性,不需要你赋值,会自动赋值。 看下你是怎么调用save方法的
  • 打赏
  • 举报
回复
数据库中你的主键是自动增长吗?如果不是自动增长,那肯定会报空指针异常。还有 我们的映射文件一般这样写的
<id name="sid" type="java.lang.Integer">
            <column name="sid" />
            <generator class="native" />
        </id>
火影之贺 2013-05-15
  • 打赏
  • 举报
回复
尝试一:<generator class="native"></generator> ; 尝试二:在数据库中先手工插入一条数据,再调试你的代码
xianwangkai 2013-05-15
  • 打赏
  • 举报
回复
你去掉在xml中 <id name="id" > <generator class="identity"></generator> </id>值,试一试? 最好贴出你的代码。
ming_311 2013-05-15
  • 打赏
  • 举报
回复
你的save方法是怎么写的?
1别 2013-05-15
  • 打赏
  • 举报
回复
错误提示 Hibernate: insert into Subject (subID, subName, subJoinTime, subDesc, id) values (?, ?, ?, ?, ?) 18:25:43,859 WARN JDBCExceptionReporter:77 - SQL Error: 0, SQLState: null 18:25:43,859 ERROR JDBCExceptionReporter:78 - 没有为参数号 5 设置值。 18:25:43,890 ERROR runtime:96 - Template processing error: "Method public java.lang.String org.hibernate.exception.NestableRuntimeException.getMessage(int) threw an exception when invoked on org.hibernate.exception.GenericJDBCException: could not insert: [com.ccsu.cxl.entity.Subject]" Method public java.lang.String org.hibernate.exception.NestableRuntimeException.getMessage(int) threw an exception when invoked on org.hibernate.exception.GenericJDBCException: could not insert: [com.ccsu.cxl.entity.Subject] The problematic instruction: ---------- ==> ${msg[0]} [on line 68, column 29 in org/apache/struts2/dispatcher/error.ftl] ---------- Java backtrace for programmers: ---------- freemarker.template.TemplateModelException: Method public java.lang.String org.hibernate.exception.NestableRuntimeException.getMessage(int) threw an exception when invoked on org.hibernate.exception.GenericJDBCException: could not insert: [com.ccsu.cxl.entity.Subject] at freemarker.ext.beans.SimpleMethodModel.exec(SimpleMethodModel.java:130) at freemarker.ext.beans.SimpleMethodModel.get(SimpleMethodModel.java:138) at freemarker.core.DynamicKeyName.dealWithNumericalKey(DynamicKeyName.java:111) at freemarker.core.DynamicKeyName._getAsTemplateModel(DynamicKeyName.java:90) at freemarker.core.Expression.getAsTemplateModel(Expression.java:89)
1别 2013-05-15
  • 打赏
  • 举报
回复
在线等。。。已经纠结2天了

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧