hibernate插入数据,表不存在异常

a1522609810 2011-11-09 06:48:43
我是一个java学员,还有很多需要我学习的东西:今天遇到一个问题,我再学校写得项目拿回家一运行,就报了一个莫名的错误,hibernate自动建表时报错说Table 'test.xxx'doesn't exist,我在Xxx(这是一个VO对象).hbm.xml中明明配置的是没有指定表名,应给用xxx做表名啊,但是咋就成了test(这是我使用mysql的默认测试数据库名).xxx了呢?请教过来人指点。
...全文
202 18 打赏 收藏 转发到动态 举报
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
杀猪剑客 2011-11-14
  • 打赏
  • 举报
回复
一看就知道没学过hibernate
[Quote=引用 16 楼 exc_9521 的回复:]
<property name="hibernate.hbm2ddl.auto">update</property>
把它改成 create
[/Quote]
Exc_9521 2011-11-14
  • 打赏
  • 举报
回复
<property name="hibernate.hbm2ddl.auto">update</property>
把它改成 create
a1522609810 2011-11-14
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 sky402101 的回复:]
你启动服务器应该有错误吧?
[/Quote]没呀!
wudi1233 2011-11-14
  • 打赏
  • 举报
回复
他的意思是 你的mysql数据库里应该是没有test这个库 不是你的配置文件里没有写这个库名
sky402101 2011-11-13
  • 打赏
  • 举报
回复
你启动服务器应该有错误吧?
a1522609810 2011-11-12
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 taodengwen 的回复:]
test数据库里面没有xxx表格
[/Quote]废话不是,我就是要让hibernate为我自动建表,预先有这个表我我这个功能不是没了啊?实际中确实是要先建出来表,但是我不是还在学习期么,这个功能不能没有。
  • 打赏
  • 举报
回复
test数据库里面没有xxx表格
a1522609810 2011-11-12
  • 打赏
  • 举报
回复
其实这个问题我已经通过其他方法绕过了,项目能继续往下写,但是发生的这个故障让我不敢懈怠,万一有下次呢?请大家再多指点一二。
我说的“通过其他方法避免了这个错误”,这句话相信大家也注意到了,我的做法是这样的————
由于我有其他项目(也是用hibernate自动建表的)中用到的表和这个一样,于是启动其他项目把表建出来之后我再运行我这个项目这个报错就没了。我检查了连个项目之间的区别,hibernate.cfg.xml和VO映射都一样,jar包也换掉,再删掉数据库表重新运行可是这个项目它就是报错啊!
a1522609810 2011-11-12
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 oxcow 的回复:]
引用 8 楼 a1522609810 的回复:

这是我的报错信息,第一行是底层的sql语句
Hibernate: insert into t_useraccount (username, password, usertype, usernickname) values (?, ?, ?, ?)
Exception in thread "main" org.hibernate.except……
[/Quote]
我这次改成Create了,报错还一样
oxcow 2011-11-11
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 a1522609810 的回复:]

这是我的报错信息,第一行是底层的sql语句
Hibernate: insert into t_useraccount (username, password, usertype, usernickname) values (?, ?, ?, ?)
Exception in thread "main" org.hibernate.exception.SQLGrammarException: c……
[/Quote]

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'test.t_useraccount' doesn't exist


这里的“建表也不是由我负责”是指表是由hibernate自动创建的? 如果是这样,那你好歹也看下数据库有表没?如果你是指是由其他同事负责的,那你也检查下数据库有没有。其实报错信息已经和明白来,就不要纠结什么hibernate包冲突什么的!

建立你在第一次运行项目时,如果不愿意手工建表,而是使用hibernate反向建表,那你最好把 <property name="hibernate.hbm2ddl.auto">update</property>设置成create,第二次运行时在改成false或者update。

a1522609810 2011-11-11
  • 打赏
  • 举报
