Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.lang.UnsupportedOperationException: The user must supply a JDBC connection

I get this exception but I can not figure out what is wrong with my configuration I am posting the relevent files thanks for any help
I am using Spring 3.1.0.RELEASE hibernate-entitymanager 3.6.10.Final (Should work with JPA 2)

Trying to run the code from a JUnit file

package com.successcharging.core.dao.jpa;

import junit.framework.Assert;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;

import com.successcharging.core.security.dao.UserDao;
import com.successcharging.core.security.model.User;
import com.successcharging.core.security.model.UserImp;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {
    "classpath:applicationContext/applicationContext*.xml"})
@TransactionConfiguration(transactionManager="transactionManager", defaultRollback=true)
@Transactional
public class UserDaoImplTest {

private static final boolean ENABLED = true;

private static final String PASSWORD = "password";

private static final String USERNAME = "joe.bloggs";

@Autowired
private UserDao userDao;
private User user;
@Before
public void before() {
    user = new UserImp();
    user.setName(USERNAME);
    user.setPassword(PASSWORD);
    user.setEnabled(ENABLED);

    userDao.save(user);
}

@Test
public void findByUserName() {
    Assert.assertNotNull(user);
    User user2 = userDao.findById(user.getId());

    Assert.assertNotNull(user2);
    Assert.assertEquals(USERNAME, user2.getName());
}
}

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.successcharging.core" />
</beans>

applicationContext-persistence.xml

 <?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="dataSource" />
    <property name="packagesToScan" value="com.successcharging.core" />

    <property name="jpaDialect">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />
    </property>
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="showSql" value="true" />
            <property name="generateDdl" value="true" />
            <property name="databasePlatform" value="org.hibernate.dialect.MySQLInnoDBDialect" />
        </bean>
    </property>
    <property name="persistenceUnitName" value="successcharging.core.security" />
    <property name="persistenceUnitManager">
        <bean
            class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager" />
    </property>
</bean> 

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="url" value="jdbc:mysql://192.168.1.129:3306/SC_SECURITY" />
    <property name="username" value="sc_admin" />
    <property name="password" value="123" />
</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

<tx:annotation-driven />


</beans>

persistence.xml

<?xml version="1.0" encoding="UTF-8" ?>

<persistence version="1.0"
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_1_0.xsd">

<persistence-unit name="successcharging.core.security"
    transaction-type="RESOURCE_LOCAL">
    <properties>
        <property name="cache.provider_class" value="org.hibernate.cache.NoCacheProvider" />
        <property name="hibernate.max_fetch_depth" value="3" />
        <property name="hibernate.query.factory_class"
            value="org.hibernate.hql.classic.ClassicQueryTranslatorFactory" />
        <property name="hibernate.query.substitutions" value="true 1, false 0" />
    </properties>
</persistence-unit>
like image 639
Nehemia Avatar asked Mar 28 '12 11:03

Nehemia


1 Answers

I found the problem I had to add the datasource to the persistenceUnitManager something like this

<property name="persistenceUnitManager">
    <bean class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManag‌​er">
        <property name="defaultDataSource" ref="dataSource" />
    </bean>
</property>
like image 159
Nehemia Avatar answered Jan 03 '23 16:01

Nehemia