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

Toolbar cannot be customized as expected in android
Create a House using java
Regex on InputStream
com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field ā€œgā€
How to change add() in Treeset
Is it possible to pass parameter from java to xml android
eclipse plugin classpath issue
SimpleDateFormatter returns two different time zones
Java Object[] looping error
How to filter only additional character with Regular Expression in Java?
I am comparing return value of BufferedReader.readLine() and a string, but it's not working
Configure Mutiple DataSource in Spring Boot with JNDI
Difference between connect and bind in bootstrap : Netty
Print inside MapReduce class
IntelliJ Can't Extract a Constant using Keyboard Shortcut Ctrl+Alt+C
Is my understanding of HashMap correct?

Categories

HOME
testing
image
testng
image-processing
session
getelementsbytagname
jira
analysis
graphql
twitter-bootstrap-4
icloud
google-project-tango
disassembler
cross-browser
fingerprint
flyway4
directx
fancybox-3
serverless-framework
dosgi
finite-automata
flux
django-cms
nodatime
lldb
physics-engine
graphicsmagick
trading
url-scheme
applozic
spring-mybatis
minitab
opentype
srcset
react-chartjs
google-sites-2016
vsts-build-task
avcapturesession
atl
android-ble
dosbox
simplexml
bitbucket-pipelines
libraries
sqlite2
hue
ws-security
g-code
dynamic-reports
botbuilder
bootstrap-dialog
sas-jmp
websphere-mq-fte
cubic-spline
auto-update
jsch
termination
pubmed
python-c-api
hendrix
dropbox-php
dotnetzip
veracode
integrity
mu
drupal-6
nativeapplication
crypt
r-forge
flow-control
angular-strap
word-vba-mac
color-picker
grails-tomcat-plugin
blackberry-10
rvest
suffix-tree
oauth2client
associative-array
natvis
wso2cloud
jms2
wireshark-dissector
muse
mmc
composite
e10s
fscommand
pick
nstableviewcell
asp.net-dynamic-data
xcode-6.2
google-reader
mesa
heisenbug
rebol3
yorick
picturefill
bundles
monomac
apc
ril
orchardcms-1.7
delphi-6
selected
mhtml
coderush
runas
viewswitcher
jspinclude
coredump
servicehost
datareader
sortable-tables
bespin
brewmp
microsoft-virtualization

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App