我有一个非常简单的,我努力使它工作的最后几个小时。我有以下简单的类:
package com.stgiaf.traininng.inventory.domain.model;
import io.quarkus.runtime.annotations.RegisterForReflection;
import javax.persistence.Entity;
import javax.persistence.Id;
import java.util.Set;
@RegisterForReflection
public class Item {
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
private long id;
public String itemId;
public String description;
public Item(String itemId, String description) {
this.itemId = itemId;
this.description = description;
}
// JPA default constructor
protected Item() {
}
public String itemId() {
return this.itemId;
}
public void setItemId(String itemId) {
this.itemId = itemId;
}
public String description() {
return description;
}
}
package com.stgiaf.traininng.inventory.infrastructure.persistence;
import com.oracle.svm.core.annotate.Inject;
import com.stgiaf.traininng.inventory.domain.model.Item;
import com.stgiaf.traininng.inventory.domain.model.ItemRepository;
import io.quarkus.hibernate.orm.PersistenceUnit;
import javax.enterprise.context.ApplicationScoped;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
@ApplicationScoped
public class HibernateItemRepository implements ItemRepository {
@Inject
EntityManager em;
@Override
public void addItem(Item item) {
em.persist(item);
}
}
最后但并非最不重要的是一个非常简单的控制器来执行整个操作:
package com.stgiaf.traininng.inventory;
import com.stgiaf.traininng.inventory.domain.model.Item;
import com.stgiaf.traininng.inventory.domain.model.ItemRepository;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/inventory")
public class InventoryResource {
@Inject
ItemRepository repository;
@GET
@Path("{itemId}")
@Produces(MediaType.TEXT_PLAIN)
public String hello(@PathParam("itemId") String itemId) {
repository.addItem(new Item(itemId, itemId + " description"));
return "success";
}
}
<?xml version="1.0" encoding="utf-8" ?>
<entity-mappings xmlns="http://www.eclipse.org/eclipselink/xsds/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.eclipse.org/eclipselink/xsds/persistence/orm resources/orm_2_2.xsd"
version="2.2">
<entity class="com.stgiaf.traininng.inventory.domain.model.Item" name="Item">
<table name="item"/>
<attributes>
<id name="id">
<generated-value strategy="AUTO"/>
</id>
<basic name="itemId">
<column name="itemId"/>
</basic>
<basic name="description">
<column name="description"/>
</basic>
</attributes>
</entity>
</entity-mappings>
因此,如前所述,我想使用orm.xml将实体Map到休眠状态。然后我在www.example.com中的唯一设置application.properties是:
quarkus.datasource.db-kind=postgresql
现在,当我执行rest端点时,我得到以下错误:
java.lang.NullPointerException: Cannot invoke "javax.persistence.EntityManager.persist(Object)" because "this.em" is null
at com.sgiaf.training.inventory.infrastracture.persistence.HibernateItemRepository.addItem(HibernateItemRepository.java:20)
at com.sgiaf.training.inventory.infrastracture.persistence.HibernateItemRepository_Subclass.addItem$$superforward1(Unknown Source)
at com.sgiaf.training.inventory.infrastracture.persistence.HibernateItemRepository_Subclass$$function$$1.apply(Unknown Source)
at io.quarkus.arc.impl.AroundInvokeInvocationContext.proceed(AroundInvokeInvocationContext.java:54)
at io.quarkus.arc.runtime.devconsole.InvocationInterceptor.proceed(InvocationInterceptor.java:62)
at io.quarkus.arc.runtime.devconsole.InvocationInterceptor.monitor(InvocationInterceptor.java:49)
at io.quarkus.arc.runtime.devconsole.InvocationInterceptor_Bean.intercept(Unknown Source)
at io.quarkus.arc.impl.InterceptorInvocation.invoke(InterceptorInvocation.java:42)
at io.quarkus.arc.impl.AroundInvokeInvocationContext.perform(AroundInvokeInvocationContext.java:41)
at io.quarkus.arc.impl.InvocationContexts.performAroundInvoke(InvocationContexts.java:33)
at com.sgiaf.training.inventory.infrastracture.persistence.HibernateItemRepository_Subclass.addItem(Unknown Source)
at com.sgiaf.training.inventory.infrastracture.persistence.HibernateItemRepository_ClientProxy.addItem(Unknown Source)
at com.sgiaf.training.inventory.InventoryResource.hello(InventoryResource.java:26)
at com.sgiaf.training.inventory.InventoryResource_Subclass.hello$$superforward1(Unknown Source)
at com.sgiaf.training.inventory.InventoryResource_Subclass$$function$$1.apply(Unknown Source)
at io.quarkus.arc.impl.AroundInvokeInvocationContext.proceed(AroundInvokeInvocationContext.java:54)
at io.quarkus.arc.runtime.devconsole.InvocationInterceptor.proceed(InvocationInterceptor.java:62)
at io.quarkus.arc.runtime.devconsole.InvocationInterceptor.monitor(InvocationInterceptor.java:49)
at io.quarkus.arc.runtime.devconsole.InvocationInterceptor_Bean.intercept(Unknown Source)
at io.quarkus.arc.impl.InterceptorInvocation.invoke(InterceptorInvocation.java:42)
at io.quarkus.arc.impl.AroundInvokeInvocationContext.perform(AroundInvokeInvocationContext.java:41)
at io.quarkus.arc.impl.InvocationContexts.performAroundInvoke(InvocationContexts.java:33)
at com.sgiaf.training.inventory.InventoryResource_Subclass.hello(Unknown Source)
at com.sgiaf.training.inventory.InventoryResource$quarkusrestinvoker$hello_02a67b16908f55e9aa20fb4489a502ac7da7e268.invoke(Unknown Source)
at org.jboss.resteasy.reactive.server.handlers.InvocationHandler.handle(InvocationHandler.java:29)
at io.quarkus.resteasy.reactive.server.runtime.QuarkusResteasyReactiveRequestContext.invokeHandler(QuarkusResteasyReactiveRequestContext.java:115)
at org.jboss.resteasy.reactive.common.core.AbstractResteasyReactiveContext.run(AbstractResteasyReactiveContext.java:142)
at io.quarkus.vertx.core.runtime.VertxCoreRecorder$14.runWith(VertxCoreRecorder.java:576)
at org.jboss.threads.EnhancedQueueExecutor$Task.run(EnhancedQueueExecutor.java:2449)
at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1478)
at org.jboss.threads.DelegatingRunnable.run(DelegatingRunnable.java:29)
at org.jboss.threads.ThreadLocalResettingRunnable.run(ThreadLocalResettingRunnable.java:29)
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
at java.base/java.lang.Thread.run(Thread.java:1589)
只有当我使用orm xml配置实体时才会发生这个错误。如果我使用基于注解的配置,一切都正常。
知道我想念什么吗
我也试过使用RedHat构建,但发生了同样的错误。我也试过用持久化xml配置hibernate,然后问题改变了,变成了:
Caused by: javax.enterprise.inject.spi.DeploymentException: javax.enterprise.inject.UnsatisfiedResolutionException: Unsatisfied dependency for type javax.persistence.EntityManager and qualifiers [@PersistenceUnit("Inventory")]
- java member: com.stgiaf.traininng.inventory.infrastructure.persistence.HibernateItemRepository#em
- declared on CLASS bean [types=[com.stgiaf.traininng.inventory.infrastructure.persistence.HibernateItemRepository, java.lang.Object, com.stgiaf.traininng.inventory.domain.model.ItemRepository], qualifiers=[@Default, @Any], target=com.stgiaf.traininng.inventory.infrastructure.persistence.HibernateItemRepository]
The following beans match by type, but none have matching qualifiers:
- Bean [class=org.hibernate.Session, qualifiers=[@javax.enterprise.inject.Default, @Any]]
1条答案
按热度按时间eimct9ow1#
因此,正如所见,我想使用orm.xml将实体Map到休眠状态
然后,您必须使用该文件的标准位置(
src/main/resources/META-INF/orm.xml
)或配置Quarkus以使用它(quarkus.hibernate-orm.mapping-files = path/to/orm.xml
)还有:
我也尝试过用一个持久性XML来配置Hibernate
你不需要。
@RegisterForReflection
在Hibernate ORM实体上你不需要这个,Quarkus会为你做。