Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/43.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用EntityManagerFactory时发生NullPointerException_Java_Hibernate_Jpa_Entitymanager_Hibernate Entitymanager - Fatal编程技术网

Java 使用EntityManagerFactory时发生NullPointerException

Java 使用EntityManagerFactory时发生NullPointerException,java,hibernate,jpa,entitymanager,hibernate-entitymanager,Java,Hibernate,Jpa,Entitymanager,Hibernate Entitymanager,我的目标是从applicationContext.xml文件实例化EntityManagerFactory,以在SQL数据库中注册所有帖子。 以下是主要文件的内容: applicationContext.xml <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.spr

我的目标是从applicationContext.xml文件实例化EntityManagerFactory,以在SQL数据库中注册所有帖子。 以下是主要文件的内容:

applicationContext.xml

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- Properties files linkers -->

    <context:property-placeholder location="/WEB-INF/resources/database/jdbc.properties"/>
    <context:property-placeholder location="/WEB-INF/resources/database/hibernate.properties"/>

    <!-- Config database - initialization -->

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.driverClassName}" />
        <property name="url" value="${jdbc.url}" />
        <property name="username" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!-- Three main layers definition -->

    <context:annotation-config />
    <context:component-scan base-package="com.zone42"/>

    <!-- Transaction sub-system initialization -->

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
        <property name="entityManagerFactory" ref="entityManagerFactory"/>
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager" />

</beans>

(WEB-INF/classes/)persistence.xml

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0" xmlns="http://java.sun.com/xml/ns/persistence"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
    <persistence-unit name="post-unit" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>
        <class>com.zone42.model.Post</class>
        <exclude-unlisted-classes>true</exclude-unlisted-classes>
    </persistence-unit>
</persistence>

org.hibernate.ejb.HibernatePersistence
com.zone42.model.Post
真的
java

public class PostDAO extends GenericDAOEntity<Post> implements IPostDAO
{

}
公共类PostDAO扩展了genericdao实体实现了IPostDAO
{
}
GenericDAOEntity.java

@Transactional
public class GenericDAOEntity<T> implements IGenericDAO<T>
{
    /**
     * Properties
     */

    @Autowired
    @PersistenceContext(unitName="post-unit")
    private EntityManagerFactory entityManagerFactory/* = Persistence.createEntityManagerFactory(persistence_unit_name)*/;

    //Get all posts

    @SuppressWarnings("unchecked")
    public List<T> findAll(Class<T> obj) {
        EntityManager entityManager = this.entityManagerFactory.createEntityManager();
        Query query = entityManager.createQuery("from " + obj.getSimpleName());
        return (query.getResultList());
    }

    /**
     * Accessors
     */

    public EntityManagerFactory getEntityManagerFactory() {
        return entityManagerFactory;
    }

    @PersistenceContext(unitName="post-unit")
    public void setEntityManagerFactory(EntityManagerFactory entityManagerFactory) {
        this.entityManagerFactory = entityManagerFactory;
    }   
}
@Transactional
公共类GenericDAOEntity实现IGenericDAO
{
/**
*性质
*/
@自动连线
@PersistenceContext(unitName=“post unit”)
私有EntityManagerFactory EntityManagerFactory/*=Persistence.createEntityManagerFactory(Persistence\u unit\u name)*/;
//获取所有帖子
@抑制警告(“未选中”)
公共列表findAll(obj类){
EntityManager EntityManager=此.EntityManager工厂.createEntityManager();
Query Query=entityManager.createQuery(“来自”+obj.getSimpleName());
返回(query.getResultList());
}
/**
*访问者
*/
公共EntityManagerFactory getEntityManagerFactory(){
返回实体管理工厂;
}
@PersistenceContext(unitName=“post unit”)
公共无效设置EntityManagerFactory(EntityManagerFactory EntityManagerFactory){
this.entityManagerFactory=entityManagerFactory;
}   
}

我尝试了几种配置组合,但没有成功。当我想从entityfactory实例创建EntityManager实例时,
NullPointerException
来自
findAll
方法。我想我的配置有问题。我希望在类中直接使用运算符new实例化EntityManagerFactory时,确定代码是否有效。现在,我只想选择另一种方式来分配工厂,即使用appionContext.xml文件中的xml。有人能帮我吗?提前感谢。

您需要将字段/设置器标记为
@Autowired
或在XML中显式连接引用:

<bean class="PostDAO">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>


如何获得
GenericDAOEntity
的实例?感谢您的回答,但注释不应出现在EntityManagerFactory声明的上方。我忘了擦掉这个声明。您还有其他想法吗?上下文:组件扫描包中是否有
PostDao