我嘗試在我的數據庫中使用hibernate會話工廠保存我的PartageDomain
,問題是在Session session = sessionFactory.getCurrentSession();
中出現nullPointerException
。我的dataSource配置得很好,我已經可以在這個項目中以完全相同的方式保存/保存其他對象,所以我不知道問題來自哪裏。Hibernate - Spring,SessionFactory nullPointerException(調用getCurrentSession()時)
從控制檯異常的快照:
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
這裏是從partageDomainImpl類代碼部分:
@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);
}
}
注意,異常不是由EL langage造成的,其實我在調用getsessionFactory
方法之前正確地打印對象,從中發現異常
在我的PartageDomain類中我有:
@Entity
public class PartageDomain implements Serializable {
// the PartageDomain properties, getters and setters ....
其實我表明你提我使用註解來管理依賴性和注射,關於我的會話工廠,我宣佈它在我的配置文件作爲
您是否在XML中設置了 和/或 ? –
Luciano
2012-08-03 13:01:55
它們都設置正確,我已經使用它們來保存我有的其他對象: \t –
maouven
2012-08-03 13:24:06
如何將PartageDaoImpl注入UserManagedBean? UserManagedBean的 – Luciano 2012-08-03 14:08:40