oracle 11g和hibernate spring和jsf的集成

我正在使用jsf 2,hibernate 4.1.4,spring 3.1,oracle 11g和maven 3.但它无法连接到oracle并且有错误。 我下载了最新版本的ojdbc6。 applicationContext.xml中

                           model.Customer     org.hibernate.dialect.Oracle10gDialect true           

脸,配置

     org.springframework.web.jsf.el.SpringBeanFacesELResolver    

web.xml中

   jsfspringhiber   org.springframework.web.context.ContextLoaderListener    contextConfigLocation  /WEB-INF/applicationContext.xml     org.springframework.web.context.request.RequestContextListener    javax.faces.PROJECT_STAGE Development   default.xhtml   Faces Servlet javax.faces.webapp.FacesServlet 1   Faces Servlet *.xhtml /faces/*   State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2 javax.faces.STATE_SAVING_METHOD client   javax.servlet.jsp.jstl.fmt.localizationContext resources.application   com.sun.faces.config.ConfigureListener   

CustomerDaoImpl.java

  public class CustomerDaoImpl implements ICustomerDao{ private SessionFactory sessionFactory; public SessionFactory getSessionFactory() { return sessionFactory;} public void setSessionFactory(SessionFactory sessionFactory) { this.sessionFactory = sessionFactory; } public void addCustomer(Customer customer){ sessionFactory.openSession(); getSessionFactory().getCurrentSession().save(customer); } public void updateCustomer(Customer customer){ sessionFactory.openSession(); getSessionFactory().getCurrentSession().update(customer); } public void deleteCustomer(Customer customer){ sessionFactory.openSession(); getSessionFactory().getCurrentSession().delete(customer); } public List findAllCustomer(){ sessionFactory.openSession(); List list = getSessionFactory().getCurrentSession ().createQuery("from CUSTOMER").list(); return list; } } 

CustomerBoImpl.java

 public class CustomerBoImpl implements ICustomerBo{ ICustomerDao customerDaoImpl; public ICustomerDao getCustomerDaoImpl() { return customerDaoImpl; } public void setCustomerDaoImpl(ICustomerDao customerDaoImpl) { this.customerDaoImpl = customerDaoImpl; } public void addCustomer(Customer customer){ getCustomerDaoImpl().addCustomer(customer); } public void updateCustomer(Customer customer){ getCustomerDaoImpl().updateCustomer(customer); } public void deleteCustomer(Customer customer){ getCustomerDaoImpl().deleteCustomer(customer); } public List findAllCustomer(){ return getCustomerDaoImpl().findAllCustomer(); } } 

Customermanagedbean.java

 @ManagedBean(name="CustomerMB") @RequestScoped public class Customermanagedbean implements Serializable{ @ManagedProperty(value="#{CustomerBoImpl}") ICustomerBo customerBoImpl; List CustomerList; public int customerId; public String name; public String address; public String createdDate; public ICustomerBo getCustomerBoImpl() { return customerBoImpl; } public void setCustomerBoImpl(ICustomerBo customerBoImpl) { this.customerBoImpl = customerBoImpl; } public List getCustomerList() { return CustomerList; } public void setCustomerList(List customerList) { CustomerList = customerList; } public int getCustomerId() { return customerId; } public void setCustomerId(int customerId) { this.customerId = customerId; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getCreatedDate() { return createdDate; } public void setCreatedDate(String createdDate) { this.createdDate = createdDate; } //add a new customer data into database public String addCustomer(){ Customer cust = new Customer(); cust.setCustomerId(getCustomerId()); cust.setName(getName()); cust.setAddress(getAddress()); cust.setCreatedDate(getCreatedDate()); customerBoImpl.addCustomer(cust); clearForm(); return ""; } //clear form values private void clearForm(){ setName(""); setAddress(""); } } 

Customer.java

 @Entity @Table(name="CUSTOMER") public class Customer{ public int customerId; public String name; public String address; public String createdDate; @Id @Column(name="CUSTOMER_ID", unique = true, nullable = false) public int getCustomerId() { return customerId; } public void setCustomerId(int customerId) { this.customerId = customerId; } @Column(name="NAME") public String getName() { return name; } public void setName(String name) { this.name = name; } @Column(name="ADDRESS") public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @Column(name="CREATED_DATE") public String getCreatedDate() { return createdDate; } public void setCreatedDate(String createdDate) { this.createdDate = createdDate; } } 

default.xhtml

        

JSF 2.0 + Spring + Hibernate Example

Customer ID #{c.customerId} Name #{c.name} Address #{c.address} Created Date #{c.createdDate}

Add New Customer

Customer ID : Name : Address : created Date :

当我运行default.xhtml时:我看不到表中的记录。 在此处输入图像描述

当我点击提交按钮时,我有以下错误:

 SEVERE: org.hibernate.HibernateException: No Session found for current thread javax.faces.el.EvaluationException: org.hibernate.HibernateException: No Session found for current thread at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:98) at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:98) at javax.faces.component.UICommand.broadcast(UICommand.java:311) at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:781) at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1246) at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:77) at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97) at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:114) at javax.faces.webapp.FacesServlet.service(FacesServlet.java:308) 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:225) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927) 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:999) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:565) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:309) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source) Caused by: org.hibernate.HibernateException: No Session found for current thread at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97) at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1041) at dao.CustomerDaoImpl.addCustomer(CustomerDaoImpl.java:18) at bo.CustomerBoImpl.addCustomer(CustomerBoImpl.java:23) at managedbean.Customermanagedbean.addCustomer(Customermanagedbean.java:70) 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 org.apache.el.parser.AstValue.invoke(AstValue.java:264) at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278) at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:102) at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:84) ... 24 more Jul 02, 2012 4:40:48 PM com.sun.faces.lifecycle.InvokeApplicationPhase execute WARNING: #{CustomerMB.addCustomer()}: org.hibernate.HibernateException: No Session found for current thread javax.faces.FacesException: #{CustomerMB.addCustomer()}: org.hibernate.HibernateException: No Session found for current thread at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:114) at javax.faces.component.UICommand.broadcast(UICommand.java:311) at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:781) at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1246) at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:77) at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:97) at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:114) at javax.faces.webapp.FacesServlet.service(FacesServlet.java:308) 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:225) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169) at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98) at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927) 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:999) at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:565) at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:309) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source) Caused by: javax.faces.el.EvaluationException: org.hibernate.HibernateException: No Session found for current thread at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:98) at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:98) ... 23 more Caused by: org.hibernate.HibernateException: No Session found for current thread at org.springframework.orm.hibernate4.SpringSessionContext.currentSession(SpringSessionContext.java:97) at org.hibernate.internal.SessionFactoryImpl.getCurrentSession(SessionFactoryImpl.java:1041) at dao.CustomerDaoImpl.addCustomer(CustomerDaoImpl.java:18) at bo.CustomerBoImpl.addCustomer(CustomerBoImpl.java:23) at managedbean.Customermanagedbean.addCustomer(Customermanagedbean.java:70) 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 org.apache.el.parser.AstValue.invoke(AstValue.java:264) at org.apache.el.MethodExpressionImpl.invoke(MethodExpressionImpl.java:278) at com.sun.faces.facelets.el.TagMethodExpression.invoke(TagMethodExpression.java:102) at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:84) ... 24 more 

我在CustomerBoImpl.java中导入org.springframework.transaction.annotation.Transactional

 import java.util.List; import bo.ICustomerBo; import dao.ICustomerDao; import model.Customer; import org.springframework.transaction.annotation.Transactional; @Transactional(readOnly = true) public class CustomerBoImpl implements ICustomerBo{ ICustomerDao customerDaoImpl; public ICustomerDao getCustomerDaoImpl() { return customerDaoImpl; } public void setCustomerDaoImpl(ICustomerDao customerDaoImpl) { this.customerDaoImpl = customerDaoImpl; } @Transactional(readOnly = false) @Override public void addCustomer(Customer customer){ getCustomerDaoImpl().addCustomer(customer); } @Transactional(readOnly = false) @Override public void updateCustomer(Customer customer){ getCustomerDaoImpl().updateCustomer(customer); } @Transactional(readOnly = false) @Override public void deleteCustomer(Customer customer){ getCustomerDaoImpl().deleteCustomer(customer); } @Override public List findAllCustomer(){ return getCustomerDaoImpl().findAllCustomer(); } }