新手请教Hibernate问题。

wwhui 2008-11-25 11:46:20
刚开始学习Hibernate,搞不懂为什么出错,请知道的说下,谢拉。

错误信息:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.
Hibernate: insert into PERSON (NAME, PASSWORD, ID) values (?, ?, ?)
Exception in thread "main" org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:202)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:235)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:139)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:297)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:985)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:333)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at tt.TableOperate.insert(TableOperate.java:24)
at tt.TestHiberate.main(TestHiberate.java:21)
Caused by: java.sql.BatchUpdateException: ORA-01401: 列に挿入した値が大きすぎます。

at oracle.jdbc.dbaccess.DBError.throwBatchUpdateException(DBError.java:459)
at oracle.jdbc.driver.OraclePreparedStatement.executeBatch(OraclePreparedStatement.java:3907)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:195)
... 9 more


Person.hbm.xml:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd>
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="tt.Person" table="PERSON">
<id name="id" column="ID" type="string">
<generator class="uuid.hex"/>
</id>

<property name="name" column="NAME" type="string" />
<property name="password" column="PASSWORD" type="string"/>
</class>
</hibernate-mapping>


hibernate.cfg.xml:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>

<session-factory>
<property name="connection.username">TEST2</property>
<property name="connection.url">

</property>
<property name="dialect">

</property>
<property name="myeclipse.connection.profile">

</property>
<property name="connection.password">TEST2</property>
<property name="connection.driver_class">

</property>
<property name="show_sql">true</property>
<mapping resource="tt/Person.hbm.xml" />

</session-factory>

</hibernate-configuration>

...全文
121 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
honglz1004 2010-04-07
  • 打赏
  • 举报
回复
请问你是怎么解决的 我也出现了这个问题
wwhui 2008-12-01
  • 打赏
  • 举报
回复
已经解决啦,非常感谢大家。
wenjjing2lianee 2008-12-01
  • 打赏
  • 举报
回复

<id name="id" column="ID" type="string">
<generator class="uuid.hex"/>
</id>
//不应手动插入ID
yangfuchao418 2008-12-01
  • 打赏
  • 举报
回复
ConstraintViolationException,它报的时候违反约束异常。你是不是设置了主外建关系,违反了约束?或你插入的时候肯定关联了其他的东西,这就要看你的关联关系了。有个时候并不是你的这一步有问题,而是上一步已经存在隐性错误了。最好寻根究底。
wwhui 2008-11-27
  • 打赏
  • 举报
回复
这个SQL语句是我要Hibernate打出来的,实际的HQL语句不是这样的。
rmjs18 2008-11-27
  • 打赏
  • 举报
回复
HQL语句啊,你怎么能写SQL语句呢?
Lucky丶華少灬 2008-11-27
  • 打赏
  • 举报
回复
hibernate框架是不支持sql语句的,你那样写是不对的.
hibernate只支持hql语句.

所以你的insert into PERSON (NAME, PASSWORD, ID) values (?, ?, ?)
这句话应该换成getHibernateTemplate().save(实体对象[Person]);
harryzyp 2008-11-26
  • 打赏
  • 举报
回复
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).

不是没有配置Log4j么
jianpc 2008-11-26
  • 打赏
  • 举报
回复
不好意思,没看完整。。。
java.sql.BatchUpdateException: ORA-01401: 列に挿入した値が大きすぎます。
看看是不是字段长度不够。。
jianpc 2008-11-26
  • 打赏
  • 举报
回复
PERSON 不要设置ID,你的生成策略已经是UUID了,Hibernate会给你搞定。
sunjiyun2007 2008-11-26
  • 打赏
  • 举报
回复
不能执行update,为什么得看源代码了
g3123298 2008-11-26
  • 打赏
  • 举报
回复

Exception in thread "main" org.hibernate.exception.ConstraintViolationException:
Could not execute JDBC batch update
没有找到JDBC 没有更新

Hibernate: insert into PERSON (NAME, PASSWORD, ID) values (?, ?, ?)
所以您的数据insert不进去


<id name="id" column="ID" type="string">
<generator class="uuid.hex"/>
</id>
把class="uuid.hex"

设置成为class="native"


http://docs.huihoo.com/hibernate/reference-v3_zh-cn/associations.html
这里有详细的native和 assigned自动标识列


若您手手动分配的用assigned
自动生成的用native
DiscussQuestions 2008-11-26
  • 打赏
  • 举报
回复
楼上说得没有错!~!
<id name="id" column="ID" type="string">
<generator class="uuid.hex"/>
</id>

既然你的主键是uuid.hex类型的,那就没有那个必要手动去设置~!
你还可以把class = "uuid.hex"改成class="assigned"也就不会有问题了!~!
jianpc 2008-11-26
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 wwhui 的回复:]
非常感谢各位的回答:

首先不是插入值的长度的问题,我已经试过了.
其次代码是没有问题的,我可以执行查询,删除,更新等操作,但是就是不能插入数据.
ID是数据库里面存在的列,如果不设置ID的话,是不能插入的.
[/Quote]

看你的配置,ID是自动生成的32位字符串,在代码中是不需要调用pojo的setId()方法的(如果需要手动设置,配置uuid.hex有什么意义呢?)。你看看数据库里面ID字段是多少位的,代码中有setId的操作的话,删掉。

试试,用代码来验证是最好的方法:)
wwhui 2008-11-26
  • 打赏
  • 举报
回复
非常感谢各位的回答:

首先不是插入值的长度的问题,我已经试过了.
其次代码是没有问题的,我可以执行查询,删除,更新等操作,但是就是不能插入数据.
ID是数据库里面存在的列,如果不设置ID的话,是不能插入的.

67,515

社区成员

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

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