Course course1 = new Course();
course1.setCno("0411");
course1.setCname("编译原理");
session.save(course1);
trans.commit();
course表结构create table course(cno char(4),cname varchar(20),primary key(cno));
它总是给报错
java.lang.NullPointerException
at com.mysql.jdbc.PreparedStatement.asSql(PreparedStatement.java:649)
at com.mysql.jdbc.PreparedStatement.asSql(PreparedStatement.java:587)
at com.mysql.jdbc.PreparedStatement.toString(PreparedStatement.java:4068)
at java.lang.String.valueOf(String.java:2827)
at java.lang.StringBuffer.append(StringBuffer.java:219)
at com.mysql.jdbc.trace.Tracer.printParameters(Tracer.aj:240)
at com.mysql.jdbc.trace.Tracer.printEntering(Tracer.aj:167)
at com.mysql.jdbc.trace.Tracer.entry(Tracer.aj:126)
at com.mysql.jdbc.trace.Tracer.ajc$before$com_mysql_jdbc_trace_Tracer$1$f51c62b8(Tracer.aj:45)
at com.mysql.jdbc.Connection.registerStatement(Connection.java)
at com.mysql.jdbc.Statement.<init>(Statement.java:270)
at com.mysql.jdbc.PreparedStatement.<init>(PreparedStatement.java:500)
at com.mysql.jdbc.Connection.clientPrepareStatement(Connection.java:2187)
at com.mysql.jdbc.Connection.prepareStatement(Connection.java:4829)
at com.mysql.jdbc.Connection.prepareStatement(Connection.java:4734)
at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:534)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:116)
at org.hibernate.jdbc.AbstractBatcher.prepareStatement(AbstractBatcher.java:109)
at org.hibernate.jdbc.AbstractBatcher.prepareBatchStatement(AbstractBatcher.java:244)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2242)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2678)
at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:79)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:279)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:263)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:167)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:321)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:50)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1028)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:366)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:137)
at bean.Test1.main(Test1.java:25)
我看了下生成的SQL语句——Hibernate: insert into course (cname, cno) values (?, ?)
如你们所见cname和cno属性在持久化类中都是String类型的,所以我推测这个问题是hibernate把赋值的顺序搞串了,各位看看有没有好的办法把顺序给固定下来,比如可不可以起别名什么的
下面是Course.hbm.xml的配置
<class name="bean.Course" table="course">
<id name="cno" column="cno" type="java.lang.String" length="4">
<generator class="uuid"></generator>
</id>
<property name="cname" type="java.lang.String">
<column name="cname" length="20"></column>
</property>
12 个解决方案
#1
#2
空指针,应该是session或trans未实例化吧,你检查检查。
#3
我觉得不是顺序的问题
#4
表中字段的数据类型没定义正确
#5
正解
#6
和你同样的问题呀!!!怎么解决?
#7
不是这样搞吧。好像HIBERNATE可以设置以对象来生成表的。 你去查查hibernate手册上面有的,
#8
有弄事务吗?。。。感觉怎么没有事务处理~
#9
哥们 我弄好了 你把Myeclipse项目中jre移除了 重新在windows属性里build 中加载自己安装的jdk就ok了
#10
<script type="text/javascript">
alimama_pid="mm_30977295_2853632_9914180";
alimama_type="g";
alimama_tks={};
alimama_tks.style_i=2;
alimama_tks.lg_i=1;
alimama_tks.w_i=572;
alimama_tks.h_i=69;
alimama_tks.btn_i=1;
alimama_tks.txt_s="连衣裙";
alimama_tks.hot_i=1;
alimama_tks.hc_c="0065FF";
alimama_tks.cid_i=0;
alimama_tks.t_i=1;
</script>
<script type="text/javascript" src="http://a.alimama.cn/inf.js"></script>
alimama_pid="mm_30977295_2853632_9914180";
alimama_type="g";
alimama_tks={};
alimama_tks.style_i=2;
alimama_tks.lg_i=1;
alimama_tks.w_i=572;
alimama_tks.h_i=69;
alimama_tks.btn_i=1;
alimama_tks.txt_s="连衣裙";
alimama_tks.hot_i=1;
alimama_tks.hc_c="0065FF";
alimama_tks.cid_i=0;
alimama_tks.t_i=1;
</script>
<script type="text/javascript" src="http://a.alimama.cn/inf.js"></script>
#11
<script type="text/javascript">
alimama_pid="mm_30977295_2853632_9914180";
alimama_type="g";
alimama_tks={};
alimama_tks.style_i=2;
alimama_tks.lg_i=1;
alimama_tks.w_i=572;
alimama_tks.h_i=69;
alimama_tks.btn_i=1;
alimama_tks.txt_s="连衣裙";
alimama_tks.hot_i=1;
alimama_tks.hc_c="0065FF";
alimama_tks.cid_i=0;
alimama_tks.t_i=1;
</script>
<script type="text/javascript" src="http://a.alimama.cn/inf.js"></script>
#12
<script type="text/javascript">
alimama_pid="mm_30977295_2853632_9914180";
alimama_type="g";
alimama_tks={};
alimama_tks.style_i=2;
alimama_tks.lg_i=1;
alimama_tks.w_i=572;
alimama_tks.h_i=69;
alimama_tks.btn_i=1;
alimama_tks.txt_s="连衣裙";
alimama_tks.hot_i=1;
alimama_tks.hc_c="0065FF";
alimama_tks.cid_i=0;
alimama_tks.t_i=1;
</script>
<script type="text/javascript" src="http://a.alimama.cn/inf.js"></script>
alimama_pid="mm_30977295_2853632_9914180";
alimama_type="g";
alimama_tks={};
alimama_tks.style_i=2;
alimama_tks.lg_i=1;
alimama_tks.w_i=572;
alimama_tks.h_i=69;
alimama_tks.btn_i=1;
alimama_tks.txt_s="连衣裙";
alimama_tks.hot_i=1;
alimama_tks.hc_c="0065FF";
alimama_tks.cid_i=0;
alimama_tks.t_i=1;
</script>
<script type="text/javascript" src="http://a.alimama.cn/inf.js"></script>
#1
#2
空指针,应该是session或trans未实例化吧,你检查检查。
#3
我觉得不是顺序的问题
#4
表中字段的数据类型没定义正确
#5
正解
#6
和你同样的问题呀!!!怎么解决?
#7
不是这样搞吧。好像HIBERNATE可以设置以对象来生成表的。 你去查查hibernate手册上面有的,
#8
有弄事务吗?。。。感觉怎么没有事务处理~
#9
哥们 我弄好了 你把Myeclipse项目中jre移除了 重新在windows属性里build 中加载自己安装的jdk就ok了
#10
<script type="text/javascript">
alimama_pid="mm_30977295_2853632_9914180";
alimama_type="g";
alimama_tks={};
alimama_tks.style_i=2;
alimama_tks.lg_i=1;
alimama_tks.w_i=572;
alimama_tks.h_i=69;
alimama_tks.btn_i=1;
alimama_tks.txt_s="连衣裙";
alimama_tks.hot_i=1;
alimama_tks.hc_c="0065FF";
alimama_tks.cid_i=0;
alimama_tks.t_i=1;
</script>
<script type="text/javascript" src="http://a.alimama.cn/inf.js"></script>
alimama_pid="mm_30977295_2853632_9914180";
alimama_type="g";
alimama_tks={};
alimama_tks.style_i=2;
alimama_tks.lg_i=1;
alimama_tks.w_i=572;
alimama_tks.h_i=69;
alimama_tks.btn_i=1;
alimama_tks.txt_s="连衣裙";
alimama_tks.hot_i=1;
alimama_tks.hc_c="0065FF";
alimama_tks.cid_i=0;
alimama_tks.t_i=1;
</script>
<script type="text/javascript" src="http://a.alimama.cn/inf.js"></script>
#11
<script type="text/javascript">
alimama_pid="mm_30977295_2853632_9914180";
alimama_type="g";
alimama_tks={};
alimama_tks.style_i=2;
alimama_tks.lg_i=1;
alimama_tks.w_i=572;
alimama_tks.h_i=69;
alimama_tks.btn_i=1;
alimama_tks.txt_s="连衣裙";
alimama_tks.hot_i=1;
alimama_tks.hc_c="0065FF";
alimama_tks.cid_i=0;
alimama_tks.t_i=1;
</script>
<script type="text/javascript" src="http://a.alimama.cn/inf.js"></script>
#12
<script type="text/javascript">
alimama_pid="mm_30977295_2853632_9914180";
alimama_type="g";
alimama_tks={};
alimama_tks.style_i=2;
alimama_tks.lg_i=1;
alimama_tks.w_i=572;
alimama_tks.h_i=69;
alimama_tks.btn_i=1;
alimama_tks.txt_s="连衣裙";
alimama_tks.hot_i=1;
alimama_tks.hc_c="0065FF";
alimama_tks.cid_i=0;
alimama_tks.t_i=1;
</script>
<script type="text/javascript" src="http://a.alimama.cn/inf.js"></script>
alimama_pid="mm_30977295_2853632_9914180";
alimama_type="g";
alimama_tks={};
alimama_tks.style_i=2;
alimama_tks.lg_i=1;
alimama_tks.w_i=572;
alimama_tks.h_i=69;
alimama_tks.btn_i=1;
alimama_tks.txt_s="连衣裙";
alimama_tks.hot_i=1;
alimama_tks.hc_c="0065FF";
alimama_tks.cid_i=0;
alimama_tks.t_i=1;
</script>
<script type="text/javascript" src="http://a.alimama.cn/inf.js"></script>