<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE hibernate-mapping
    PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping package="com.boco.hibernate">    <class name="com.boco.hibernate.Codebase" table="hibernate_Codebase" dynamic-insert="true" dynamic-update="true">
    <meta attribute="class-description">
      Represents a single Codebase.
      @author mc
    </meta>     <meta attribute="class-scope">public</meta>        <id name="codebaseid" type="long">
            <generator class="increment" />
        </id>     <property name="brandname" type="string" >
      <meta attribute="use-in-tostring">true</meta>
      <column name="brand_name" />
    </property>     <property name="codebase" type="string" >
      <meta attribute="use-in-tostring">true</meta>
      <column name="codebase" />
    </property> <many-to-one
    name="city"
    column="key_city_id"
    class="com.boco.hibernate.City"
    cascade="save-update"
/>     <set
        name="codes"
        cascade="all-delete-orphan" 
        inverse="true"
        >
        
        <key column="key_codebase_id" />
        <one-to-many class="com.boco.hibernate.Code" />
     </set>
    </class></hibernate-mapping><?xml version="1.0" encoding="GBK"?>
<!DOCTYPE hibernate-mapping
    PUBLIC "-//Hibernate/Hibernate Mapping DTD//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">

<hibernate-mapping package="com.boco.hibernate">    <class name="Code" table="hibernate_Code" dynamic-insert="true" dynamic-update="true">
    <meta attribute="class-description">
      Represents a single Code.
      @author mc
    </meta>     <meta attribute="class-scope">public</meta>        <id name="codeid" type="long">
            <generator class="increment" />
        </id>     <property name="code" type="string" >
      <meta attribute="use-in-tostring">true</meta>
      <column name="code" />
    </property> <many-to-one
    name="codebase"
    column="key_codebase_id"
    class="com.boco.hibernate.Codebase"
    cascade="save-update"
/>    </class></hibernate-mapping>
3.写了build.xml文件。<?xml version="1.0"?>
<project name="Test Hibernate" default="prepare" basedir=".">  <!-- Set up properties containing important project directories -->
  <property name="source.root" value="src"/>
  <property name="class.root" value="classes"/>
  <property name="lib.dir" value="lib"/>
  <property name="schema.dir" value="schema"/>  <!-- Set up the class path for compilation and execution -->
  <path id="project.class.path">
      <!-- Include our own classes, of course -->
      <pathelement location="${class.root}" />
      <!-- Include jars in the project library directory -->
      <fileset dir="${lib.dir}">
        <include name="*.jar"/>
      </fileset>
  </path>  <!-- Create our runtime subdirectories and copy resources into them -->
  <target name="prepare" description="Sets up build structures">
    <delete dir="${class.root}"/>
    <mkdir dir="${class.root}"/>    <!-- Copy our property files and O/R mappings for use at runtime -->
    <copy todir="${class.root}" >
      <fileset dir="${source.root}" >
        <include name="**/*.properties"/>
        <include name="**/*.hbm.xml"/>
        <include name="**/*.cfg.xml"/>
      </fileset>
    </copy>
  </target>
  <!-- Generate the java code for all mapping files in our source tree -->
  <target name="codegen" depends="prepare"
          description="Generate Java source from the O/R mapping files">
    <!-- Teach Ant how to use Hibernate's code generation tool -->
    <taskdef name="hbm2java"
           classname="net.sf.hibernate.tool.hbm2java.Hbm2JavaTask"
           classpathref="project.class.path"/>    <hbm2java output="${source.root}">
      <fileset dir="${source.root}">
        <include name="**/*.hbm.xml"/>
      </fileset>
    </hbm2java>
  </target> <!-- Compile the java source of the project -->
  <target name="compile" depends="codegen"
          description="Compiles all Java classes">
    <javac srcdir="${source.root}"
           destdir="${class.root}"
           debug="on"
           optimize="off"
           deprecation="on">
      <classpath refid="project.class.path"/>
    </javac>
  </target> 
  <!-- Generate the schemas for all mapping files in our class tree -->
  <target name="schema" depends="compile"
          description="Generate DB schema from the O/R mapping files">    <!-- Teach Ant how to use Hibernate's schema generation tool -->
    <taskdef name="schemaexport"
             classname="net.sf.hibernate.tool.hbm2ddl.SchemaExportTask"
             classpathref="project.class.path"/>    <schemaexport properties="${class.root}/hibernate.properties"
                  quiet="no" text="no" drop="no" output="schema/sampledb.sql" delimiter=";">
      <fileset dir="${class.root}">
        <include name="**/*.hbm.xml"/>
      </fileset>
    </schemaexport>
  </target> <target name="run" description="Run a Hibernate sample"
   depends="schema">
    <java classname="com.boco.hibernate.TestHibernateService" fork="true">
       <classpath refid="project.class.path"/>
    </java>
 </target></project>

