将org.hibernate.jpa.HibernatePersistenceProvider与BoneCP一起使用时遇到错误

时间:2018-07-17 06:37:43

标签: java mysql spring hibernate datasource

以前,我使用(from loanPerson in context.LoanPersons.AsParallel() join warranter in context.Warranters.AsParallel() on loanPerson.Id equals warranter.LoanPersonId where loanPerson.Id == 84829 select new { loanPersonId = loanPerson.Id, waranterId = warranter.WarranterPersonID, }).ToList(); 作为提供程序类,但现在我将其更改为org.hibernate.ejb.HibernatePersistence,因为最新的休眠版本不推荐使用HibernatePersistence。目前,我正在使用org.hibernate.jpa.HibernatePersistenceProvider

我的persistence.xml:

hibernate 5.3.2.Final

我的build.gradle是:

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

        <persistence-unit name="iTest" transaction-type="RESOURCE_LOCAL">
            <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
            <class>com.test.Test</class>
            <exclude-unlisted-classes>true</exclude-unlisted-classes>
            <properties>
                <property name="hibernate.connection.driver_class" value="com.mysql.jdbc.Driver" />
                <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
                <property name="hibernate.connection.username" value="root" />
                <property name="hibernate.connection.password" value="root" />
                <property name="hibernate.jdbc.batch_size" value="1000" />

                <property name="hibernate.format_sql" value="false" />
                <property name="hibernate.show_sql" value="false" />
                <property name="hibernate.use_sql_comments" value="false" />
                <property name="hibernate.hbm2ddl.auto" value="update" />

                <property name="hibernate.connection.provider_class" value="com.jolbox.bonecp.provider.BoneCPConnectionProvider" />
                <property name="bonecp.idleConnectionTestPeriod" value="60" />
                <property name="bonecp.partitionCount" value="3" />
                <property name="bonecp.maxConnectionsPerPartition" value="60" />
                <property name="bonecp.minConnectionsPerPartition" value="20" />
                <property name="bonecp.statementsCacheSize" value="100" />
                <property name="bonecp.releaseHelperThreads" value="3" />
            </properties>
        </persistence-unit>

    </persistence>

在EntityManagerFactory上调用方法createEntityManager()时出现以下异常。

dependencies {
        compile group: 'com.opencsv', name: 'opencsv', version:'4.2'
        compile group: 'org.apache.logging.log4j', name: 'log4j-core', version:'2.11.0'
        compile group: 'org.apache.commons', name: 'commons-lang3', version:'3.7'
        compile group: 'org.springframework', name: 'spring-context', version:'5.0.7.RELEASE'
        compile group: 'org.springframework', name: 'spring-orm', version:'5.0.7.RELEASE'
        compile(group: 'org.hibernate', name: 'hibernate-core', version:'5.3.2.Final') {
            exclude(module: 'slf4j-api')
            exclude(module: 'slf4j-api')
            exclude(module: 'cglib')
            exclude(module: 'xml-apis')
        }
        compile group: 'org.apache.commons', name: 'commons-pool2', version:'2.5.0'
        compile group: 'org.slf4j', name: 'slf4j-api', version:'1.7.25'
        compile group: 'org.slf4j', name: 'slf4j-log4j12', version:'1.7.25'
        compile group: 'org.springframework', name: 'spring-aspects', version:'5.0.7.RELEASE'
        /*compile group: 'org.hibernate', name: 'hibernate-annotations', version:'3.5.6-Final'*/
        compile group: 'mysql', name: 'mysql-connector-java', version:'8.0.11'
        compile group: 'org.aspectj', name: 'aspectjrt', version:'1.9.1'
        compile group: 'org.aspectj', name: 'aspectjweaver', version:'1.9.1'
        compile group: 'org.apache.commons', name: 'commons-configuration2', version:'2.2'
        compile(group: 'com.jolbox', name: 'bonecp', version:'0.8.0.RELEASE') {
            exclude(module: 'slf4j-api')
        }
        compile(group: 'com.jolbox', name: 'bonecp-provider', version:'0.7.1.RELEASE') {
            exclude(module: 'slf4j-api')
        }
        compile group: 'com.google.guava', name: 'guava', version:'25.1-jre'
        compile group: 'com.googlecode.json-simple', name: 'json-simple', version:'1.1.1'
        testCompile group: 'junit', name: 'junit', version:'4.10'
    }

我遇到异常Caused by: org.hibernate.boot.registry.selector.spi.StrategySelectionException: Unable to resolve name [com.jolbox.bonecp.BoneCPConnectionProvider] as strategy [org.hibernate.engine.jdbc.connections.spi.ConnectionProvider] at org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.selectStrategyImplementor(StrategySelectorImpl.java:133) at org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator.initiateService(ConnectionProviderInitiator.java:138) at org.hibernate.engine.jdbc.connections.internal.ConnectionProviderInitiator.initiateService(ConnectionProviderInitiator.java:41) at org.hibernate.boot.registry.internal.StandardServiceRegistryImpl.initiateService(StandardServiceRegistryImpl.java:94) at org.hibernate.service.internal.AbstractServiceRegistryImpl.createService(AbstractServiceRegistryImpl.java:263) ... 24 more Caused by: org.hibernate.boot.registry.classloading.spi.ClassLoadingException: Unable to load class [com.jolbox.bonecp.BoneCPConnectionProvider] at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:354) at org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.selectStrategyImplementor(StrategySelectorImpl.java:129) ... 28 more Caused by: java.lang.ClassNotFoundException: Could not load requested class : com.jolbox.bonecp.BoneCPConnectionProvider at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl$AggregatedClassLoader.findClass(ClassLoaderServiceImpl.java:342) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at java.lang.ClassLoader.loadClass(ClassLoader.java:357) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:348) at org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl.classForName(ClassLoaderServiceImpl.java:351) ... 29 more

当我查看jar文件内部时,classpath中的jar内有可用的类。

1 个答案:

答案 0 :(得分:0)

由于您使用的是不具有与您的休眠版本兼容的BoneCPConnectionProvider类的bonecp 0.7.1.RELEASE。因此将依赖关系更新为0.8.0.RELEASE