Announcement Announcement Module
Collapse
No announcement yet.
Newbie: Hibernate Entity Manager problem Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Newbie: Hibernate Entity Manager problem

    I'm trying to use JPA with hibernate entity manager in my application, but every time I invoke any method on an entity manager bean, I get a nullpointerexception.

    Here's my applicationContext.xml:

    Code:
    <?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:context="http://www.springframework.org/schema/context"
    	xmlns:tx="http://www.springframework.org/schema/tx"
    	xmlns:aop="http://www.springframework.org/schema/aop"
    	xsi:schemaLocation="
    	    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
       		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
    		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
    		http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
    
    
    	
    	
    	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
    		<property name="url" value="jdbc:mysql://localhost:3306/bugs" />
    		<property name="username" value="bugs" />
    		<property name="password" value="bugs/>
    	</bean>
    	<bean id="emf" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    		<property name="dataSource" ref="dataSource" />
    		<property name="persistenceUnitName" value="bugsUnit" />
    		<property name="jpaVendorAdapter">
    			<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
    				<property name="showSql" value="true" />
    				<property name="generateDdl" value="true" />
    				<property name="database" value="MYSQL" />
    			</bean>
    		</property>
    	</bean>
    	<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    		<property name="entityManagerFactory" ref="emf" />
    	</bean>
    	
    	<bean id="entityController" class="org.bugs.controller.EntityController" scope="session"/>
    	<bean id="user" class="org.bugs.session.User" scope="session">
    		<property name="controller" ref="entityController"/>
    	</bean>
    <tx:annotation-driven />
    	
    </beans>
    persistence.xml:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <persistence version="1.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_1_0.xsd">
    	<persistence-unit name="bugsUnit" transaction-type="RESOURCE_LOCAL">
    		<provider>org.hibernate.ejb.HibernatePersistence</provider>
    	</persistence-unit>
    </persistence>
    EntityController.java:
    Code:
    package org.bugs.controller;
    
    import java.util.List;
    
    import javax.persistence.EntityManager;
    import javax.persistence.PersistenceContext;
    
    import org.springframework.transaction.annotation.Transactional;
    
    
    public class EntityController {
    
    
    	@PersistenceContext
        private EntityManager em;
    	
    	@SuppressWarnings("unchecked")
    	@Transactional
    	public <T> List<T> getList(Class<T> clase){
    		return em.createQuery("select p from "+clase.getSimpleName()+" p").getResultList();
    	}
    	@Transactional
    	public void save(Object objeto){
    		em.persist(objeto);
    	}
    	@Transactional
    	public void delete(Object objeto){
    		em.remove(objeto);
    	}
    
    	
    }
    User.java:
    Code:
    package org.bugs.session;
    
    import java.util.Date;
    
    ...
    
    public class User {
    
    	private EntityController controller;
    	
           ...
    	public void setController(EntityController controller) {
    		this.controller= controller;
    	}
    	public EntityController getController() {
    		return controller;
    	}
    	public void saveEntity() {
    		MyEntity entity = new MyEntity();
    		...
    		controller.save(entity);
    	}
            ...
    }
    Anytime I invoke the saveEntity method, I get a NullPointerException in the controller.save(entity) line.

    I'm new to Spring and JPA so I must have made a mistake somewhere.

    Anybody can help me?

  • #2
    You need to add <context:annotation-config/> to your applicationContext.xml

    Comment


    • #3
      Thanks, it works now.
      I had to add
      Code:
      <tx:annotation-driven transaction-manager="transactionManager"/>
      to my Spring configuration file too, so transaction management would work.

      Comment

      Working...
      X