将Grails 1.3.7与EclipseLink 2.2.0 JPA Provider一起使用

时间:2011-09-01 14:47:28

标签: grails jpa eclipselink

我尝试使用Grails 1.3.7和插件gorm-jpa 0.7.1来使用EclipseLink 2.2.0作为持久性提供程序来启用JPA持久性。

当运行应用程序并单击JPA带注释的实体的控制器时,我从EclipseLink的JPQLParser获得“UnwantedTokenException”:

Executing action [list] of controller [model.PersonController] caused exception:
An exception occurred while creating a query in EntityManager:
Exception Description: Syntax error parsing the query [select person from model.Person as person ], line 1, column 24: syntax error at [.].
Internal Exception: UnwantedTokenException(found=., expected 80);
  at org.eclipse.persistence.internal.libraries.antlr.runtime.BaseRecognizer.recoverFromMismatchedToken(BaseRecognizer.java:587)
  [...]
  at org.eclipse.persistence.internal.jpa.parsing.jpql.JPQLParser.parse(JPQLParser.java:134)

似乎EclipseLink在“model.Person”中有点问题。

如何解决这个问题?

要重现问题,请按如下方式设置grails项目:

grails create-app GrailsJPA
cd GrailsJPA
grails uninstall-plugin hibernate
grails install-plugin gorm-jpa
grails create-domain-class model.Person

编辑“grails-app \ domain \ model \ Person.groovy”如下:

package model

import javax.persistence.*;

@Entity
class Person {

    @Id
    @GeneratedValue
    long id;

    @Basic
    long version;

    @Basic
    String firstName

    @Basic
    String lastName

    static constraints = {
        firstName(nullable:true)
        lastName(nullable:true)
    }

}

为已定义的实体生成控制器和视图:

grails generate-controller model.Person
grails generate-view model.Person

编辑“grails-app \ conf \ spring \ resources.groovy”,如下所示:

beans = {

    entityManagerFactory(org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean) {
        beanClassLoader = ref("classLoader")
        dataSource = ref("dataSource")
        loadTimeWeaver = new org.springframework.instrument.classloading.SimpleLoadTimeWeaver()
    }

    transactionManager(org.springframework.orm.jpa.JpaTransactionManager) {
        entityManagerFactory = entityManagerFactory
    }

}

创建文件“web-app \ WEB-INF \ classes \ META-INF \ 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_1_0.xsd" version="1.0">
    <persistence-unit name="manager" transaction-type="RESOURCE_LOCAL">
        <provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
        <class>model.Person</class>
        <properties>
            <property name="eclipselink.ddl-generation" value="create-tables"/>
        </properties>
    </persistence-unit>
</persistence>

http://www.eclipse.org/eclipselink/downloads/2.2.0下载EclipseLink 2.2.0安装程序ZIP,并从ZIP中的“eclipselink \ jlib \”中提取“eclipselink.jar”到grails项目目录:

lib\eclipselink.jar

现在运行grails应用程序:

grails run-app

浏览

http://localhost:8080/GrailsJPA

现在点击控制器“model.PersonController”重现上述异常。

有关如何解决此问题的任何想法?

1 个答案:

答案 0 :(得分:1)