java


EntityManager is null with XMLless JPA 2.1


I'm migrating from Hibernate to JPA 2.1, and I followed this tutorial, that creates a JPA configuration without persistence.xml
But the EntityManager is null when Tomcat is starting.
All my entities are annotated with #Entity.
Here is my applicationContext.xml:
<context:component-scan
base-package="br.com.foo, br.com.bar"/>
<bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactoryBean" />
</bean>
<tx:annotation-driven/>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="jdbc:mysql://localhost:3306/myDb" />
<property name="username" value="root" />
<property name="password" value="root" />
</bean>
<bean id="entityManagerFactoryBean" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="packagesToScan" value="br.com.foo.persistence" />
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
</property>
<property name="jpaProperties">
<props>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
</props>
</property>
</bean>
...
GenericDAO, all other DAO inherit from it:
package br.com.bar;
#Repository
#Transactional
public abstract class GenericDAOImpl<Entity> implements GenericDAO<Entity> {
#PersistenceContext
private EntityManager manager;
protected SessionFactory sessionFactory;
public GenericDAOImpl() {
this.sessionFactory = manager.unwrap(SessionFactory.class); //this line throws NullPointerException
}
...
An Entity:
package br.com.foo.persistence;
#Entity
#Table(name = "USER")
#Inheritance(strategy = InheritanceType.JOINED)
public class User extends BaseEntity {
private static final long serialVersionUID = 3966629079539419005L;
#Column(name = "NAME", length = 200)
private String name;
The pom.xml
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>${hibernate.version}</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.1-api</artifactId>
<version>1.0.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>${hibernate.version}</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.2.0.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate.common</groupId>
<artifactId>hibernate-commons-annotations</artifactId>
<version>4.0.1.Final</version>
<classifier>tests</classifier>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-c3p0</artifactId>
<version>3.3.1.GA</version>
</dependency>
Stack Trace of the error:
GRAVE: Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'companyDAOImpl' defined in file [C:\Users\marcelo\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\evcash-core\WEB-INF\classes\br\com\foo\CompanyDAOImpl.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [br.com.foo.dao.CompanyDAOImpl]: Constructor threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1110)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1055)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:751)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:861)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:541)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:444)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:326)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4939)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5434)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
at java.util.concurrent.FutureTask.run(FutureTask.java:262)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [br.com.bar.dao.CompanyDAOImpl]: Constructor threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:154)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:89)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:1103)
... 22 more
Caused by: java.lang.NullPointerException
at br.com.bar.dao.impl.GenericDAOImpl.<init>(GenericDAOImpl.java:39)
at br.com.foo.dao.CompanyDAOImpl.<init>(CompanyDAOImpl.java:19)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:526)
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:142)
... 24 more
CompanyDAOImpl:
package br.com.foo.dao;
#Repository
public class CompanyDAOImpl
extends GenericDAOImpl<Company>
implements CompanyDAO{
#Override
protected Class<Company> getClazz() {
return Company.class;
}
...
The instance of EntityManager manager isn't defined inside the constructor. Spring injects the dependencies after the object is instantiated.

Related Links

Not enough space for ad banner
Adding mongod “where” Clause in java
Java String comparison ALWAYS returns false
Creating accessors in Java
Failure making a POST after doing a GET in Java, and adding a header request
Java Swing Gridbaglayout look
Java in Oracle database
Java Filling an array with infinite variable names
possible values of port numbers of Tomcat and required changes in server.xml
Java 8 get field of contained Object
Sort XML alphabetically while mantaining XSD schema validity
use Java Monitor instead of Semaphore
How to work with regex to check a content of String
JSTL forEach doesn't loop [duplicate]
phonebook with arraylist in java
ArrayList<String> getIntent().getStringArrayListExtra

Categories

HOME
java
gerrit
onedrive
iot
jira
analysis
icloud
django-imagekit
constraint-programming
node-notifier
esper
applepay
medical
node-pdfkit
windows-azure-storage
spring-tool-suite
vb.net-2010
messages
saxon
http-status-code-504
flux
wkwebview
telerik-reporting
dxf
frame
yadcf
karaf
jspresso
typed.js
scaffold
xcode-extension
retina-display
hue
webdriver-manager
sfdc
ncalc
jvm-languages
spring-security-kerberos
jsch
termination
akka-cluster
pdfclown
color-profile
orthogonal
migradoc
parallel-data-warehouse
hill-climbing
whois
bgp
yaws
dataview
independentsoft
natvis
jfugue
hsv
python-3.2
retina
fluid-dynamics
directoryservices
nstableviewcell
nsbutton
asp.net-web-api-odata
p4java
jquery-layout
poller
listings
buster.js
type-equivalence
simba
runas
automount
postgresql-performance
spyware
calling-convention
subviews
mirah
sef
project-hosting
geneva-server

Resources

Database Users
RDBMS discuss
Database Dev&Adm
javascript
java
csharp
php
android
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App