JPA+Hibernate Google Cloud Sql

发布于 2024-12-11 03:11:48 字数 3354 浏览 0 评论 0原文

随着Google推出新服务-Google Cloud Sql,我决定将gwt+jpa+hibernate+spring+maven Web应用程序迁移到gae。 所以我在本地和 gae 上启动我的应用程序时遇到问题。 我在创建 dao bean 时出错:

     [ERROR] Caused by: java.lang.NoSuchMethodError: javax.persistence.spi.PersistenceUnitInfo.getValidationMode()Ljavax/persistence/ValidationMode;
[ERROR]     at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:625)
[ERROR]     at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:73)
[ERROR]     at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:225)
[ERROR]     at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:310)
[ERROR]     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1477)
[ERROR]     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417)

所以在我看来,我在 hibernate 中使用 jpa-1.0 而不是 jpa-2.0 (因为 jpa 1.0 缺少方法:

getValidationMode()

我检查了我的库...一切正常 - 使用 jpa 2.0 和 hibernate-jpa-2.0-api.jar。 但我发现一个问题: 当我使用没有 gae 的代码并且使用另一个数据库时,一切正常。事实上,appengine sdk 包含自己的 JPA,版本为 1.0(引用自此处 将 JPA 与 App Engine 结合使用)

JPA 和数据存储 JAR 包含在 App Engine Java SDK 中。您可以在 appengine-java-sdk/lib/user/orm/ 目录中找到它们。 将 JAR 复制到应用程序的 war/WEB-INF/lib/ 目录。

当应用程序启动时,它使用appengine sdk中的jpa 1.0(在javax.persistence.spi.PersistenceUnitInfo中没有方法getValidationMode())而不是我的。

那么,我怎样才能解决这个问题,让我的应用程序使用正确的罐子呢? 这也是我的 persistence.xml:

   <?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">

        <!-- A JPA Persistence Unit -->
        <persistence-unit name="PersistenceUnit"
            transaction-type="RESOURCE_LOCAL">
            <provider>org.hibernate.ejb.HibernatePersistence</provider>

            <properties>
                <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
                <property name="hibernate.hbm2ddl.auto" value="update" />
                <property name="hibernate.show_sql" value="true" />
                <property name="hibernate.connection.url" value="jdbc:google:rdbms://bragininim:test/test"></property>
                <property name="hibernate.connection.driver_class"
                    value="com.google.appengine.api.rdbms.AppEngineDriver"></property>

                <!-- Connection -->
                <property name="hibernate.connection.username" value="****" />
                <property name="hibernate.connection.password" value="****" />

            </properties>
        </persistence-unit>

    </persistence>

As Google presented new service - Google Cloud Sql, I decided to migrate gwt+jpa+hibernate+spring+maven web application to gae.
So I have a problem while starting my app locally and on gae.
I have an error creating dao bean caused by this:

     [ERROR] Caused by: java.lang.NoSuchMethodError: javax.persistence.spi.PersistenceUnitInfo.getValidationMode()Ljavax/persistence/ValidationMode;
[ERROR]     at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:625)
[ERROR]     at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:73)
[ERROR]     at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:225)
[ERROR]     at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:310)
[ERROR]     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1477)
[ERROR]     at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1417)

So it seemed to me, that I was using jpa-1.0 instead of jpa-2.0 with hibernate (cause jpa 1.0 lacks of method:

getValidationMode()

I checked my libs...everthing is ok - using jpa 2.0 with hibernate-jpa-2.0-api.jar.
But I found a promlem:
When I use my code without gae and I use another db everthing is ok. The fact is that appengine sdk include its own JPA and the version is 1.0 (quote from here Using JPA with App Engine)

The JPA and datastore JARs are included with the App Engine Java SDK. You can find them in the appengine-java-sdk/lib/user/orm/ directory.
Copy the JARs to your application's war/WEB-INF/lib/ directory.

When an application starts, it use jpa 1.0(with no method getValidationMode() in javax.persistence.spi.PersistenceUnitInfo) from appengine sdk instead of mine.

So, how can I come over this problem to make my app use right jars?
Here is my persistence.xml also:

   <?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">

        <!-- A JPA Persistence Unit -->
        <persistence-unit name="PersistenceUnit"
            transaction-type="RESOURCE_LOCAL">
            <provider>org.hibernate.ejb.HibernatePersistence</provider>

            <properties>
                <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect" />
                <property name="hibernate.hbm2ddl.auto" value="update" />
                <property name="hibernate.show_sql" value="true" />
                <property name="hibernate.connection.url" value="jdbc:google:rdbms://bragininim:test/test"></property>
                <property name="hibernate.connection.driver_class"
                    value="com.google.appengine.api.rdbms.AppEngineDriver"></property>

                <!-- Connection -->
                <property name="hibernate.connection.username" value="****" />
                <property name="hibernate.connection.password" value="****" />

            </properties>
        </persistence-unit>

    </persistence>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

长安忆 2024-12-18 03:11:48

Hibernate JPA 支持存在一些问题,因为它使用 JPA 2.0 规范。但Google App Engine仅支持JPA1.0规范。我也有同样的问题,因为我使用了 Spring Hibernate 支持(而不是 Hibernate JPA)。

对于你的问题,我认为最好使用 Spring Hibernate 支持。

There is some issue with Hibernate JPA support, because it used JPA 2.0 specification. But Google App Engine only support JPA1.0 specification. I also had same issue, there for I used Spring Hibernate support(instead of Hibernate JPA).

For your problem also I think better to use Spring Hibernate support.

一绘本一梦想 2024-12-18 03:11:48

您应该使用旧版本的 hibernate(与 jpa 1 兼容)。 GAE 将这些事情强加给你。实际上我不知道它支持JPA - 我以为只支持JDO。

You should use an older version of hibernate (compatible with jpa 1). GAE forces these things upon you. Actually I didn't know it supports JPA - I thought only JDO is supported.

往事随风而去 2024-12-18 03:11:48

我不明白为什么你在 persistence.xml 中定义用户名和密码。

我认为您不需要这些凭据,因为应用程序对数据库的访问受到 Google SQL Cloud 控制台管理的限制,您可以在其中指定允许的应用程序 ID。

I don't understand why you define username and password in the persistence.xml.

I thougth that you didn't need those credentials cause the app access to DB is restricted from console administration of Google SQL Cloud, where you specify allowed app's IDs.

浅忆 2024-12-18 03:11:48

如果您使用的是 eclipse,我认为 Java 的 GAE 项目只需将 GAE jar 复制到 WEB-INF\lib,从 DataNucleus 和 Geronimo 中删除 JPA jar,然后复制 Hibernate jar。
我猜我从未尝试过这个。

祝你好运!

If you are using eclipse, I think that a GAE project for java just copies the GAE jars to the WEB-INF\lib, remove the JPA jars from DataNucleus and Geronimo and copy your Hibernate jar.
I never tried this I am guessing.

Good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文