当前位置:编程学习 > JAVA >>

hibernate 不能插入数据

数据库有一张表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)


求解为什么不能插入数据  --------------------编程问答-------------------- 在线等。。。已经纠结2天了 --------------------编程问答-------------------- 错误提示
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) --------------------编程问答-------------------- 你的save方法是怎么写的? --------------------编程问答-------------------- 你去掉在xml中 <id name="id" >        
   <generator class="identity"></generator>      
</id>值,试一试?
最好贴出你的代码。 --------------------编程问答-------------------- 尝试一:<generator class="native"></generator> ;
尝试二:在数据库中先手工插入一条数据,再调试你的代码
--------------------编程问答-------------------- 数据库中你的主键是自动增长吗?如果不是自动增长,那肯定会报空指针异常。还有 我们的映射文件一般这样写的
<id name="sid" type="java.lang.Integer">
            <column name="sid" />
            <generator class="native" />
        </id>
--------------------编程问答-------------------- id为主键 自增长的属性,不需要你赋值,会自动赋值。
看下你是怎么调用save方法的  --------------------编程问答-------------------- 楼主,你的映射表没配置对应的数据库表字段吧
<property name="subID" colum="subID"/>    
--------------------编程问答-------------------- 调用方法怎么写的
? --------------------编程问答-------------------- id已经设置自动增长列,不需要在出入了。数据库会自动添加
insert into Subject (subID, subName, subJoinTime, subDesc) values (?, ?, ?, ?)
试试 --------------------编程问答--------------------  <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是序列名 --------------------编程问答-------------------- 既然用identity 那你总要把数据库的序列名写上吧 不然它怎么知道是哪个 --------------------编程问答-------------------- 干嘛不用annotation?有时候觉得比xml好用···
补充:Java ,  Java EE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,