分析hibernate 4用buildSessionFactory(ServiceRegistry serviceRegistry) 来代替buildSessionFactory()原因

版权声明:ThomasKwan https://blog.csdn.net/thomassamul/article/details/81709280

hibernate 开启事务SessionFactory  

因为Service Registry 是一个独立的组件,具有注册表(registry)和存储库功能。他的功能比较全面  用在XML模式上面比较好,区别不大。  

 

public static Session openSession(){

try{

config=new Configuration().configure();

factory=config.buildSessionFactory();

session=factory.openSession();

}catch(HibernateException e){

e.printStackTrace();

}

return session;

}

展开阅读全文

hibernate4.3在servlet中的无法找到ServiceRegistry

04-30

如题,起初配置的hibernate5,通过servlet调用hibernateUtil生成session一切正常。n后来改成了hibernate4.3.11,并修改了对应的配置文件。n结果在main方法里面调用hibernateUtil,再进行操作一切正常。但如何通过servlet访问,会马上报:**java.lang.ClassNotFoundException: org.hibernate.service.ServiceRegistry**nn这个问题百度了很久,网上也有类似的疑问,但没有一个正确解决了的,希望能够得到大神的帮助。n以下是异常和相关代码:n# 异常njava.lang.ClassNotFoundException: org.hibernate.service.ServiceRegistryn at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)n at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)n at findLose.backGroud.wsz.service.UserService.(UserService.java:32)n at findLose.backGroud.wsz.control.web.LoginControl.(LoginControl.java:34)n at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)n at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:57)n at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)n at java.lang.reflect.Constructor.newInstance(Constructor.java:526)n at java.lang.Class.newInstance(Class.java:379)n at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:116)n at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1148)n at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:864)n at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:134)n at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)n at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)n at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)n at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)n at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)n at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)n at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)n at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)n at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)n at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2466)n at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2455)n at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)n at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)n at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)n at java.lang.Thread.run(Thread.java:745)n n n# 相关代码nnn```n @WebServlet("/dologin.html")npublic class LoginControl extends HttpServlet n UserService userService = new UserService(); //出错代码,这个里面调用了hibernateUtil来获取currentSessionn …………n }n```nnnn```n public class HibernateUtil nn private static SessionFactory sessionFactory;n private static Session session;nn static n // 创建Configuration,该对象用于读取hibernate.cfg.xml,并完成初始化n Configuration config = new Configuration().configure();n StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder()n .applySettings(config.getProperties());n ServiceRegistry ssr = ssrb.build();n sessionFactory = config.buildSessionFactory(ssr);n nn /**n * 获取SessionFactoryn * n * @returnn */n public static SessionFactory getSessionFactory() n return sessionFactory;n nn public static Session getCurrentSession() n session = sessionFactory.openSession();n return session;n nn public static void closeSession(Session session) nn if (null != session) n session.close();n n nn```nn配置:nnn```n nnnnn n com.mysql.jdbc.Drivern jdbc:mysql://localhost:3306/findlosen rootn 123456n n n org.hibernate.dialect.MySQLDialectn n updaten n truen n truen n threadn n n n n n n n nn```nnnnnnnnn 问答

没有更多推荐了,返回首页