null entitymanager

f45qwnt8  于 2021-07-03  发布在  Java
关注(0)|答案(0)|浏览(283)

我目前正在开发一个应用程序,它使用restfulweb服务访问数据库,并与ejb集成。尽管我做了很多尝试,@persistencecontext注入没有工作,而且m y entitymanager对象总是设置为null。我已经检查了ejb项目中的persistence.xml,并保证注解中使用的名称和persistence unit标记都具有相同的值。
下面是persistence.xml文件的代码以及使用注入的ejb类。

<persistence version="2.1"
    xmlns="http://xmlns.jcp.org/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
    <persistence-unit name="TestPersistence"
        transaction-type="JTA">
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <jta-data-source>java:jboss/datasources/postgresDS</jta-data-source>
        <properties>
            <property name="hibernate.dialect"
                value="org.hibernate.dialect.PostgreSQLDialect" />
            <property name="hibernate.hbm2ddl.auto" value="update" />
            <property name="hibernate.transaction.jta.platform"
                value="org.hibernate.service.jta.platform.internal.JBossAppServerJtaPlatform" />
        </properties>
    </persistence-unit>
</persistence>

Java Class that used @PersistenceContext annotation:

public class InstitutionBean implements InstitutionBeanRemote, InstitutionBeanLocal {    
    /**
     * Default constructor. 
     */
    public InstitutionBean() {
        // TODO Auto-generated constructor stub
    }

    @PersistenceContext(name = "TestPersistence")
    private EntityManager em;

    public String getAll() {

        //try {
            String jpql = "SELECT * FROM institution i";

            System.out.println("[EM] " + em);

            TypedQuery<Institution> typedQuery = em.createQuery(jpql, Institution.class);

            System.out.println("[In getAll] After QueryCreation getAll()");

            List<Institution> institutionsList = typedQuery.getResultList();
            String reply = "";

            System.out.println("[In getAll] Hello after query");

            System.out.println("Isnstitutions List: " + institutionsList);

            reply += "Institutions: \n\n";

            for(Institution i: institutionsList) {
                reply += i.toString();
            }

            System.out.println("[In getAll] Hello before returning");

            return reply;

        /*} catch (Exception e) {
            e.getStackTrace();
            return "Error in query";
        }*/
    }
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题