回复
这是我的报错信息,第一行是底层的sql语句
Hibernate: insert into t_useraccount (username, password, usertype, usernickname) values (?, ?, ?, ?)
Exception in thread "main" org.hibernate.exception.SQLGrammarException: could not insert: [dazzleCN.mysql.jyz.vo.UserAccount]
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:92)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:64)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2345)
at org.hibernate.persister.entity.AbstractEntityPersister.insert(AbstractEntityPersister.java:2852)
at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:71)
at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:273)
at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:320)
at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:203)
at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:129)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.saveWithGeneratedOrRequestedId(DefaultSaveOrUpdateEventListener.java:210)
at org.hibernate.event.def.DefaultSaveEventListener.saveWithGeneratedOrRequestedId(DefaultSaveEventListener.java:56)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.entityIsTransient(DefaultSaveOrUpdateEventListener.java:195)
at org.hibernate.event.def.DefaultSaveEventListener.performSaveOrUpdate(DefaultSaveEventListener.java:50)
at org.hibernate.event.def.DefaultSaveOrUpdateEventListener.onSaveOrUpdate(DefaultSaveOrUpdateEventListener.java:93)
at org.hibernate.impl.SessionImpl.fireSave(SessionImpl.java:713)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:701)
at org.hibernate.impl.SessionImpl.save(SessionImpl.java:697)
at dazzleCN.mysql.jyz.Module.VoDaoModule.IVObjectDaoImpl.doInsert(IVObjectDaoImpl.java:27)
at dazzleCN.mysql.jyz.Module.VoDaoModule.IVObjectDaoProxy.doInsert(IVObjectDaoProxy.java:25)
at dazzleCN.mysql.jyz.test.TestDataClass.main(TestDataClass.java:21)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'test.t_useraccount' doesn't exist
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
at com.mysql.jdbc.Util.getInstance(Util.java:386)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1052)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3597)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3529)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1990)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2151)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2625)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2119)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2415)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2333)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2318)
at org.hibernate.id.IdentityGenerator$GetGeneratedKeysDelegate.executeAndExtract(IdentityGenerator.java:94)
at org.hibernate.id.insert.AbstractReturningDelegate.performInsert(AbstractReturningDelegate.java:57)
... 18 more
这是我对象映射文件的配置

<hibernate-mapping package="dazzleCN.mysql.jyz.vo">
<class name="UserAccount" table="t_useraccount">
<id name="id" column="userid">
<generator class="native"></generator>
</id>
<property name="username" not-null="true"></property>
<property name="password" not-null="true"></property>
<property name="usertype" not-null="true"></property>
<property name="usernickname" not-null="true"></property>
</class>
</hibernate-mapping>

我觉着我的代码没错啊,hibernate底层的sql语句执行后才报错的,而建表也不是由我负责,是不是hibernate有问题,比如说jar包,这方面有经验的人帮帮忙啊。
a1522609810 2011-11-11
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 jya126 的回复:]
因为你没有指定数据库,mysql默认配置test数据库,就找不到这个表了
[/Quote]
这不是有么

<hibernate-configuration>
<session-factory >
<property name="hibernate.connection.url">jdbc:mysql:///test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.show_sql">true</property>
<mapping resource="dazzleCN/mysql/jyz/vo/UserAccount.hbm.xml" />
<mapping resource="dazzleCN/mysql/jyz/vo/UserInfo.hbm.xml" />
<mapping resource="dazzleCN/mysql/jyz/vo/Commodity.hbm.xml" />
</session-factory>
</hibernate-configuration>
a1522609810 2011-11-11
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 a261888667 的回复:]
hibernate.cfg.xml中 hibernate.hbm2ddl.auto 配置的是什么?
最好自己创建数据库
[/Quote]
咋没指定啊

<hibernate-configuration>
<session-factory >
<property name="hibernate.connection.url">jdbc:mysql:///test</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">root</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQLInnoDBDialect</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<property name="hibernate.show_sql">true</property>
<mapping resource="dazzleCN/mysql/jyz/vo/UserAccount.hbm.xml" />
<mapping resource="dazzleCN/mysql/jyz/vo/UserInfo.hbm.xml" />
<mapping resource="dazzleCN/mysql/jyz/vo/Commodity.hbm.xml" />
</session-factory>
</hibernate-configuration>
gengfeng111 2011-11-11
  • 打赏
  • 举报
回复
zou
jya126 2011-11-10
  • 打赏
  • 举报
回复
因为你没有指定数据库,mysql默认配置test数据库,就找不到这个表了
a261888667 2011-11-10
  • 打赏
  • 举报
回复
hibernate.cfg.xml中 hibernate.hbm2ddl.auto 配置的是什么?
最好自己创建数据库
a1522609810 2011-11-10
  • 打赏
  • 举报
回复
是的,这个mysql的测试数据库,我在程序里一直连这个库
oxcow 2011-11-09
  • 打赏
  • 举报
回复
[Quote=引用楼主 a1522609810 的回复:]
我是一个java学员,还有很多需要我学习的东西:今天遇到一个问题,我再学校写得项目拿回家一运行,就报了一个莫名的错误,hibernate自动建表时报错说Table 'test.xxx'doesn't exist,我在Xxx(这是一个VO对象).hbm.xml中明明配置的是没有指定表名,应给用xxx做表名啊,但是咋就成了test(这是我使用mysql的默认测试数据库名).xxx了呢?请教过来人指点。……
[/Quote]

test应该是你的数据库名。 你应该看下你本地的数据库中是否有一个叫test的数据库。

67,513

社区成员

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

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