Hibernate + Derby。找不到数据库

时间:2013-06-22 17:02:30

标签: java hibernate derby

我用DBUSER表创建了USERS数据库。但当我尝试连接到数据库时,我采用SQLException。我也可以使用ij连接到数据库。

登录

Jun 22, 2013 6:36:06 PM org.hibernate.annotations.common.Version 
INFO: HCANN000001: Hibernate Commons Annotations {4.0.2.Final}
Jun 22, 2013 6:36:06 PM org.hibernate.Version logVersion
INFO: HHH000412: Hibernate Core {4.2.2.Final}
Jun 22, 2013 6:36:06 PM org.hibernate.cfg.Environment 
INFO: HHH000206: hibernate.properties not found
Jun 22, 2013 6:36:06 PM org.hibernate.cfg.Environment buildBytecodeProvider
INFO: HHH000021: Bytecode provider name : javassist
Jun 22, 2013 6:36:06 PM org.hibernate.cfg.Configuration configure
INFO: HHH000043: Configuring from resource: /hibernate.cfg.xml
Jun 22, 2013 6:36:06 PM org.hibernate.cfg.Configuration getConfigurationInputStream
INFO: HHH000040: Configuration resource: /hibernate.cfg.xml
Jun 22, 2013 6:36:07 PM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.source
forge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
Jun 22, 2013 6:36:07 PM org.hibernate.cfg.Configuration addResource
INFO: HHH000221: Reading mappings from resource: com/petrez/user/DBUser.hbm.xml
Jun 22, 2013 6:36:07 PM org.hibernate.internal.util.xml.DTDEntityResolver resolveEntity
WARN: HHH000223: Recognized obsolete hibernate namespace http://hibernate.source
forge.net/. Use namespace http://www.hibernate.org/dtd/ instead. Refer to Hibernate 3.6 Migration Guide!
Jun 22, 2013 6:36:07 PM org.hibernate.cfg.Configuration doConfigure
INFO: HHH000041: Configured SessionFactory: null
Jun 22, 2013 6:36:07 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImplconfigure
INFO: HHH000402: Using Hibernate built-in connection pool (not for production use!)
Jun 22, 2013 6:36:10 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImplconfigure
INFO: HHH000115: Hibernate connection pool size: 20
Jun 22, 2013 6:36:10 PM org.hibernate.service.jdbc.connections.internal.DriverMa
nagerConnectionProviderImpl configure
INFO: HHH000006: Autocommit mode: false
Jun 22, 2013 6:36:10 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl
configure
INFO: HHH000401: using driver [org.apache.derby.jdbc.EmbeddedDriver] at URL [jdbc:derby:DBUSER]
Jun 22, 2013 6:36:10 PM org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl configure
INFO: HHH000046: Connection properties: {user=admin, password=****}
Jun 22, 2013 6:36:10 PM org.hibernate.engine.jdbc.internal.JdbcServicesImpl configure
WARN: HHH000342: Could not obtain connection to query metadata : Database 'DBUSER' not found.
Jun 22, 2013 6:36:10 PM org.hibernate.dialect.Dialect 
INFO: HHH000400: Using dialect: org.hibernate.dialect.DerbyTenSevenDialect
Jun 22, 2013 6:36:10 PM org.hibernate.engine.jdbc.internal.LobCreatorBuilder use
ContextualLobCreation
INFO: HHH000422: Disabling contextual LOB creation as connection was null
Jun 22, 2013 6:36:11 PM org.hibernate.engine.transaction.internal.TransactionFactoryInitiator initiateService
INFO: HHH000399: Using default transaction strategy (direct JDBC transactions)
Jun 22, 2013 6:36:11 PM org.hibernate.hql.internal.ast.ASTQueryTranslatorFactory
 
