- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
为了使用实体管理器的 PersistenceContext 注入(inject)来测试 JPA,我有具有以下结构的基本示例项目:人实体:
@Entity
public class Person {
@Id
@GeneratedValue
private long id;
private String name;
private String surname;
public Person() {
}
public Person(String name, String surname) {
this.name = name;
this.surname = surname;
}
public long getId() {
return id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
if (id != person.id) return false;
if (name != null ? !name.equals(person.name) : person.name != null) return false;
return surname != null ? surname.equals(person.surname) : person.surname == null;
}
@Override
public int hashCode() {
int result = (int) (id ^ (id >>> 32));
result = 31 * result + (name != null ? name.hashCode() : 0);
result = 31 * result + (surname != null ? surname.hashCode() : 0);
return result;
}
@Override
public String toString() {
return "Person{" +
"id=" + id +
", name='" + name + '\'' +
", surname='" + surname + '\'' +
'}';
}
}
其余配置:
@ApplicationPath("/api")
public class RestConfig extends Application {
}
测试休息端点:
@Path("/test")
//@Vetoed
public class TestResource {
@PersistenceContext(unitName = "pu")
private EntityManager entityManager;
@GET
@Path("/dbtest")
public Response testDb() {
System.out.println("DBG Invocation of dbtest");
Person person=new Person("tomas","bisciak");
entityManager.persist(person); //ALWAYS NULL
System.out.println("Persisted");
Person retrievedEntity= entityManager.find(Person.class,1);
System.out.println("Retrieved entity:"+retrievedEntity);
return Response.status(Response.Status.OK).build();
}
}
持久性.xml
<persistence xmlns="http://xmlns.jcp.org/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
version="2.1">
<persistence-unit name="pu" transaction-type="JTA">
<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>
<jta-data-source>java:jboss/datasources/MySqlDS2</jta-data-source>
<class>Person</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="com.mysql.jdbc.Driver" />
<property name="javax.persistence.jdbc.user" value="root" />
<property name="javax.persistence.jdbc.password" value="root" />
<property name="javax.persistence.jdbc.url" value="jdbc:mysql://localhost:3306/xchange" />
<property name="eclipselink.logging.level" value="FINE"/>
<property name="eclipselink.target-database" value="MySQL"/>
<property name="eclipselink.ddl-generation" value="create-tables" />
</properties>
</persistence-unit>
</persistence>
在我的 wildflystandalone.xml 中定义的数据源:
<datasource jndi-name="java:jboss/datasources/MySqlDS2" pool-name="MySqlDS2" enabled="true" use-java-context="true">
<connection-url>jdbc:mysql://localhost:3306/xchange</connection-url>
<driver-class>com.mysql.jdbc.Driver</driver-class>
<driver>mysql-connector-java-5.1.45-bin.jar_com.mysql.jdbc.Driver_5_1</driver>
<security>
<user-name>root</user-name>
<password>root</password>
</security>
</datasource>
带有 war 结构的项目结构:
在我的 pom.xml 中遵循以下内容:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.bisciak.jpa</groupId>
<artifactId>jpatest</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<endorsed.dir>${project.build.directory}/endorsed</endorsed.dir>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<failOnMissingWebXml>false</failOnMissingWebXml>
</properties>
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.eclipse.persistence/eclipselink -->
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>eclipselink</artifactId>
<version>2.7.1</version>
</dependency>
<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>6.0.6</version>
</dependency>
<!--Testing dependencies-->
<!-- https://mvnrepository.com/artifact/org.mockito/mockito-core -->
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.13.0</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
所有这些都在最新稳定的 Wildfly 上运行,我得到的输出是:
Connected to server
[2018-01-29 03:17:37,085] Artifact jpatest:war exploded: Artifact is being deployed, please wait...
15:17:37,168 INFO [org.jboss.as.server.deployment] (MSC service thread 1-2) WFLYSRV0027: Starting deployment of "jpatest-1.0-SNAPSHOT" (runtime-name: "jpatest-1.0-SNAPSHOT.war")
15:17:38,312 INFO [org.jboss.weld.deployer] (MSC service thread 1-5) WFLYWELD0003: Processing weld deployment jpatest-1.0-SNAPSHOT.war
15:17:38,351 INFO [org.hibernate.validator.internal.util.Version] (MSC service thread 1-5) HV000001: Hibernate Validator 5.3.5.Final
15:17:38,516 INFO [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-5) WFLYJCA0005: Deploying non-JDBC-compliant driver class com.mysql.cj.jdbc.Driver (version 6.0)
15:17:38,537 INFO [org.jboss.weld.Version] (MSC service thread 1-5) WELD-000900: 2.4.3 (Final)
15:17:38,574 INFO [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-1) WFLYJCA0018: Started Driver service with driver-name = jpatest-1.0-SNAPSHOT.war_com.mysql.cj.jdbc.Driver_6_0
15:17:38,896 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-6) MSC000001: Failed to start service jboss.deployment.unit."jpatest-1.0-SNAPSHOT.war".WeldStartService: org.jboss.msc.service.StartException in service jboss.deployment.unit."jpatest-1.0-SNAPSHOT.war".WeldStartService: Failed to start service
at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1978)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.IllegalArgumentException: WFLYWELD0037: Error injecting persistence unit into CDI managed bean. Can't find a persistence unit named 'pu' in deployment jpatest-1.0-SNAPSHOT.war for injection point private javax.persistence.EntityManager TestResource.entityManager
at org.jboss.as.weld.services.bootstrap.WeldJpaInjectionServices.getScopedPUName(WeldJpaInjectionServices.java:114)
at org.jboss.as.weld.services.bootstrap.WeldJpaInjectionServices.registerPersistenceContextInjectionPoint(WeldJpaInjectionServices.java:77)
at org.jboss.weld.injection.ResourceInjectionFactory$PersistenceContextResourceInjectionProcessor.getResourceReferenceFactory(ResourceInjectionFactory.java:350)
at org.jboss.weld.injection.ResourceInjectionFactory$PersistenceContextResourceInjectionProcessor.getResourceReferenceFactory(ResourceInjectionFactory.java:338)
at org.jboss.weld.injection.ResourceInjectionFactory$ResourceInjectionProcessor.createFieldResourceInjection(ResourceInjectionFactory.java:216)
at org.jboss.weld.injection.ResourceInjectionFactory$ResourceInjectionProcessor.createResourceInjections(ResourceInjectionFactory.java:188)
at org.jboss.weld.injection.ResourceInjectionFactory.discoverType(ResourceInjectionFactory.java:448)
at org.jboss.weld.injection.ResourceInjectionFactory.getResourceInjections(ResourceInjectionFactory.java:96)
at org.jboss.weld.injection.producer.ResourceInjector.<init>(ResourceInjector.java:59)
at org.jboss.weld.injection.producer.ResourceInjector.of(ResourceInjector.java:49)
at org.jboss.weld.injection.producer.BeanInjectionTarget.<init>(BeanInjectionTarget.java:63)
at org.jboss.weld.injection.producer.BeanInjectionTarget.createDefault(BeanInjectionTarget.java:47)
at org.jboss.weld.manager.InjectionTargetFactoryImpl.chooseInjectionTarget(InjectionTargetFactoryImpl.java:113)
at org.jboss.weld.manager.InjectionTargetFactoryImpl.createInjectionTarget(InjectionTargetFactoryImpl.java:86)
at org.jboss.weld.bean.ManagedBean.<init>(ManagedBean.java:100)
at org.jboss.weld.bean.ManagedBean.of(ManagedBean.java:80)
at org.jboss.weld.bootstrap.AbstractBeanDeployer.createManagedBean(AbstractBeanDeployer.java:261)
at org.jboss.weld.bootstrap.BeanDeployer.createClassBean(BeanDeployer.java:226)
at org.jboss.weld.bootstrap.ConcurrentBeanDeployer$2.doWork(ConcurrentBeanDeployer.java:74)
at org.jboss.weld.bootstrap.ConcurrentBeanDeployer$2.doWork(ConcurrentBeanDeployer.java:71)
at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:62)
at org.jboss.weld.executor.IterativeWorkerTaskFactory$1.call(IterativeWorkerTaskFactory.java:55)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
at org.jboss.threads.JBossThread.run(JBossThread.java:320)
15:17:38,896 ERROR [org.jboss.as.controller.management-operation] (management-handler-thread - 3) WFLYCTL0013: Operation ("deploy") failed - address: ([("deployment" => "jpatest-1.0-SNAPSHOT")]) - failure description: {"WFLYCTL0080: Failed services" => {"jboss.deployment.unit.\"jpatest-1.0-SNAPSHOT.war\".WeldStartService" => "Failed to start service
Caused by: java.lang.IllegalArgumentException: WFLYWELD0037: Error injecting persistence unit into CDI managed bean. Can't find a persistence unit named 'pu' in deployment jpatest-1.0-SNAPSHOT.war for injection point private javax.persistence.EntityManager TestResource.entityManager"}}
15:17:38,911 ERROR [org.jboss.as.server] (management-handler-thread - 3) WFLYSRV0021: Deploy of deployment "jpatest-1.0-SNAPSHOT.war" was rolled back with the following failure message:
{"WFLYCTL0080: Failed services" => {"jboss.deployment.unit.\"jpatest-1.0-SNAPSHOT.war\".WeldStartService" => "Failed to start service
Caused by: java.lang.IllegalArgumentException: WFLYWELD0037: Error injecting persistence unit into CDI managed bean. Can't find a persistence unit named 'pu' in deployment jpatest-1.0-SNAPSHOT.war for injection point private javax.persistence.EntityManager TestResource.entityManager"}}
15:17:38,911 INFO [org.jboss.as.connector.deployers.jdbc] (MSC service thread 1-8) WFLYJCA0019: Stopped Driver service with driver-name = jpatest-1.0-SNAPSHOT.war_com.mysql.cj.jdbc.Driver_6_0
15:17:38,963 INFO [org.jboss.as.server.deployment] (MSC service thread 1-6) WFLYSRV0028: Stopped deployment jpatest-1.0-SNAPSHOT (runtime-name: jpatest-1.0-SNAPSHOT.war) in 62ms
[2018-01-29 03:17:39,010] Artifact jpatest:war exploded: Error during artifact deployment. See server log for details.
[2018-01-29 03:17:39,010] Artifact jpatest:war exploded: java.lang.Exception: {"WFLYCTL0080: Failed services" => {"jboss.deployment.unit.\"jpatest-1.0-SNAPSHOT.war\".WeldStartService" => "Failed to start service
Caused by: java.lang.IllegalArgumentException: WFLYWELD0037: Error injecting persistence unit into CDI managed bean. Can't find a persistence unit named 'pu' in deployment jpatest-1.0-SNAPSHOT.war for injection point private javax.persistence.EntityManager TestResource.entityManager"}}
将持久性单元注入(inject) CDI 托管 bean 时出错。找不到名为“pu”的持久单元
或者当我在 TestResource 类上使用 @Vetoed 时,entityManager 上的空指针。
我到底做错了什么,我无法使用@PersistenceContext注入(inject)容器管理的entityManager?
我试图理解这一点,因为对于其他人来说这似乎有效。
请不要告诉我使用应用程序管理的事务,我可以使该工作完成,我需要容器管理的entityMnanager,所以我需要注入(inject)它
@PersistenceContext(unitName="")
注释和编辑:
我用的是:Intellij、Wildfly、eclipselink、Java EE7、Maven、MySQL
此外,我的 beans.xml 中没有任何内容,只是将 beans 发现模式设置为全部:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
http://xmlns.jcp.org/xml/ns/javaee/beans_1_1.xsd"
bean-discovery-mode="all">
</beans>
最佳答案
由于您没有使用支持 EntityManager
注入(inject)的 EJB,因此您必须使用 EntityManagerFactory
来创建和销毁 EntityManager。
EntityManagerFactory factory = Persistence.createEntityManagerFactory("pu");
EntityManager entityManager = factory.createEntityManager();
使用后请务必关闭“entityManager”
entityManager.close();
否则,将您的资源转换为 EJB,添加 @Stateless
。
关于java - 在最基本的示例中无法使用 @PersistenceContext 注入(inject) EntityManager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48503545/
有一些使用多个数据源的示例: @Inject @DataSource("users") AgroalDataSource dataSource1; @Inject @DataSource("inven
我试图使用持久性和 servlet guice 扩展,让简单的 webapp 在 Jetty 上与 Guice 和 JPA 一起工作。 我写了这个服务实现类: public class PersonS
我使用 Hibernate 5.1.0.Final、Guice、Jersey。我有创建 EntityManagerFactory 的 HibernateModule并管理EntityManager实例
我正在使用 ThreadLocal 和请求/实体模式来获取实体。这种情况发生时,我关闭了一个实体管理器,并且在后台有一些实体可以编辑、复制和修改,然后我想将其保留或与新的实体管理器合并。我不知道这是一
好的,我正在使用 EJB 3.0 和 hibernate,我们将 .ear 文件放入嵌入 Apache Tomcat 6.0.18 的 Easy-Beans 1.0.1(带有 Hibernate)部署
我正在使用 Spring + JPA + Hibernate + EntityManager 与数据库对话。我收到“A JTA EntityManager cannot use getTransact
更新数据库时我应该更喜欢什么?这两种方法的优缺点是什么?我什么时候应该使用其中一种? public void disemployEmployee(Integer employeeId, Date en
我正在尝试在存储库中注入(inject) EntityManager。 编译成功,但是当我运行应用程序并发送一个发布请求时,我收到了这个错误: Unexpected error occurred: F
我正在尝试在 Spring Tools Suite 和 Pivotal tc Server Developer Edition 上开发 Spring+Hibernate+EntityManager+S
我有一个使用 spring boot + spring data JPA 的示例项目。在日志中,我观察到 EntityManagers 在我进行第一次 rest 调用之前被创建了几次。请澄清为什么会发
给定网络应用程序中的以下情况: // EntityManager em, one per Request with Spring's OpenEntityManagerInViewFilter //
当我们在 JAVA EE 环境中的 EntityManager 上使用 @PersistenceContext 注释时,容器将创建 entityManagerFactory(我猜是整个 session
我遇到了一种情况(我认为这很奇怪,但可能很正常),我使用 EntityManager.getReference(LObj.getClass(), LObj.getId()) 来获取数据库实体,然后通过
我有以下服务... @Stateless @LocalBean public class RandomService { @EJB RandomString stringTokenizer;
我在实体类中有这个函数,但 getDoctrine 不喜欢...... public function getObject() { $em = $this->getDoctrine()->ge
我正在尝试以级联方式保存某个对象并检索它。 我有 3 个对象超过 3 个实体。 实体: class Order { /** * @var integer $id *
我正在开发一个 JPA 应用程序(使用 hibernate ),我正在与 作斗争。自动冲洗 特征。 默认情况下,每当我们处理对任何实体的查询时,完整的 实体管理器 被冲洗。在大多数情况下这是可以的:我
我刚刚建立了一个到目前为止仍然非常小的项目 maven/jpa/hibernate 项目,我试图在其中持久化一个对象。 我的类(class)是一个非常简单的类(class): @Entity publ
我当前的项目使用 HSQLDB2.0 和 JPA2.0 。 场景是:我查询数据库以获取 contactDetails 的列表的 person .我删单contactInfo在 UI 中,但不保存该数据
我是 jpa 和 spring 世界的新手,我目前正在对一个简单的方法进行一些单元测试,但只有当我在单元测试模式下运行我的测试类时才会继续收到此错误消息: java.lang.IllegalState
我是一名优秀的程序员,十分优秀!