Note that there are some explanatory texts on larger screens.

plurals
  1. POEntityManager is null in DAO class
    primarykey
    data
    text
    <p>I'm trying to build an application with Java Spring and hibernate. For the dao classes I'm using the generic dao pattern. When I try to execute a query a nullpointer exception is thrown because the entityManager is null. I think it's not injected properly but I don't know why.</p> <p>Application context:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"&gt; &lt;bean id = "klantDao" class="service.JpaKlantDao"/&gt; &lt;bean id="userDao" class="service.JpaUserDao"/&gt; &lt;bean id="assembler" class="service.Assembler" /&gt; &lt;/beans&gt; </code></pre> <p>Dispatcher servlet:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-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/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd"&gt; &lt;mvc:annotation-driven /&gt; &lt;context:component-scan base-package="controllers"/&gt; &lt;context:component-scan base-package="service"/&gt; &lt;mvc:resources mapping="/resources/**" location="/resources/" /&gt; &lt;jee:jndi-lookup id="dataSource" jndi-name="jdbc/CentricJavaDB"/&gt; &lt;bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"&gt; &lt;property name="dataSource" ref="dataSource" /&gt; &lt;property name="packagesToScan"&gt; &lt;list&gt; &lt;value&gt;domain&lt;/value&gt; &lt;/list&gt; &lt;/property&gt; &lt;property name="jpaVendorAdapter"&gt; &lt;bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"&gt; &lt;property name="showSql" value="true" /&gt; &lt;property name="generateDdl" value="true" /&gt; &lt;property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect"/&gt; &lt;/bean&gt; &lt;/property&gt; &lt;/bean&gt; &lt;bean id="transactionManager" class=" org.springframework.orm.jpa.JpaTransactionManager "&gt; &lt;constructor-arg ref="entityManagerFactory" /&gt; &lt;/bean&gt; &lt;tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/&gt; &lt;bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" /&gt; &lt;bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"&gt; &lt;property name="basename" value="mymessages" /&gt; &lt;/bean&gt; &lt;/beans&gt; </code></pre> <p>Web.xml:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;web-app version="3.1" 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/web-app_3_1.xsd"&gt; &lt;context-param&gt; &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt; &lt;param-value&gt; /WEB-INF/applicationContext.xml, /WEB-INF/spring-security.xml &lt;/param-value&gt; &lt;/context-param&gt; &lt;listener&gt; &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt; &lt;/listener&gt; &lt;servlet&gt; &lt;servlet-name&gt;dispatcher&lt;/servlet-name&gt; &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt; &lt;load-on-startup&gt;2&lt;/load-on-startup&gt; &lt;/servlet&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;dispatcher&lt;/servlet-name&gt; &lt;url-pattern&gt;*.htm&lt;/url-pattern&gt; &lt;/servlet-mapping&gt; &lt;!--Spring Security Configuration--&gt; &lt;filter&gt; &lt;filter-name&gt;springSecurityFilterChain&lt;/filter-name&gt; &lt;filter-class&gt;org.springframework.web.filter.DelegatingFilterProxy&lt;/filter-class&gt; &lt;/filter&gt; &lt;filter-mapping&gt; &lt;filter-name&gt;springSecurityFilterChain&lt;/filter-name&gt; &lt;url-pattern&gt;/*&lt;/url-pattern&gt; &lt;/filter-mapping&gt; &lt;session-config&gt; &lt;session-timeout&gt; 30 &lt;/session-timeout&gt; &lt;/session-config&gt; &lt;welcome-file-list&gt; &lt;welcome-file&gt;overzicht.jsp&lt;/welcome-file&gt; &lt;/welcome-file-list&gt; &lt;/web-app&gt; </code></pre> <p>The controller where I inject the dao:</p> <pre><code>@Controller public class LoginController { @Autowired private KlantDao klantDao; @RequestMapping(value="/login", method = RequestMethod.GET) public String klantInloggen(Model model) { String k; k = klantDao.findAll().get(0).getEmailadres(); return "login"; } } </code></pre> <p>The generic dao interface:</p> <pre><code>public interface GenericDao&lt;T&gt; { public List&lt;T&gt; findAll(); public T update(T object); public T get(Long id); public void delete(T object); public void insert(T object); public boolean exists(Long id) ; } </code></pre> <p>The generic dao implementation:</p> <pre><code>@Transactional public class JpaGenericDao&lt;T&gt; implements GenericDao&lt;T&gt; { private Class&lt;T&gt; type; private EntityManager em; public JpaGenericDao(Class&lt;T&gt; type) { super(); this.type = type; } @PersistenceContext public void setEntityManager(EntityManager em) { this.em = em; } @Transactional(readOnly = true) @Override public T get(Long id) { T entity = this.em.find(this.type, id); return entity; } @Transactional(readOnly = true) @Override public List&lt;T&gt; findAll() { return this.em.createQuery( "select entity from " + this.type.getName() + " entity").getResultList(); } //@Transactional @Override public void insert(T object) { em.persist(object); } //@Transactional @Override public void delete(T object) { em.remove(em.merge(object)); } @Transactional(readOnly = true) @Override public boolean exists(Long id) { T entity = this.em.find(this.type, id); return entity != null; } @Override public T update(T object) { return em.merge(object); } } </code></pre> <p>The Klant dao interface:</p> <pre><code>public interface KlantDao { public List&lt;Klant&gt; findAll(); } </code></pre> <p>The klant dao implementation:</p> <pre><code>public class JpaKlantDao extends JpaGenericDao&lt;Klant&gt; implements KlantDao { public JpaKlantDao() { super(Klant.class); } } </code></pre> <p>I hope someone can help me, I've been debugging for hours and I can't find what I'm doing wrong.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload