• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

spring wont inject the entitymanager

 
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi there,

i have a Dao with its DaoImpl and a controller.. and of course a view...

when i try to access the database in my controller i always receive a nullpointerexception and i have no idea why
what i've seen is that my persistence.xml makes some trouble:


but what is not supported? i'm really confused...

so this is my persistence,xml:



and this is my applicationContext.xml


please help me out, i have no idea whats wrong
i also tried to rename transactionManager to entityManager but then spring complains about missing transactionManager
 
olze oli
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh i forgot to add the bean
but i have now the problem that i dont know how i can tell spring to inject the correct instance of the entitymanager


wont work it says:
 
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Type mismatch, inject EntityManager to EntityManager or EntityManagerFactory to EntityManagerFactory.
 
olze oli
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i know that its a type mismatch but how can i build my entityManager bean?
when i try to do something like: <bean id="entityManager" class="javax.persistence.EntityManager"> it says it is an interface and not a class...
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unfortunately that isn't possible. Your Dao should have a reference to EntityManagerFactory and not EntityManager, Spring cannot create EntityManagers for you and inject them.

The pattern is



So a em factory gets injected into your DAO/Repository and your methods get an EntityManager from it

Mark
 
olze oli
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ok that sounds logic and clears a lot of my confused thinking...
i'll try this tomorrow

thanks!
 
olze oli
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it doesnt solve the problem.. here's the code:


from my dao and my applicationContext:


the error:


 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can't have

@PersistenceContext(unitName = "MontyBroganPU")
private EntityManager entityManager;

@PersistenceContext(unitName = "MontyBroganPU")
public void setEntityManager(EntityManager entityManager) {
this.entityManager = entityManager;
}

remove that from your Dao.

Also you don't need @PersistenceContext on both your setter and your property. Choose one or the other, for what you have for now choose on the setter method.

Mark
 
olze oli
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this is killing me... its still not working...

my complete applicationContext


my LectureDaoImpl


and the error:



 
olze oli
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and what confuses me further... http://stackoverflow.com/questions/1310087/injecting-entitymanager-vs-entitymanagerfactory

If you are using Spring, it is better to inject EntityManagers instead of EntityManagerFactory.


 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

olze oli wrote:and what confuses me further... http://stackoverflow.com/questions/1310087/injecting-entitymanager-vs-entitymanagerfactory

If you are using Spring, it is better to inject EntityManagers instead of EntityManagerFactory.




I'd ignore that one answer. EntityManager injection is not the way to go. Trust me, in my first days of working at SpringSource I asked them about this and they said inject the EntityManagerFactory.

What happened to




I would expect to see exactly that.

Mark
 
olze oli
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i changed it to exactly what you wrote and commented out the rest but its still complaining:


i dont understand why its still trying to inject EntityManager
when i comment out the:

it deploys without any error, but then it doesnt inject the EntityManager/Factory and i get a NullPointerException

can you give me any hint where i should look at? i did read the API Doc of PersistenceAnnotationBeanPostProcessor but i didnt find anything matching my problem

thanks for helping
 
Hong Anderson
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure about the Annotation?
I think it should be PersistenceUnit instead of PersistenceContext.
 
olze oli
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yes that was a mistake
PersistenceContext is for EntityManager and PersistenceUnit for EntityManagerFactory...
but EntityManagerFactory is still null
i just dont know why it does not inject correctly?
do i have to create the EntityManagerFactory somewhere else?
 
Hong Anderson
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you uncommented PersistenceAnnotationBeanPostProcessor?
 
olze oli
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i commented it out for testing but currently its not because this is obligatory for DI
 
olze oli
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i can upload the .war if someone need it...
maybe its some strange config i created... but i have no idea what could solve this
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK

I was expecting a class like to declare your EntityManagerFactory bean to be injected.

LocalContainerEntityManagerFactoryBean

Looking at the Javadocs for PersistenceAnnotationBeanPostProcessor

It stated

"This post-processor will inject sub-interfaces of EntityManagerFactory and EntityManager if the annotated fields or methods are declared as such. The actual type will be verified early, with the exception of a shared ("transactional") EntityManager reference, where type mismatches might be detected as late as on the first actual invocation. "

Hmmm type mismatches.

I also found this interesting

"Note: A default PersistenceAnnotationBeanPostProcessor will be registered by the "context:annotation-config" and "context:component-scan" XML tags."

This last one would explain why you want to have the PersistenceAnnotationBeanPostProcessor commented out because you already have the context:annotation-config tag

Mark

 
olze oli
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i tried it now with @Autowired on the field / on the setter method of the Factory and included this in my applicationContext:

and removed the LectureDaoImpl bean and the BeanPostProcessor... still nullpointerexception...
 
Hong Anderson
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you try not use Annotation, and inject EntityManagerFactory manually (config in XML)?
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you in an App server or in Tomcat. Basically I am asking it is local or container manager EntityManagerFactory?

Mark
 
olze oli
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i tried it without annotations but i get the same NullPointer
i'm creating a portlet for liferay and liferay runs on glassfish v2
my IDE is netbeans 6.7.1 on ubuntu 9.10 x86_64

i uploaded the file to http://217.172.187.219/MontyBrogan.tar.bz2
so if someone wants to take a look at it..
 
Hong Anderson
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please post your current configuration.
 
olze oli
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if you mean the applicationContext,xml:



[Sorry, I edited out your user/password just for extra protection. Mark]
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, just a couple of comments.

Where is your Service layer, you have a DAO directly in your Controller and skip the Service Layer. The Service layer is the Use Case business logic layer and is the most common, and in my opinion, best place to start your transactions.

Also, having both

<bean id="lectureDao" class="DaoImpl.LectureDaoImpl">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>

and

<bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

Seems redundant to me. Since you have an injection of the entityManagerFactory directly into the lectureDao bean, why do you also need the Annotation of @PersistenceContext for the PersistenceAnnotationBeanPostProcessor?

Mark
 
Hong Anderson
Ranch Hand
Posts: 1936
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Use this current configuration, remove all Annotations from LectureDaoImpl, and debug setEntityManagerFactory, I expect it should get called.
 
olze oli
Ranch Hand
Posts: 187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
first of all thanks alot for your help

@mark
ganzgeheim is german and means "topsecret", so i think this shouldnt be a security problem ;)
i'm really new to spring and this web/portlet development... and i thought the LectureDao is my service layer?

@Kengkaj
will try it this evening...
edit: entityManagerFactory is set

btw. what i did: removed all annotations from LectureDaoImpl, removed <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />

the lectureDao in my controller is also set (seen during debugging), but when i try to access the portlet, lectureDao is null
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dao stands for Data Access Object, so this the is data access layer. the typical architecture is

Controller
|
Service
|
Dao/Repository data access layer.

Controller just has Web UI code. Service layer has business logic, and Dao only has data access code.

Service layer is where you set your transactional boundaries. But don't worry too much about that now, lets get everything working for you before you refactor.

Mark
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic