maouven
maouven

Reputation: 111

Hibernate - Spring, SessionFactory nullPointerException (when calling getCurrentSession())

I try to save my PartageDomain in my database using hibernate session factory, the problem is that in Session session = sessionFactory.getCurrentSession(); a nullPointerException is thrown. My dataSource is well configured, and I can already save/persist other objects with exactly the same way in this project, so I don't khow where the problem comes from.

a snapshot from the console exception :

javax.faces.FacesException: /pages/indexx.xhtml @28,72 listener="#{userMB.saveUserRights}": java.lang.NullPointerException
    at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:85)
    at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97)
    at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:114)

.....

javax.el.ELException: /pages/indexx.xhtml @28,72 listener="#{userMB.saveUserRights}": java.lang.NullPointerException
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:108)
    at com.sun.faces.facelets.tag.jsf.core.AjaxBehaviorListenerImpl.processAjaxBehavior(AjaxHandler.java:447)
    at javax.faces.event.AjaxBehaviorEvent.processListener(AjaxBehaviorEvent.java:109)
    at javax.faces.component.behavior.BehaviorBase.broadcast(BehaviorBase.java:98)
    at javax.faces.component.UIComponentBase.broadcast(UIComponentBase.java:764)
    at javax.faces.component.UIData.broadcast(UIData.java:911)

...

Caused by: java.lang.NullPointerException
    at com.stage.dao.PartageDaoImpl.add(PartageDaoImpl.java:35)
    at com.stage.beans.UserManagedBean.saveUserRights(UserManagedBean.java:224)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at com.sun.el.parser.AstValue.invoke(AstValue.java:234)
    at com.sun.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:297)
    at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:102)
    ... 23 more

here is part of code from the partageDomainImpl class :

@Repository
public class PartageDaoImpl implements PartageDao, Serializable {
@Resource(name = "sessionFactory")
    private SessionFactory sessionFactory;
// sessionFactory getter and setter :)

public void add(PartageDomain partageDomain) {      System.out.println(partageDomain.getPartageId().getUserDomain().getFirstName()); // I get this
        Session session = sessionFactory.getCurrentSession();
        // Save
        try {
            session.persist(partageDomain);
        } catch (Exception e) {
            session.saveOrUpdate(partageDomain);
        }
    }

Note that the exception is not caused by the EL langage, in fact I get the object correctly by printing it before calling the getsessionFactory method from which come the exception

in my PartageDomain class I have :

@Entity
public class PartageDomain implements Serializable {
// the PartageDomain properties, getters and setters ....

In fact I'm showing that to you to mention that I'm using annotation to manage dependencies and injections, concerning my session factory, I declared it in my configuration file as

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"
                 p:dataSource-ref="dataSource"
                 p:configLocation="${hibernate.config}"
                 p:packagesToScan="com.stage"/>

Upvotes: 2

Views: 4601

Answers (2)

Boris Treukhov
Boris Treukhov

Reputation: 17774

Just a guess - It is possible that you have a conflict in your context files.

I think that your @Repository annotated bean is somehow being created in the servlet application context, while <tx:annotation-driven transaction-manager="transactionManager" /> is declared in the root web app context.

In other words the @Transactional works only in the context where tx:annotation-driven is declared.

I suppose that it's possible for some the beans to be autoscanned and created twice making them to exist both in the web application and servlet application contexts, if autoscanning is present in both context configurations.

So the solution may be to check that all beans are created once and in the right places - DAOs in the root context, Controllers in the servlet context etc.

update Also please note that this code uses catch em all exception handling antipattern and possibly accesses hibernate session after it has raised an exception which is not supported by Hibernate as I know.

Upvotes: 1

maouven
maouven

Reputation: 111

I finally found the problem, in fact, it's my partageServiceImpl that has the sessionfactory property managed by spring-hibernate injection system.

In my userManagedBean, I was calling directly the the PartageDaoImpl.add method to add the partageDomain object, the correct thing is to call the PartageServiceImpl.add which has the sessionFactory property, in this whay the currentSession will not be null

I admit that it's a grave error that I made and It costs to me the whole day trying to solving it, so I hope this error will be faslty corrected by the others when seeying the exception that I have.

Upvotes: 1

Related Questions