我是hibernate的新手,并尝试在oracle db中映射已创建的表。在下面给出的代码中:
Session session = sessionFactory.openSession();
Transaction transaction = session.beginTransaction();
String hql = "FROM CrbtSubMasterDemo where rownum<20";
Query query = session.createQuery(hql);
List itr = query.list();
session.getTransaction().commit();
for (Iterator iterator = itr.iterator(); iterator.hasNext();)
{
System.out.println("[" + iterator.next() + "]");
}
当我运行此代码时。它首先给出了这个例外:
Apr 26, 2016 8:10:47 PM org.hibernate.tool.schema.extract.internal.InformationExtractorJdbcDatabaseMetaDataImpl processGetTableResults
INFO: HHH000262: Table not found: sdp.crbt_subscriber_master
Hibernate: create table sdp.crbt_subscriber_master (MSISDN varchar2(255) not null, STATUS varchar2(255), DATE_REGISTERED date, PLAN_INDICATOR number(10,0), RBT_CODE number(10,0), PASSWORD varchar2(255), TPIN varchar2(255), FREE_EVENTS_USED number(10,0), BLACK_LISTED number(10,0), LANGUAGE number(10,0), DEFAULT_GROUP_SETTING varchar2(255), DEFAULT_SINGLE_SETTING varchar2(255), DATE_SETTING_VALIDITY number(10,0), IMSI varchar2(255), LAST_CHARGED date, IS_MONTHLY_CHARGEABLE varchar2(255), CORP_ID number(10,0), SUB_TYPE varchar2(255), RENEW_MODE number(10,0), EXPIRY_DATE date, ACTIVE_FEATURES number(10,0), IN_USE_RBT number(10,0), UPDATE_TIME date, CORP_EXPIRY date, primary key (MSISDN))
Apr 26, 2016 8:10:47 PM org.hibernate.tool.schema.internal.ExceptionHandlerLoggedImpl handleException
WARN: GenerationTarget encountered exception accepting command : Unable to execute command [create table sdp.crbt_subscriber_master (MSISDN varchar2(255) not null, STATUS varchar2(255), DATE_REGISTERED date, PLAN_INDICATOR number(10,0), RBT_CODE number(10,0), PASSWORD varchar2(255), TPIN varchar2(255), FREE_EVENTS_USED number(10,0), BLACK_LISTED number(10,0), LANGUAGE number(10,0), DEFAULT_GROUP_SETTING varchar2(255), DEFAULT_SINGLE_SETTING varchar2(255), DATE_SETTING_VALIDITY number(10,0), IMSI varchar2(255), LAST_CHARGED date, IS_MONTHLY_CHARGEABLE varchar2(255), CORP_ID number(10,0), SUB_TYPE varchar2(255), RENEW_MODE number(10,0), EXPIRY_DATE date, ACTIVE_FEATURES number(10,0), IN_USE_RBT number(10,0), UPDATE_TIME date, CORP_EXPIRY date, primary key (MSISDN))]
org.hibernate.tool.schema.spi.CommandAcceptanceException: Unable to execute command [create table sdp.crbt_subscriber_master (MSISDN varchar2(255) not null, STATUS varchar2(255), DATE_REGISTERED date, PLAN_INDICATOR number(10,0), RBT_CODE number(10,0), PASSWORD varchar2(255), TPIN varchar2(255), FREE_EVENTS_USED number(10,0), BLACK_LISTED number(10,0), LANGUAGE number(10,0), DEFAULT_GROUP_SETTING varchar2(255), DEFAULT_SINGLE_SETTING varchar2(255), DATE_SETTING_VALIDITY number(10,0), IMSI varchar2(255), LAST_CHARGED date, IS_MONTHLY_CHARGEABLE varchar2(255), CORP_ID number(10,0), SUB_TYPE varchar2(255), RENEW_MODE number(10,0), EXPIRY_DATE date, ACTIVE_FEATURES number(10,0), IN_USE_RBT number(10,0), UPDATE_TIME date, CORP_EXPIRY date, primary key (MSISDN))]
at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:63)
at org.hibernate.tool.schema.internal.SchemaMigratorImpl.applySqlString(SchemaMigratorImpl.java:567)
at org.hibernate.tool.schema.internal.SchemaMigratorImpl.applySqlStrings(SchemaMigratorImpl.java:551)
at org.hibernate.tool.schema.internal.SchemaMigratorImpl.createTable(SchemaMigratorImpl.java:339)
at org.hibernate.tool.schema.internal.SchemaMigratorImpl.performMigration(SchemaMigratorImpl.java:257)
at org.hibernate.tool.schema.internal.SchemaMigratorImpl.doMigration(SchemaMigratorImpl.java:137)
at org.hibernate.tool.schema.internal.SchemaMigratorImpl.doMigration(SchemaMigratorImpl.java:110)
at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.performDatabaseAction(SchemaManagementToolCoordinator.java:176)
at org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.process(SchemaManagementToolCoordinator.java:64)
at org.hibernate.internal.SessionFactoryImpl.(SessionFactoryImpl.java:458)
at org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:465)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:708)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724)
at com.telemune.demoHibernate.QueryTester.(QueryTester.java:17)
at com.telemune.demoHibernate.QueryTester.main(QueryTester.java:21)
Caused by: java.sql.SQLException: ORA-00955: name is already used by an existing object
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:331)
at oracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:288)
at oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:745)
at oracle.jdbc.driver.T4CStatement.doOall8(T4CStatement.java:210)
at oracle.jdbc.driver.T4CStatement.executeForRows(T4CStatement.java:961)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1190)
at oracle.jdbc.driver.OracleStatement.executeInternal(OracleStatement.java:1726)
at oracle.jdbc.driver.OracleStatement.execute(OracleStatement.java:1696)
at org.hibernate.tool.schema.internal.exec.GenerationTargetToDatabase.accept(GenerationTargetToDatabase.java:51)
... 14 more
Apr 26, 2016 8:10:47 PM org.hibernate.tool.schema.extract.internal.InformationExtractorJdbcDatabaseMetaDataImpl processGetTableResults
INFO: HHH000262: Table not found: sdp.crbt_subscriber_master
Apr 26, 2016 8:10:47 PM org.hibernate.tool.schema.extract.internal.InformationExtractorJdbcDatabaseMetaDataImpl processGetTableResults
INFO: HHH000262: Table not found: sdp.crbt_subscriber_master
and then show the required result successfully.
But I want to know the reason for the Exception. Asking for help. I only know some things about hibenate, sorry if its a silly question.
sdp
is the name of data base. and this is my mapping:
sdp
答案 0 :(得分:1)
我认为问题是你在这里指定一个带有表名的模式名称
<class name="com.telemune.demoPojo.CrbtSubMasterDemo"
table="sdp.crbt_subscriber_master">
Hibernate将sdp.crbt_subscriber_master
视为表名。
但是当Hibernate尝试执行create table sdp.crbt_subscriber_master()
时,Oracle将sdp
视为模式名称,将crbt_subscriber_master
视为表名。
不知道如何为映射指定架构名称,但您可以按照此处所述进行尝试
Chapter 5. Basic O/R Mapping: mapping-declaration-class
<class name="com.telemune.demoPojo.CrbtSubMasterDemo"
schema="sdp" table="crbt_subscriber_master">
答案 1 :(得分:0)
此处表名称区分大小写。存在不匹配。
INFO: HHH000262: Table not found: sdp.crbt_subscriber_master
数据库正试图获得crbt_subscriber_master
,但您已经提供了
String hql = "FROM CrbtSubMasterDemo where rownum
还有另一个问题。您错过了完美地添加声明hql。
使用此代码
String hql = "FROM CrbtSubMasterDemo where rownum";
而不是
String hql = "FROM CrbtSubMasterDemo where rownum
您还可以阅读本教程:ORA-00955: name is already being used by existing object tips
hibernate.hbm2ddl.auto自动验证或导出架构DDL 创建SessionFactory时到数据库。使用create-drop, 关闭SessionFactory时将删除数据库模式 明确。
e.g。验证|更新|创建|创建降
所以可能的选项列表是,
验证:验证架构,不对数据库进行任何更改。
更新:更新架构。
创建:创建架构,销毁以前的数据。
create-drop :在会话结束时删除架构。
所以,因为它已经创建了数据表。然后,如果您从配置中删除此属性,那么它将被解决。
<property name="hbm2ddl.auto">update</property>
但你可以尝试一下。这也将解决问题。
<property name="hbm2ddl.auto">validate</property>
或
<property name="hbm2ddl.auto">none</property>
有时空字符串'比'无'要好得多。要使用“无”,您将收到warning message: org.hibernate.cfg.SettingsFactory - Unrecognized value for "hibernate.hbm2ddl.auto": none