Spring + Jpa CRUD操作entityManager对象抛出空指针异常

时间:2014-02-03 12:02:43

标签: spring jpa entitymanager

这是我的CRUD操作界面 并使用此添加,更新,删除,搜索操作

 package com.cnsi.dao;

    import java.util.List;

    import com.cnsi.modal.User;


    public interface UserDao {
        public void add(User u);
        public void edit(User u);
        public void delete(int id);
        public User getInterfaceId(int id);
        public List<?> getAllInterface();
        }
     this interface will help me to achieve abstraction

实施类

此类实现添加,更新,删除,搜索方法

package com.cnsi.dao.Impl;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;

import org.springframework.stereotype.Repository;

import com.cnsi.dao.UserDao;
import com.cnsi.modal.User;

@Repository
public class JpaUserDaoImpl implements UserDao {


    EntityManagerFactory entityManagerFactory;

    EntityManager em;



    @PostConstruct
    public void init() {
        em=entityManagerFactory.createEntityManager();
    } 

    @Override
    public void add(User u) {
        em.persist(u);
    }

    @Override
    public void edit(User u) {
        em.merge(u);

    }

    @Override
    public void delete(int id) {


        em.remove(getInterfaceId(id));

    }

    @Override
    public User getInterfaceId(int id) {

        return (User)em.find(User.class, id);
    }

    @Override
    public List<User> getAllInterface() {

        return em.createQuery("from user").getResultList();
    }

}

我的spring-context文件,

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:jee="http://www.springframework.org/schema/jee" xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        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-2.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="com.javapapers.spring.mvc" />
    <context:component-scan base-package="com.cnsi.dao.Impl" />


    <mvc:annotation-driven />

    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/" />
        <property name="suffix" value=".jsp" />
    </bean>
    <!-- <jee:jndi-lookup id="entityManagerFactory" jndi-name="persistence/spitterPU" 
        /> -->

    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
        destroy-method="close">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver" />
        <property name="url" value="jdbc:oracle:thin:@10.86.24.7:1521:wahipaaD" />
        <property name="username" value="mddemo" />
        <property name="password" value="mddemo" />
    </bean>

    <bean id="jpaVendorAdapter"
        class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        <property name="database" value="ORACLE" />
        <property name="showSql" value="true" />
        <property name="generateDdl" value="false" />
        <property name="databasePlatform" value="org.hibernate.dialect.OracleDialect" />
    </bean>


    <bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect" />

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

    </bean>

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

    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="persistenceXmlLocation" value="classpath:META-INF/ spring-persistence.xml" />
        <property name="persistenceUnitName" value="personPersistenceUnit" />
        <property name="dataSource" ref="dataSource" />
        <!-- <property name="jpaVendorAdapter" ref="jpaVendorAdapter" /> -->
        <property name="jpaDialect" ref="jpaDialect" />
    </bean>
    <!-- <bean class="com.cnsi.dao.Impl.JpaUserDao" id="jpaUserDao">
        <property name="entityManagerFactory" ref="entityManagerFactory" />
    </bean> -->

</beans>

但是当我尝试获取实体管理器对象'entityManagerFactory.createEntityManager()'这个方法时,我得到空指针异常

我在jsp下面用于添加

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>

<?xml version="1.0" encoding="ISO-8859-1" ?>

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Add team page</title>
</head>
<body>
    <h1>Add User page</h1>
    <p>Here you can add a new User.</p>
    <form:form method="POST" commandName="user"
        action="${pageContext.request.contextPath}/view/add">
        <table>
            <tbody>
                <tr>
                    <td>User Name:</td>
                    <td><form:input path="name" /></td>
                </tr>
                <tr>
                    <td>Id:</td>
                    <td><form:input path="id" /></td>
                </tr>
                <tr>
                    <td><input type="submit" value="Add" /></td>
                    <td></td>
                </tr>
            </tbody>
        </table>
    </form:form>

    <p>
        <a href="${pageContext.request.contextPath}/index.html">Home page</a>
    </p>
</body>
</html>

在jsp下面用于编辑

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>

<?xml version="1.0" encoding="ISO-8859-1" ?>

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Edit team page</title>
</head>
<body>
    <h1>Edit team page</h1>
    <p>Here you can edit the existing team.</p>
    <p>${message}</p>
    <form:form method="POST" commandName="user"
        action="${pageContext.request.contextPath}/view/edit/${user.id}.html">
        <table>
            <tbody>
                <tr>
                    <td>User Name:</td>
                    <td><form:input path="name" /></td>
                </tr>
                <tr>
                    <td>Id:</td>
                    <td><form:input path="id" /></td>
                </tr>
                <tr>
                    <td><input type="submit" value="Edit" /></td>
                    <td></td>
                </tr>
            </tbody>
        </table>
    </form:form>

    <p>
        <a href="${pageContext.request.contextPath}/home.html">Home page</a>
    </p>
</body>
</html>

我得到零点异常请帮帮我 做简单的crud操作

2 个答案:

答案 0 :(得分:1)

您不会将entityManagerFactory注入您的DAO,因此它在init()方法中为空。

请更改:

EntityManagerFactory entityManagerFactory;

为:

@Autowired
EntityManagerFactory entityManagerFactory;

答案 1 :(得分:0)

由于它是在spring-context中配置的,因此您不需要init方法。简单地说

@PersistenceContext
private EntityManager em;

它应该有用。