简体   繁体   中英

derby + hibernate +Spring MVC

Netbeans fails to create hibernate.reveng.xml with error

Cannot establish database connection with selected Hibernate Configuration file. Please verify the database connection details in hibernate.cfg.xml

My hibernate.cfg.xml looks like this

<?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.ClientDriver</property>
      <property name="hibernate.dialect">org.hibernate.dialect.DerbyDialect</property>

    <property name="hibernate.connection.url">jdbc:derby://localhost:1527/User</property>
  </session-factory>
</hibernate-configuration>

What is the problem? I am working with the Java DB (Derby) Database. I haven't this problem not when I use MySQL

The most likely cause is that Derby our of the box is expected to be run in embedded mode and won't allow port connections. You should be running it in network mode.

There's information here: http://db.apache.org/derby/papers/DerbyTut/index.html

which should provide you with the information to set up the network mode or migrate to embedded (I'd suggest going network first to test whether that is the issue) and then switching if desired).

I would Go in a different direction. I would use JPA and set hibernate as you JPA vendor. It would look a little different, but spring has full support with JPA (including transaction manager and every thing). It will look something like this:
Instead of hibernate.cfg.xml, you will have an xml called "persistence.xml" (will reside inside resources/META-INF) and it will look like this:

<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
    version="2.0">
    <persistence-unit name="myPersistanceUnit">

        <class>com.company.entities.Entity1</class>
        <class>com.company.entities.Entity2</class>
        <class>com.company.entities.Entity3</class>

        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.DerbyDialect" />
            <property name="hibernate.show_sql" value="true" />
        </properties>

    </persistence-unit>
</persistence>

You ApplicationContext.xml will have something like this:

<tx:annotation-driven />

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
        p:entityManagerFactory-ref="entityManagerFactory" />

<!-- The data source is configured in the application server but spring also supports a configured bean -->
    <bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName" value="jdbc/MyDataSource" />
    </bean>

    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="persistenceUnitName" value="myPersistanceUnit" />
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="databasePlatform" value="org.hibernate.dialect.DerbyDialect" />
            </bean>
        </property>
    </bean>

Instead of hibernate annotations, You will use JPA annotations (which are basically the same - under the javax.persistence package) and your base dao call will use

@PersistenceContext
    protected EntityManager entityManager;

to handle the DB operations. under these configurations, i don't think you will have any problems.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM