我写这个课时遇到这个错误:
public class FtHelper2 {
Session session=null;
public FtHelper2(){
System.out.println("aASDASDSAD");
this.session = HibernateUtil.getSessionFactory().getCurrentSession();
}
public String abc(){
return "abc";
}
}
如果我删除会话,它可以工作。 Session在这里有什么问题?
的HibernateUtil
public class HibernateUtil {
private static final SessionFactory sessionFactory;
static {
try {
// Create the SessionFactory from standard (hibernate.cfg.xml)
// config file.
sessionFactory = new AnnotationConfiguration().configure().buildSessionFactory();
} catch (Throwable ex) {
// Log the exception.
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
return sessionFactory;
}
}
这些是我的映射:
<hibernate-mapping>
<class name="TekirMobile.FinancialTransactions" table="FinancialTransactions" catalog="TekirMobile">
<id name="id" type="int">
<column name="ID" />
<generator class="assigned" />
</id>
<property name="date" type="string">
<column name="DATE" length="45" />
</property>
<property name="contact" type="string">
<column name="CONTACT" length="45" />
</property>
<property name="amount" type="string">
<column name="AMOUNT" length="45" />
</property>
<property name="document" type="string">
<column name="DOCUMENT" length="45" />
</property>
</class>
</hibernate-mapping>
<hibernate-mapping>
<class name="TekirMobile.Customer" table="Customer" catalog="TekirMobile">
<id name="id" type="int">
<column name="ID" />
<generator class="assigned" />
</id>
<property name="name" type="string">
<column name="NAME" length="45" />
</property>
<property name="company" type="string">
<column name="COMPANY" length="45" />
</property>
<property name="type" type="string">
<column name="TYPE" length="45" />
</property>
</class>
</hibernate-mapping>
可能是什么问题?出了什么问题? cfg文件在不同的包中。它们是默认包。
完整的堆栈跟踪如下:
Ĵ
avax.faces.el.EvaluationException: java.lang.NoClassDefFoundError: Could not initialize class TekirMobile.HibernateUtil
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:101)
at javax.faces.component.UICommand.broadcast(UICommand.java:315)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:171)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:936)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1004)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1146)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:679)
Caused by: java.lang.NoClassDefFoundError: Could not initialize class TekirMobile.HibernateUtil
at TekirMobile.FtHelper2.<init>(FtHelper2.java:18)
at TekirMobile.FtManagedBean.abc(FtManagedBean.java:66)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at org.apache.el.parser.AstValue.invoke(AstValue.java:278)
at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:274)
at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:105)
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:88)
... 24 more