我已经开始学习Helidon MP有一段时间了,在教程和我阅读的几乎所有基于这个微服务框架的源代码中,示例都是在 H2 数据库上编写的。到目前为止,我找不到任何基于 MongoDB 的示例。我已经了解eclipselink为MongoDB开发的 JPA 平台,并尝试了JPA/NoSQL Examples中的指南。此外,我实际上可以在一个简单的 maven 项目中成功运行测试,并且直接使用工厂创建EntityManager对象可以正常工作。但是将它放在 Helidon MP 项目中并使用CDI for EntityManager 就像说明一样,当我访问时会导致http://localhost:8080/person/sahand
异常curl
命令。在我放相关代码之前,我刚刚发现当 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 对象,但我觉得这个解决方案很混乱。要么我使用了错误的依赖关系,要么是我不知道的其他东西。这也是我对 Helidon 和 JPA 的指导。