INFO: HHH000397: Using ASTQueryTranslatorFactory
Jun 22, 2013 6:36:13 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
WARN: SQL Error: 40000, SQLState: XJ004
Jun 22, 2013 6:36:13 PM org.hibernate.engine.jdbc.spi.SqlExceptionHelper logExceptions
ERROR: Database 'DBUSER' not found.
Exception in thread "main" org.hibernate.exception.GenericJDBCException: Could not open connection
        at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:54)
        at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)
        at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)
        at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:221)
        at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.getConnection(LogicalConnectionImpl.java:157)
        at org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.doBegin(JdbcTransaction.java:67)
        at org.hibernate.engine.transaction.spi.AbstractTransactionImpl.begin(AbstractTransactionImpl.java:160)
        at org.hibernate.internal.SessionImpl.beginTransaction(SessionImpl.java:1426)
        at com.petrez.App.main(App.java:16)
Caused by: java.sql.SQLException: Database 'DBUSER' not found.
        at org.apache.derby.impl.jdbc.SQLExceptionFactory40.getSQLException(Unknown Source)
        at org.apache.derby.impl.jdbc.Util.newEmbedSQLException(Unknown Source)
        at org.apache.derby.impl.jdbc.Util.newEmbedSQLException(Unknown Source)
        at org.apache.derby.impl.jdbc.Util.generateCsSQLException(Unknown Source)
        at org.apache.derby.impl.jdbc.EmbedConnection.newSQLException(Unknown Source)
        at org.apache.derby.impl.jdbc.EmbedConnection.handleDBNotFound(Unknown Source)
        at org.apache.derby.impl.jdbc.EmbedConnection.(Unknown Source)
        at org.apache.derby.impl.jdbc.EmbedConnection40.(Unknown Source)
        at org.apache.derby.jdbc.Driver40.getNewEmbedConnection(Unknown Source)
        at org.apache.derby.jdbc.InternalDriver.connect(Unknown Source)
        at org.apache.derby.jdbc.Driver20.connect(Unknown Source)
        at org.apache.derby.jdbc.EmbeddedDriver.connect(Unknown Source)
        at org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.getConnection(DriverManagerConnectionProviderImpl.java:204)
        at org.hibernate.internal.AbstractSessionImpl$NonContextualJdbcConnectionAccess.obtainConnection(AbstractSessionImpl.java:292)
        at org.hibernate.engine.jdbc.internal.LogicalConnectionImpl.obtainConnection(LogicalConnectionImpl.java:214)
        ... 5 more
Caused by: java.sql.SQLException: Database 'DBUSER' not found.
        at org.apache.derby.impl.jdbc.SQLExceptionFactory.getSQLException(Unknown Source)
        at org.apache.derby.impl.jdbc.SQLExceptionFactory40.wrapArgsForTransportAcrossDRDA(Unknown Source)
        ... 20 more

DBUser.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>
    <class name="com.petrez.user.DBUser" table="USERS">
        <id name="userId" type="int">
            <column name="USER_ID" precision="5" scale="0" />
            <generator class="assigned" />
        </id>
        <property name="username" type="string">
            <column name="USERNAME" length="20" not-null="true" />
        </property>
        <property name="createdBy" type="string">
            <column name="CREATED_BY" length="20" not-null="true" />
        </property>
        <property name="createdDate" type="date">
            <column name="CREATED_DATE" length="7" not-null="true" />
        </property>
    </class>
</hibernate-mapping>

hibernate.cfg.xml中

<?xml version="1.0" encoding="utf-8"?>
<!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="hibernate.connection.driver_class">org.apache.derby.jdbc.EmbeddedDriver</property>
        <property name="hibernate.connection.url">jdbc:derby:DBUSER</property>

        <property name="hibernate.connection.username">admin</property>
        <property name="hibernate.connection.password">admin</property>

        <property name="hibernate.dialect">org.hibernate.dialect.DerbyTenSevenDialect</property>
        <property name="show_sql">true</property>
        <mapping resource="com/petrez/user/DBUser.hbm.xml"></mapping>
    </session-factory>
</hibernate-configuration>

1 个答案:

答案 0 :(得分:1)

您可以尝试将create = true与url字符串一起使用。

<property name=”hibernate.connection.url”>jdbc:derby:DBUSER;create=true</property>

creates在数据库连接URL Derby系统中指定的标准数据库,然后连接到它。如果无法创建数据库,则错误日志中将显示错误,并且连接尝试失败,并显示SQLException,指示无法找到数据库。

如果数据库已存在,则创建与现有数据库的连接,并发出SQLWarning。