Stanislav Sorokin
Stanislav Sorokin

Reputation: 35

UnsupportedOperationException: The application must supply JDBC connections JPA Hibernate Spring JDBC

When I try to begin transaction

entityManager.getTransaction().begin();

in AbstractDao, I get

java.lang.UnsupportedOperationException: The application must supply JDBC connections

My persistence.xml

<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="pu" transaction-type="RESOURCE_LOCAL">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <properties>
        <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/>
        <property name="hibernate.hbm2ddl.auto" value = "create-drop"/>
    </properties>
</persistence-unit>
</persistence>

My hibernate.properties

jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/ewp?createIfNotExist=true
jdbc.username=root
jdbc.password=root
hibernate.dialect=org.hibernate.dialect.MySQLDialect
hibernate.show_sql=true
hibernate.format_sql=false
hibernate.hbm2ddl.auto=create-drop

EntityManagerFactory bean:

    @Bean
public EntityManagerFactory entityManagerFactory(){
    EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("pu");
    return entityManagerFactory;
}

EntityManager bean:

@Bean
public EntityManager entityManager(){
    return entityManagerFactory.createEntityManager();
}

Upvotes: 3

Views: 10522

Answers (2)

mindSet
mindSet

Reputation: 241

If you are using spring orm using hibernate template then you need to provide the configuration as follows. We use hibernate-entitymanager for Hibernate as JPA implementation. hibernate-entitymanager is dependent on hibernate-core this why we don’t have to put hibernate-core in pom.xml explicitly. It’s being pulled into our project through maven transitive dependencies.

In spring.xml

<beans xmlns=.....>
<context:component-scan base-package="com.town.practice" />
<context:annotation-config />
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="org.hsqldb.jdbcDriver" />
        <property name="url" value="jdbc:hsqldb:mem://productDb" />
        <property name="username" value="sa" />
        <property name="password" value="" />
    </bean>

    <bean id="entityManagerFactory" 
            class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
            p:packagesToScan="com.town.practice.model"
            p:dataSource-ref="dataSource"
            >
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
                <property name="generateDdl" value="true" />
                <property name="showSql" value="true" />
            </bean>
        </property>
    </bean>

    <!-- Transactions -->
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean>
    <!-- enable the configuration of transactional behavior based on annotations -->
    <tx:annotation-driven transaction-manager="transactionManager" />

</beans>

Upvotes: 0

GauravJ
GauravJ

Reputation: 2272

From hibernate site, your properties name look to be incorrect. It should be,

"hibernate.connection.driver_class" for JDBC driver class
"hibernate.connection.url" for JDBC URL
"hibernate.connection.username" for database user
"hibernate.connection.password" for database user password

Correct your property name.

Upvotes: 3

Related Questions