1

我尝试使用 spring/jpa/hibernate 和 GAE/Google Cloud SQL 进行简单测试。

但我找不到正确的配置,我总是不断得到:

Caused by: com.google.apphosting.api.ApiProxy$CallNotFoundException: The API package 'rdbms' or call 'OpenConnection()' was not found.
    at com.google.apphosting.api.ApiProxy.makeSyncCall(ApiProxy.java:98) ~[appengine-api-1.0-sdk-1.6.4.1.jar:na]
    at com.google.appengine.api.rdbms.RdbmsApiProxyClient$ApiProxyBlockingInterface.makeSyncCall(RdbmsApiProxyClient.java:95) ~[appengine-api-1.0-sdk-1.6.4.1.jar:na]
    at com.google.appengine.api.rdbms.RdbmsApiProxyClient$ApiProxyBlockingInterface.openConnection(RdbmsApiProxyClient.java:73) ~[appengine-api-1.0-sdk-1.6.4.1.jar:na]
    at com.google.cloud.sql.jdbc.internal.SqlProtoClient.openConnection(SqlProtoClient.java:58) ~[appengine-api-1.0-sdk-1.6.4.1.jar:na]
    at com.google.cloud.sql.jdbc.Driver.connect(Driver.java:66) ~[appengine-api-1.0-sdk-1.6.4.1.jar:na]
    at com.google.cloud.sql.jdbc.Driver.connect(Driver.java:26) ~[appengine-api-1.0-sdk-1.6.4.1.jar:na]
    at java.sql.DriverManager.getConnection(DriverManager.java:579) ~[na:1.7.0]
    at java.sql.DriverManager.getConnection(DriverManager.java:190) ~[na:1.7.0]
    at org.hibernate.service.jdbc.connections.internal.DriverManagerConnectionProviderImpl.getConnection(DriverManagerConnectionProviderImpl.java:192) ~[hibernate-core-4.1.2.Final.jar:4.1.2.Final]

版本:

  • 弹簧框架:3.1.1
  • 休眠:4.1.2
  • 谷歌应用引擎:1.6.4.1

持久性.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="gae-test" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>com.gro.gae.domainmodel.user.UserEntity</class>
        <properties>
            <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"></property>
            <property name="hibernate.show_sql" value="true"></property>
            <property name="hibernate.hbm2ddl.auto" value="update"></property>
            <property name="hibernate.connection.url" value="jdbc:google:rdbms://sqlpetstocks:petstocks/dngdatabase"></property>
            <property name="hibernate.connection.driver_class" value="com.google.appengine.api.rdbms.AppEngineDriver"></property>
            <property name="hibernate.connection.driver_class" value="com.google.cloud.sql.Driver"></property>
        </properties>
    </persistence-unit>

</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:jpa="http://www.springframework.org/schema/data/jpa"
    xmlns:context="http://www.springframework.org/schema/context" 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/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

    <jpa:repositories base-package="com.gro.gae.persistence"
        factory-class="com.gro.gae.persistence.CustomJpaRepositoryFactoryBean" />

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean">
        <property name="persistenceUnitName" value="gae-test" /> 
    </bean>

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

    <context:annotation-config />

    <tx:annotation-driven />

</beans>

单元测试 :

@RunWith(org.springframework.test.context.junit4.SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:applicationContext-persistence.xml" })
@TestExecutionListeners( { DependencyInjectionTestExecutionListener.class, TransactionalTestExecutionListener.class })
@TransactionConfiguration(defaultRollback = true)
@Transactional
public class ConfigurationTest  {

    @Test
    public void testConfig(){

    }

}

有任何想法吗 ?

谢谢

纪尧姆

4

1 回答 1

2

为时已晚,但也许它会帮助某人。
我发现了同样的问题,VM args在 JUnit 执行下不起作用。

解决方案是让我为测试定义另一个 entityManager/persistenceUnit。

关键是改变新testPersistence.xml的属性:

<property name="javax.persistence.jdbc.driver" value="com.google.appengine.api.rdbms.AppEngineDriver"/>
<property name="javax.persistence.jdbc.url" value="jdbc:google:rdbms:localhost:3306/DB_NAME" />

<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/DB_NAME" />

希望这可以帮助。

于 2012-09-13T22:03:43.173 回答