注入MongoDB EntityManager的Helidon MP ValidationException

时间:2020-08-08 12:36:59

标签: mongodb maven eclipselink helidon

一段时间以来,我已经开始学习 Helidon MP ,在教程和我基于此 micro-service 读取的几乎所有源代码中,框架,示例被写在H2数据库上。到目前为止,我找不到任何基于MongoDB的示例。我已经了解 eclipselink MongoDB 开发的JPA平台,并尝试了JPA/NoSQL Examples中的指南。此外,我实际上可以在一个简单的maven项目中成功运行测试,并且使用factory直接创建 EntityManager 对象可以正常工作。但是,当我使用http://localhost:8080/person/sahand命令访问curl时,将其放入Helidon MP项目中并按照指示使用 CDI 进行EntityManager会导致异常。在放置相关代码之前,我刚刚发现,当Helidon为 NoSQL 数据库访问创建EntityManager时,它应在创建org.eclipse.persistence.eis.EISLogin时使用org.eclipse.persistence.sessions.DatabaseLogin对象,从而在以下位置导致ClassCastException一点等等。 这是我得到的例外:

Caused by: Exception [EclipseLink-7108] (Eclipse Persistence Services - 2.7.7.v20200504-69f2c2b80d): org.eclipse.persistence.exceptions.ValidationException
Exception Description: This operation is not supported for non-relational platforms.
        at org.eclipse.persistence.exceptions.ValidationException.notSupportedForDatasource(ValidationException.java:590)
        at org.eclipse.persistence.internal.sessions.AbstractSession.getLogin(AbstractSession.java:2751)
        at io.helidon.integrations.cdi.eclipselink.CDISEPlatform.initializeExternalTransactionController(CDISEPlatform.java:228)
        at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.preConnectDatasource(DatabaseSessionImpl.java:851)
        at org.eclipse.persistence.internal.sessions.DatabaseSessionImpl.login(DatabaseSessionImpl.java:823)
        at org.eclipse.persistence.internal.jpa.EntityManagerFactoryProvider.login(EntityManagerFactoryProvider.java:258)
        at org.eclipse.persistence.internal.jpa.EntityManagerSetupImpl.deploy(EntityManagerSetupImpl.java:769)

这是用于数据库访问的资源管理器:

@Path("/person")
@Dependent
public class PersonResourceManager {
    @PersistenceContext
    private EntityManager em;

    @GET
    @Path("/{name}")
    @Produces("text/plain")
    @Transactional
    public String getResponse(@PathParam("name") String name) {
        Query query = em.createQuery("Select p from PERSON p where p.name LIKE '" + name + "'");
        Person person = (Person) query.getResultList().get(0);
        return String.valueOf(person.getAge()) + "\n";
    }
}

这是我要从已创建的MongoDB集合中读取的实体:

@Entity(name = "PERSON")
@NoSql(dataFormat = DataFormatType.MAPPED)
public class Person implements Serializable {
    @Id
    @GeneratedValue
    @Field(name = "_id")
    private String id;

    @Basic
    private String name;

    @Basic
    private int age;

    @Version
    private long version;

    @Deprecated
    protected Person() {}
    public Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getId() {return id;}
    public String getName() {return name;}
    public void setName(String name) {this.name = name;}
    public int getAge() {return age;}
    public void setAge(int age) {this.age = age;}
}

这些是与项目中解析的数据库相关的Maven依赖项:

<dependency>
    <groupId>javax.transaction</groupId>
    <artifactId>javax.transaction-api</artifactId>
    <version>1.2</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>javax.resource</groupId>
    <artifactId>connector-api</artifactId>
    <version>1.5</version>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>io.helidon.integrations.cdi</groupId>
    <artifactId>helidon-integrations-cdi-eclipselink</artifactId>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>io.helidon.integrations.cdi</groupId>
    <artifactId>helidon-integrations-cdi-jpa</artifactId>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>io.helidon.integrations.cdi</groupId>
    <artifactId>helidon-integrations-cdi-jta-weld</artifactId>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>io.helidon.integrations.cdi</groupId>
    <artifactId>helidon-integrations-cdi-datasource-hikaricp</artifactId>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>jakarta.persistence</groupId>
    <artifactId>jakarta.persistence-api</artifactId>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>org.mongodb</groupId>
    <artifactId>mongo-java-driver</artifactId>
    <version>3.12.6</version>
</dependency>
<dependency>
    <groupId>org.eclipse.persistence</groupId>
    <artifactId>eclipselink</artifactId>
    <version>2.7.7</version>
</dependency>
<dependency>
    <groupId>org.eclipse.persistence</groupId>
    <artifactId>org.eclipse.persistence.nosql</artifactId>
    <version>2.7.7</version>
</dependency>

persistence.xml文件中的单位定义:

    <persistence-unit name="mongodb" transaction-type="RESOURCE_LOCAL">
        <class>sahand.example.helidon.Person</class>
        <properties>
            <property name="eclipselink.target-database" value="org.eclipse.persistence.nosql.adapters.mongo.MongoPlatform"/>
            <property name="eclipselink.nosql.connection-spec" value="org.eclipse.persistence.nosql.adapters.mongo.MongoConnectionSpec"/>
            <property name="eclipselink.nosql.property.mongo.port" value="27017"/>
            <property name="eclipselink.nosql.property.mongo.host" value="localhost"/>
            <property name="eclipselink.nosql.property.mongo.db" value="jpa-nosql-demo"/>
            <property name="eclipselink.logging.level" value="FINEST"/>
        </properties>
    </persistence-unit>

最后,我猜测application.yaml的一部分是正确的。但是,我可以确定在读取此配置之前就发生了异常。

javax:
  sql:
    DataSource:
      person:
        dataSourceClassName: othatrg.eclipse.persistence.nosql.adapters.mongo.MongoPlatform
        dataSource:
          url: mongodb://localhost:27017
          user: sample_user
          password: "samplePass"

如果有人可以帮助我,我将非常感激。尽管可以按工厂创建每个EntityManager对象,但我觉得这种解决方案很麻烦。我正在使用错误的依赖关系或其他我不知道的东西。 This也是我对Helidon和JPA遵循的说明。

1 个答案:

答案 0 :(得分:0)

从某种意义上说,这是bug in Helidon,因为Helidon中的JPA子系统正在调用关系数据库特定的方法Session#getLogin(),而不是更通用的Session#getDatasourceLogin()方法。 / p>

fix应该在Helidon 2.0.2中可用。尽管不能保证能解决此问题,但Helidon仍然可以这样做。