File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Spring and the fly likes spring wont inject the entitymanager Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "spring wont inject the entitymanager" Watch "spring wont inject the entitymanager" New topic
Author

spring wont inject the entitymanager

olze oli
Ranch Hand

Joined: Jun 20, 2009
Posts: 148
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

Joined: Jun 20, 2009
Posts: 148
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:
Hong Anderson
Ranch Hand

Joined: Jul 05, 2005
Posts: 1936
Type mismatch, inject EntityManager to EntityManager or EntityManagerFactory to EntityManagerFactory.


SCJA 1.0, SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCJP 5.0, SCEA 5, SCBCD 5; OCUP - Fundamental, Intermediate and Advanced; IBM Certified Solution Designer - OOAD, vUML 2; SpringSource Certified Spring Professional
olze oli
Ranch Hand

Joined: Jun 20, 2009
Posts: 148
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...
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

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


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
olze oli
Ranch Hand

Joined: Jun 20, 2009
Posts: 148
ok that sounds logic and clears a lot of my confused thinking...
i'll try this tomorrow

thanks!
olze oli
Ranch Hand

Joined: Jun 20, 2009
Posts: 148
it doesnt solve the problem.. here's the code:


from my dao and my applicationContext:


the error:


Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

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

Joined: Jun 20, 2009
Posts: 148
this is killing me... its still not working...

my complete applicationContext


my LectureDaoImpl


and the error:



olze oli
Ranch Hand

Joined: Jun 20, 2009
Posts: 148
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
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

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

Joined: Jun 20, 2009
Posts: 148
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

Joined: Jul 05, 2005
Posts: 1936
Are you sure about the Annotation?
I think it should be PersistenceUnit instead of PersistenceContext.
olze oli
Ranch Hand

Joined: Jun 20, 2009
Posts: 148
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

Joined: Jul 05, 2005
Posts: 1936
Have you uncommented PersistenceAnnotationBeanPostProcessor?
olze oli
Ranch Hand

Joined: Jun 20, 2009
Posts: 148
i commented it out for testing but currently its not because this is obligatory for DI
olze oli
Ranch Hand

Joined: Jun 20, 2009
Posts: 148
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
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

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

Joined: Jun 20, 2009
Posts: 148
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

Joined: Jul 05, 2005
Posts: 1936
Can you try not use Annotation, and inject EntityManagerFactory manually (config in XML)?
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

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

Joined: Jun 20, 2009
Posts: 148
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

Joined: Jul 05, 2005
Posts: 1936
Please post your current configuration.
olze oli
Ranch Hand

Joined: Jun 20, 2009
Posts: 148
if you mean the applicationContext,xml:



[Sorry, I edited out your user/password just for extra protection. Mark]
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

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

Joined: Jul 05, 2005
Posts: 1936
Use this current configuration, remove all Annotations from LectureDaoImpl, and debug setEntityManagerFactory, I expect it should get called.
olze oli
Ranch Hand

Joined: Jun 20, 2009
Posts: 148
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
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: spring wont inject the entitymanager