//User.javapackage com.sourpuss.hibernate.user;
import java.util.Date;
public class User {
private int id;
private String name;
private Date birthday;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
}
//User.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC 
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.sourpuss.hibernate.user">
<class name="User">
<id name="id">
<generator class="native"/>
</id>
<property name="name" />
<property name="birthday"/>
</class>
</hibernate-mapping>
//hibernate.cfg.xml
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"><hibernate-configuration>
<session-factory>
<property name="dialect">org.hibernate.dialect.OracleDialect</property>
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@localhost:1521:wgb</property>
<property name="connection.username">scott</property>
<property name="connection.password">tiger</property>
<mapping resource="com/sourpuss/hibernate/user/User.hbm.xml"/>

</session-factory>
</hibernate-configuration>//Test.java验证类
package com.sourpuss.hibernate.user;import java.util.Date;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;public class Test {
public static void main(String[] args) {
Configuration cfg=new Configuration();
cfg.configure();
SessionFactory sf=cfg.buildSessionFactory();

Session s=sf.openSession();
Transaction ts=s.beginTransaction();
User user=new User();
user.setName("sourpuss");
user.setBirthday(new Date());
user.setId(1);
s.save(user);
ts.commit();
s.close();
}
}此程序报错11:02:49,796  INFO Environment:514 - Hibernate 3.2.5
11:02:51,484 DEBUG AbstractEntityPersister:2204 - Inserting entity: [com.sourpuss.hibernate.user.User#5]
11:02:51,484 DEBUG AbstractBatcher:366 - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
11:02:51,484 DEBUG SQL:401 - insert into User (name, birthday, id) values (?, ?, ?)
11:02:51,484 DEBUG AbstractBatcher:484 - preparing statement
11:02:51,500 DEBUG AbstractEntityPersister:1992 - Dehydrating entity: [com.sourpuss.hibernate.user.User#5]
11:02:51,500 DEBUG AbstractBatcher:44 - Executing batch size: 1
11:02:51,500 DEBUG AbstractBatcher:374 - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
11:02:51,500 DEBUG AbstractBatcher:533 - closing statement
11:02:51,500 DEBUG JDBCExceptionReporter:69 - Could not execute JDBC batch update [insert into User (name, birthday, id) values (?, ?, ?)]
java.sql.BatchUpdateException: ORA-00903: 表名无效 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:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:237)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at com.sourpuss.hibernate.user.Test.main(Test.java:24)
11:02:51,515  WARN JDBCExceptionReporter:77 - SQL Error: 903, SQLState: 42000
11:02:51,515 ERROR JDBCExceptionReporter:78 - ORA-00903: 表名无效11:02:51,515  WARN JDBCExceptionReporter:77 - SQL Error: 903, SQLState: 42000
11:02:51,515 ERROR JDBCExceptionReporter:78 - ORA-00903: 表名无效11:02:51,515 ERROR AbstractFlushingEventListener:301 - Could not synchronize database state with session
org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:253)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:237)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at com.sourpuss.hibernate.user.Test.main(Test.java:24)
Caused by: java.sql.BatchUpdateException: ORA-00903: 表名无效 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:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
... 8 more
11:02:51,515 DEBUG ConnectionManager:478 - registering flush end
Exception in thread "main" org.hibernate.exception.SQLGrammarException: Could not execute JDBC batch update
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:253)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:237)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:298)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:1000)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:338)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
at com.sourpuss.hibernate.user.Test.main(Test.java:24)
Caused by: java.sql.BatchUpdateException: ORA-00903: 表名无效 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:48)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:246)
... 8 more

解决方案 »

  1.   

     User 表名无效,检查一下数据库中有没有这个表。
      

  2.   

    不好意思,拷贝的时候不小心丢了一句。
    我用的是数据库自动创建表。<property name="hbm2ddl.auto">create</property> hibernate.cfg.xml 是这样的<hibernate-configuration> 
    <session-factory> 
    <property name="dialect">org.hibernate.dialect.OracleDialect </property> 
    <property name="connection.driver_class">oracle.jdbc.driver.OracleDriver </property> 
    <property name="connection.url">jdbc:oracle:thin:@localhost:1521:wgb </property> 
    <property name="connection.username">scott </property> 
    <property name="connection.password">tiger </property> 
    <property name="hbm2ddl.auto">create</property>
    <mapping resource="com/sourpuss/hibernate/user/User.hbm.xml"/> </session-factory> 
    </hibernate-configuration> 
    麻烦再看下,谢谢
      

  3.   

    2楼,谢谢,
    com.sourpuss.hibernate.user 是我在src中创建的一个包,所有程序都在这个包内,我想不应该是个这吧?
      

  4.   

    我看别人用的好像是<property name="hibernate.hbm2ddl.auto" value="create" />
    不是你的<property name="hbm2ddl.auto">create </property> 
      

  5.   

    <hibernate-mapping package="com.sourpuss.hibernate.user">
    改成
    <hibernate-mapping package="com.sourpuss.hibernate.User"> 
    把测试类中的user.setId(1);去掉
    你ID用的是native就不能显式赋值,Oracle会生成一个序列号,自动增长
      

  6.   

    改过之后出现以下错误!Exception in thread "main" org.hibernate.InvalidMappingException: Could not parse mapping document from resource com/sourpuss/hibernate/user/User.hbm.xml
    at org.hibernate.cfg.Configuration.addResource(Configuration.java:569)
    at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1587)
    at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1555)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1534)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1508)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1428)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1414)
    at com.sourpuss.hibernate.user.Test.main(Test.java:12)
    Caused by: org.hibernate.MappingException: class com.sourpuss.hibernate.User not found while looking for property: id
    at org.hibernate.util.ReflectHelper.reflectedPropertyClass(ReflectHelper.java:74)
    at org.hibernate.mapping.SimpleValue.setTypeUsingReflection(SimpleValue.java:276)
    at org.hibernate.cfg.HbmBinder.bindSimpleId(HbmBinder.java:401)
    at org.hibernate.cfg.HbmBinder.bindRootPersistentClassCommonValues(HbmBinder.java:334)
    at org.hibernate.cfg.HbmBinder.bindRootClass(HbmBinder.java:273)
    at org.hibernate.cfg.HbmBinder.bindRoot(HbmBinder.java:144)
    at org.hibernate.cfg.Configuration.add(Configuration.java:669)
    at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:504)
    at org.hibernate.cfg.Configuration.addResource(Configuration.java:566)
    ... 7 more
    Caused by: java.lang.ClassNotFoundException: com.sourpuss.hibernate.User
    at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:164)
    at org.hibernate.util.ReflectHelper.classForName(ReflectHelper.java:100)
    at org.hibernate.util.ReflectHelper.reflectedPropertyClass(ReflectHelper.java:70)
    ... 15 more
      

  7.   

    应当就是hbm2ddl.auto写错了,应该为hibernate.hbm2ddl.auto
      

  8.   

    User.hbm.xml中没有指定tableJava code//User.hbm.xml
    <?xml version="1.0"?>
    <!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
    <hibernate-mapping package="com.sourpuss.hibernate.user">
    <class name="User" table="你的表名">
    <id name="id">
    <generator class="native"/>
    </id>
    <property name="name" />
    <property name="birthday"/>
    </class>
    </hibernate-mapping> 
      

  9.   

    oracle中不能用user作表名,它自己有一个,专用的!!~
      

  10.   

    把测试类中的user.setId(1);去掉
    你ID用的是native就不能显式赋值,Oracle会生成一个序列号,自动增长
      

  11.   

    去掉了user.setId(1);还是有错!Exception in thread "main" org.hibernate.InvalidMappingException: Could not parse mapping document from resource com/sourpuss/hibernate/user/User.hbm.xml
    at org.hibernate.cfg.Configuration.addResource(Configuration.java:569)
    at org.hibernate.cfg.Configuration.parseMappingElement(Configuration.java:1587)
    at org.hibernate.cfg.Configuration.parseSessionFactory(Configuration.java:1555)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1534)
    at org.hibernate.cfg.Configuration.doConfigure(Configuration.java:1508)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1428)
    at org.hibernate.cfg.Configuration.configure(Configuration.java:1414)
    at com.sourpuss.hibernate.user.Test.main(Test.java:12)
    Caused by: org.hibernate.MappingException: class com.sourpuss.hibernate.User not found while looking for property: id
    at org.hibernate.util.ReflectHelper.reflectedPropertyClass(ReflectHelper.java:74)
    at org.hibernate.mapping.SimpleValue.setTypeUsingReflection(SimpleValue.java:276)
    at org.hibernate.cfg.HbmBinder.bindSimpleId(HbmBinder.java:401)
    at org.hibernate.cfg.HbmBinder.bindRootPersistentClassCommonValues(HbmBinder.java:334)
    at org.hibernate.cfg.HbmBinder.bindRootClass(HbmBinder.java:273)
    at org.hibernate.cfg.HbmBinder.bindRoot(HbmBinder.java:144)
    at org.hibernate.cfg.Configuration.add(Configuration.java:669)
    at org.hibernate.cfg.Configuration.addInputStream(Configuration.java:504)
    at org.hibernate.cfg.Configuration.addResource(Configuration.java:566)
    ... 7 more
    Caused by: java.lang.ClassNotFoundException: com.sourpuss.hibernate.User
    at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:164)
    at org.hibernate.util.ReflectHelper.classForName(ReflectHelper.java:100)
    at org.hibernate.util.ReflectHelper.reflectedPropertyClass(ReflectHelper.java:70)
    ... 15 more
      

  12.   


    经验证不是这个问题。这个hibernate可以省略的。
      

  13.   

    把你的com/sourpuss/hibernate/user/User.hbm.xml 贴出来看一下
    它说无法解析这个文件!!~
      

  14.   

    <?xml version="1.0"?> 
    <!DOCTYPE hibernate-mapping PUBLIC 
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN" 
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"> 
    <hibernate-mapping package="com.sourpuss.hibernate.user"> 
    <class name="User"> 
    <id name="id"> 
    <generator class="native"/> 
    </id> 
    <property name="name" /> 
    <property name="birthday"/> 
    </class> 
    </hibernate-mapping> 
    帮忙看看,谢
      

  15.   

    <id name="id" column="id">
       <generator class="sequence">
    <param name="sequence">SEQ_id</param>
      </generator>
    </id>
      

  16.   

    哈哈,谢谢大家的指点,问题已经解决了,oracle 数据库中不能用user做表名,我以后会注意这些命名的问题了。现在结帖给分…………
      

  17.   


    哈哈,和我报的错一样,谢谢!
    我也用的是User
    都是网上看的例子,太懒,什么都没改