解决方案 »

  1.   

    4.结果:在MySQL可以。在oracle中不行,但是在oracle中4个表都有了,既:hibernate_province、hibernate_city、hibernate_codebase、hibernate_code。结果提示如下:......
    ......
    run:
         [java] (cfg.Environment                     478 ) Hibernate 2.1.7
         [java] (cfg.Environment                     512 ) loaded properties from resource hibernate.properties: {hibernate.connection.username=app, hibernate.connection.password=app, hibernate.cglib.use_reflection_optimizer=true, hibernate.dialect=net.sf.hibernate.dialect.OracleDialect, hibernate.show_sql=true, hibernate.connection.url=jdbc:oracle:thin:@10.130.1.58:1521:appdb, hibernate.connection.driver_class=oracle.jdbc.driver.OracleDriver}
         [java] (cfg.Environment                     538 ) using CGLIB reflection optimizer
         [java] (cfg.Environment                     567 ) using JDK 1.4 java.sql.Timestamp handling
         [java] (cfg.Configuration                   350 ) Mapping resource: com/boco/hibernate/Province.hbm.xml
         [java] (cfg.Binder                          230 ) Mapping class: com.boco.hibernate.Province -> hibernate_Province
         [java] (cfg.Configuration                   350 ) Mapping resource: com/boco/hibernate/City.hbm.xml
         [java] (cfg.Binder                          230 ) Mapping class: com.boco.hibernate.City -> hibernate_City
         [java] (cfg.Configuration                   350 ) Mapping resource: com/boco/hibernate/Codebase.hbm.xml
         [java] (cfg.Binder                          230 ) Mapping class: com.boco.hibernate.Codebase -> hibernate_Codebase
         [java] (cfg.Configuration                   350 ) Mapping resource: com/boco/hibernate/Code.hbm.xml
         [java] (cfg.Binder                          230 ) Mapping class: com.boco.hibernate.Code -> hibernate_Code
         [java] (cfg.Configuration                   632 ) processing one-to-many association mappings
         [java] (cfg.Binder                          1182) Mapping collection: com.boco.hibernate.Province.citys -> hibernate_City
         [java] (cfg.Binder                          1182) Mapping collection: com.boco.hibernate.City.codebases -> hibernate_Codebase
         [java] (cfg.Binder                          1182) Mapping collection: com.boco.hibernate.Codebase.codes -> hibernate_Code
         [java] (cfg.Configuration                   641 ) processing one-to-one association property references
         [java] (cfg.Configuration                   666 ) processing foreign key constraints
         [java] (dialect.Dialect                     86  ) Using dialect: net.sf.hibernate.dialect.OracleDialect
         [java] (cfg.SettingsFactory                 74  ) Use outer join fetching: true
         [java] (connection.DriverManagerConnectionProvider 42  ) Using Hibernate built-in connection pool (not for production use!)
         [java] (connection.DriverManagerConnectionProvider 43  ) Hibernate connection pool size: 20
         [java] (connection.DriverManagerConnectionProvider 77  ) using driver: oracle.jdbc.driver.OracleDriver at URL: jdbc:oracle:thin:@10.130.1.58:1521:appdb
         [java] (connection.DriverManagerConnectionProvider 78  ) connection properties: {user=app, password=app}
         [java] (transaction.TransactionManagerLookupFactory 33  ) No TransactionManagerLookup configured (in JTA environment, use of process level read-write cache is not recommended)
         [java] (cfg.SettingsFactory                 114 ) Use scrollable result sets: true
         [java] (cfg.SettingsFactory                 117 ) Use JDBC3 getGeneratedKeys(): false
         [java] (cfg.SettingsFactory                 120 ) Optimize cache for minimal puts: false
         [java] (cfg.SettingsFactory                 126 ) echoing all SQL to stdout
         [java] (cfg.SettingsFactory                 129 ) Query language substitutions: {}
         [java] (cfg.SettingsFactory                 140 ) cache provider: net.sf.hibernate.cache.EhCacheProvider
         [java] (cfg.Configuration                   1121) instantiating and configuring caches
         [java] (impl.SessionFactoryImpl             119 ) building session factory
         [java] (impl.SessionFactoryObjectFactory    82  ) Not binding factory to JNDI, no JNDI name configured
         [java] 1
         [java] 2
         [java] (util.JDBCExceptionReporter          57  ) SQL Error: 942, SQLState: 42000
         [java] (util.JDBCExceptionReporter          58  ) ORA-00942: 表或视图不存在
         [java] 5
         [java] 6
         [java] net.sf.hibernate.exception.SQLGrammarException: Could not save object
         [java] at net.sf.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:69)
         [java] at net.sf.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:30)
         [java] at net.sf.hibernate.impl.SessionImpl.convert(SessionImpl.java:4110)
         [java] at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:792)
         [java] at net.sf.hibernate.impl.SessionImpl.save(SessionImpl.java:747)
         [java] at com.boco.hibernate.TestHibernateService.saveProvince(TestHibernateService.java:44)
         [java] at com.boco.hibernate.TestHibernateService.test(TestHibernateService.java:89)
         [java] at com.boco.hibernate.TestHibernateService.main(TestHibernateService.java:171)
         [java] Caused by: java.sql.SQLException: ORA-00942: 表或视图不存在
         [java] at oracle.jdbc.dbaccess.DBError.throwSqlException(DBError.java:168)
         [java] at oracle.jdbc.ttc7.TTIoer.processError(TTIoer.java:208)
         [java] at oracle.jdbc.ttc7.Oall7.receive(Oall7.java:543)
         [java] at oracle.jdbc.ttc7.TTC7Protocol.doOall7(TTC7Protocol.java:1405)
         [java] at oracle.jdbc.ttc7.TTC7Protocol.parseExecuteDescribe(TTC7Protocol.java:643)
         [java] at oracle.jdbc.driver.OracleStatement.doExecuteQuery(OracleStatement.java:1674)
         [java] at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1870)
         [java] at oracle.jdbc.driver.OraclePreparedStatement.executeUpdate(OraclePreparedStatement.java:363)
         [java] at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:314)
         [java] at net.sf.hibernate.id.IncrementGenerator.getNext(IncrementGenerator.java:68)
         [java] at net.sf.hibernate.id.IncrementGenerator.generate(IncrementGenerator.java:42)
         [java] at net.sf.hibernate.impl.SessionImpl.saveWithGeneratedIdentifier(SessionImpl.java:774)
         [java] ... 4 more
         [java] Exception in thread "main"
         [java] Java Result: 1
    BUILD SUCCESSFUL结果:在MySQL可以。在oracle中不行,但是在oracle中4个表都有了,既:hibernate_province、hibernate_city、hibernate_codebase、hibernate_code。
      

  2.   

    这么多代码,没细看,报错表找不到,是不是hbm中的table名字的问题,试试全部大写