当前位置: 首页 > 知识库问答 >
问题:

JavaEE/JPA@PersistenceContext(unitName=)不注入Hibernate实体管理器实现

孙朗
2023-03-14

目前,我学习JavaEE JPA规范。我使用Hibernate作为JPA实现。我有下一个问题:
我有一个简单的实体

@Entity
public class Book {

@Id
@GeneratedValue
private Long id;

private String title;

private String description;

private Float unitCost;

public Book() {

}
// getters and setters
}

还有一个简单的服务:

public class BookService {

@PersistenceUnit(unitName = "book_store")
private EntityManager entityManager;

public void create(Book book) {
    entityManager.persist(book);
    entityManager.flush();
}
}

persistence.xml有下一个视图:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<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_2.xsd"
         version="2.2">
    <persistence-unit name="book_store">
    <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
    <class>com.example.jpatest1.Book</class>
    <properties>
        <property name="javax.persistence.jdbc.driver" 
    value="com.mysql.jdbc.Driver"/>
        <property name="javax.persistence.jdbc.password" 
    value="somePasss"/>
        <property name="javax.persistence.jdbc.user" value="sammy"/>
        <property name="javax.persistence.jdbc.url"
                  value="jdbc:mysql://localhost:3306/book_store? 
    autoReconnect=true&amp;useSSL=false"/>
        <property name="hibernate.dialect" 
    value="org.hibernate.dialect.MySQL5Dialect"/>
        <property name="hibernate.show_sql" value="true"/>
        <property name="hibernate.hbm2ddl.auto" value="create-drop"/>
    </properties>
    </persistence-unit>
</persistence>

我不明白为什么Hibernate会话没有注入BookService。在作为EntityManager的图书服务中,我得到了org.jboss.as.TransactionScopedEntityManager而不是来自Hibernate的SessionImpl。为什么使用@Peristence Context(unitName=)我没有获得Hibernate EntityManager实现?
此外,我在github上固定链接:代码源代码上的链接

共有1个答案

关宏毅
2023-03-14

对象字段entityManager的类型是EntityManager,因此您会注入您在该字段中声明的内容。

要从 EntityManager 获取会话,您可以尝试以下操作:

Session session = entityManager.unwrap(Session.class);

 类似资料:
  • 问题内容: 我在数据访问层中将JPA-2.0与Hibernate一起使用。 为了进行审核日志记录,我通过在persistence.xml中配置以下属性来使用Hibernate的EmptyInterceptor: 凡 AuditLogInterceptor 扩展Hibernate的’ org.hibernate.EmptyInterceptor ‘。 我在数据访问层中使用JPA实体管理器来执行数据库

  • 我有一个在Eclipse中定义的非常基本的JavaEE应用程序(一个EJB,一个带有PrimeFaces的网页,一个JPA实体)。它有四个项目,一个用于EAR,一个用于EJB,一个用于JPA,一个用于Web部件。问题是,我试图在JPA项目的persistence.xml中配置HiberNate作为持久化框架,但它并没有像我预期的那样完全工作。在我的EJB(@Statless)中,我可以通过两种方式

  • 我试图使用spring boot将EntityManager注释注入到我的DAO中,但是得到一个< code > InvalidDataAccessApiUsageException 消息,说没有可用的事务EntityManager。我的印象是,只要Spring Boot从< code>application.yml中获得了我的数据源信息,并且我用< code>@PersitenceContext

  • 我一直在研究很多类似的问题,这些问题并没有反映我的确切问题。如果我忽略了有人已经解决了这个问题,请让我知道。 我目前正在Wildfly 10.1上将一个旧的EJB CMP bean迁移到JPA。这里的问题是我的无状态会话beans中的entitymanager没有注入@PersistenceContext,使EntityManager为空。我尝试使用EntityManagerFactory来解决这

  • 我有一个EAR应用程序(要部署在WebLogic12c上),它有一个“持久化”组件。“persist”组件使用JPA(实现:EclipseLink)来持久化对象。 使用entityManager的bean声明为,实体管理器通过注释注入。 问题是,我每次尝试访问entityManager时都有一个(意思是他没有被正确注入)。 persistence.xml 我在常规代码中是这样使用的: MyBean

  • 我刚刚开始使用Spring ROO,并使用数据库逆向工程命令生成了我的实体类。然而每当我试图调用生成的实体类中的一个CRUD方法时,我总是得到这个异常:Java . lang . illegalstateexception:实体管理器没有被注入(Spring Aspects JAR是否被配置为AJC/AJDT方面库?) 我怀疑(通过查看生成的文件)EntityManager没有被注入到类中。你能告