hibernate的真实的痛楚

真不知道是谁 2005-08-25 02:17:08
我的程序很让人受不了,在MySql成功,在oracle失败。
1.我先写了hibernate.properties.一是MySQL数据库,一是oracle数据库。
hibernate.dialect=net.sf.hibernate.dialect.MySQLDialect
hibernate.connection.driver_class=com.mysql.jdbc.Driver
hibernate.connection.url=jdbc:mysql://localhost:3306/SAMPLEDB
hibernate.connection.username=root
hibernate.connection.password=1234
hibernate.show_sql=true

hibernate.dialect=net.sf.hibernate.dialect.OracleDialect
hibernate.connection.driver_class=oracle.jdbc.driver.OracleDriver
hibernate.connection.url=jdbc:oracle:thin:@10.130.1.58:1521:appdb
hibernate.connection.username=mcode
hibernate.connection.password=appboco
hibernate.show_sql=true

2.写了hbm.xml文件

<?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="Province" table="hibernate_Province" dynamic-insert="true" dynamic-update="true">
<meta attribute="class-description">
Represents a single Province.
@author mc
</meta>

<meta attribute="class-scope">public</meta>

<id name="provid" type="long">
<generator class="increment" />
</id>

<property name="provname" type="string" >
<meta attribute="use-in-tostring">true</meta>
<column name="prov_name" />
</property>

<set
name="citys"
cascade="all-delete-orphan"
inverse="true"
>

<key column="key_prov_id" />
<one-to-many class="com.boco.hibernate.City" />
</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="City" table="hibernate_City" dynamic-insert="true" dynamic-update="true">
<meta attribute="class-description">
Represents a single City.
@author mc
</meta>

<meta attribute="class-scope">public</meta>

<id name="cityid" type="long">
<generator class="increment" />
</id>

<property name="cityname" type="string" >
<meta attribute="use-in-tostring">true</meta>
<column name="city_name" />
</property>

<many-to-one
name="prov"
column="key_prov_id"
class="com.boco.hibernate.Province"
cascade="save-update"
/>

<set
name="codebases"
cascade="all-delete-orphan"
inverse="true"
>

<key column="key_city_id" />
<one-to-many class="com.boco.hibernate.Codebase" />
</set>


</class>

</hibernate-mapping>


...全文
120 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
真不知道是谁 2005-08-25
  • 打赏
  • 举报
回复

谢谢688wl。问题OK了。
688wl 2005-08-25
  • 打赏
  • 举报
回复
这么多代码,没细看,报错表找不到,是不是hbm中的table名字的问题,试试全部大写
javaprogramlover 2005-08-25
  • 打赏
  • 举报
回复
我也有同样的遭遇,慰问一下!
真不知道是谁 2005-08-25
  • 打赏
  • 举报
回复
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。
真不知道是谁 2005-08-25
  • 打赏
  • 举报
回复
<?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>

67,512

社区成员

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

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