在我的jax-rs上使用hibernate我收到此错误:
HTTP ERROR 500
访问/ services / datesTest时出现问题。原因是:
Server Error Caused by:
java.lang.ExceptionInInitializerError at connectors.Connector.buildSessionFactory(Connector.java:32)at connectors.Connector.getSessionFactory(Connector.java:37)at connectors.Connector.getSession(Connector.java:45)at mainproject.services.HibernateDatesTest.verifyToken(HibernateDatesTest.java:24) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在java.lang.reflect.Method.invoke(Method.java:606)at com.sun.jersey.spi.container.JavaMethodInvokerFactory $ 1.invoke(JavaMethodInvokerFactory.java:60) 在 com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider $ TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:185) 在 com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75) 在 com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:288) 在 com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108) 在 com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147) 在 com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84) 在 com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1469) 在 com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1400) 在 com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349) 在 com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339) 在 com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416) 在 com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537) 在 com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:699) 在javax.servlet.http.HttpServlet.service(HttpServlet.java:820)at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:565) 在 org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:479) 在 org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:119) 在 org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:521) 在 org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:227) 在 org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1031) 在 org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:406) 在 org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:186) 在 org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:965) 在 org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:117) 在 org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:111) 在org.eclipse.jetty.server.Server.handle(Server.java:349)处 org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:449) 在 org.eclipse.jetty.server.AbstractHttpConnection $ RequestHandler.headerComplete(AbstractHttpConnection.java:910) 在org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:634) 在 org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:230) 在 org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:76) 在 org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:609) 在 org.eclipse.jetty.io.nio.SelectChannelEndPoint $ 1.run(SelectChannelEndPoint.java:45) 在 org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:599) 在 org.eclipse.jetty.util.thread.QueuedThreadPool $ 3.run(QueuedThreadPool.java:534) 在java.lang.Thread.run(Thread.java:744)引起: java.lang.NoSuchMethodError: org.hibernate.integrator.internal.IntegratorServiceImpl(Ljava / UTIL / LinkedHashSet; Lorg /休眠/引导/注册表/类装入/ SPI / ClassLoaderService):V 在 org.hibernate.boot.registry.BootstrapServiceRegistryBuilder.build(BootstrapServiceRegistryBuilder.java:247) 在 org.hibernate.boot.registry.StandardServiceRegistryBuilder。(StandardServiceRegistryBuilder.java:73) 在connectors.Connector.buildSessionFactory(Connector.java:21)... 44更多引起:
java.lang.NoSuchMethodError: org.hibernate.integrator.internal.IntegratorServiceImpl(Ljava / UTIL / LinkedHashSet; Lorg /休眠/引导/注册表/类装入/ SPI / ClassLoaderService):V 在 org.hibernate.boot.registry.BootstrapServiceRegistryBuilder.build(BootstrapServiceRegistryBuilder.java:247) 在 org.hibernate.boot.registry.StandardServiceRegistryBuilder。(StandardServiceRegistryBuilder.java:73) 在connectors.Connector.buildSessionFactory(Connector.java:21)at connectors.Connector.getSessionFactory(Connector.java:37)at connectors.Connector.getSession(Connector.java:45)at mainproject.services.HibernateDatesTest.verifyToken(HibernateDatesTest.java:24) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在java.lang.reflect.Method.invoke(Method.java:606)at com.sun.jersey.spi.container.JavaMethodInvokerFactory $ 1.invoke(JavaMethodInvokerFactory.java:60) 在 com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider $ TypeOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:185) 在 com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75) 在 com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:288) 在 com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108) 在 com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147) 在 com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84) 在 com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1469) 在 com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1400) 在 com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349) 在 com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339) 在 com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416) 在 com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537) 在 com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:699) 在javax.servlet.http.HttpServlet.service(HttpServlet.java:820)at org.eclipse.jetty.servlet.ServletHolder.handle(ServletHolder.java:565) 在 org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:479) 在 org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:119) 在 org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:521) 在 org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:227) 在 org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1031) 在 org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:406) 在 org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:186) 在 org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:965) 在 org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:117) 在 org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:111) 在org.eclipse.jetty.server.Server.handle(Server.java:349)处 org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:449) 在 org.eclipse.jetty.server.AbstractHttpConnection $ RequestHandler.headerComplete(AbstractHttpConnection.java:910) 在org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:634) 在 org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:230) 在 org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:76) 在 org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:609) 在 org.eclipse.jetty.io.nio.SelectChannelEndPoint $ 1.run(SelectChannelEndPoint.java:45) 在 org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:599) 在 org.eclipse.jetty.util.thread.QueuedThreadPool $ 3.run(QueuedThreadPool.java:534) at java.lang.Thread.run(Thread.java:744)Powered by Jetty://
我的会话构建器类:
package connectors;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
public class Connector {
private static SessionFactory sessionFactory;
private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
Configuration configuration = new Configuration();
configuration.configure("hibernate.cfg.xml");
System.out.println("Hibernate Configuration loaded");
//apply configuration property settings to StandardServiceRegistryBuilder
StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
System.out.println("Hibernate serviceRegistry created");
SessionFactory sessionFactory = configuration.buildSessionFactory(ssrb.build());
return sessionFactory;
}
catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
if(sessionFactory == null) sessionFactory = buildSessionFactory();
return sessionFactory;
}
public static Session getSession(){
return getSessionFactory().openSession();
}
}
我的Pom.xml:
http://maven.apache.org/xsd/maven-4.0.0.xsd"> resueue-WS 服务器端 1.0 4.0.0
<artifactId>resqueue-mainproj</artifactId> <dependencies> <!-- our projects --> <dependency> <groupId>serverside</groupId> <artifactId>resqueue-dal</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>serverside</groupId> <artifactId>resqueue-srdlib</artifactId> <version>1.0</version> </dependency> <!-- Jetty --> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-servlet</artifactId> <version>7.6.0.v20120127</version> </dependency> <dependency> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-webapp</artifactId> <version>7.6.0.v20120127</version> </dependency> <!-- Nimbus --> <dependency> <groupId>com.nimbusds</groupId> <artifactId>nimbus-jose-jwt</artifactId> <version>2.9</version> </dependency> <!-- Jersey --> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-server</artifactId> <version>1.8</version> </dependency> <dependency> <groupId>com.sun.jersey</groupId> <artifactId>jersey-json</artifactId> <version>1.8</version> </dependency> <!-- jUnit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <scope>test</scope> </dependency> <!--org.apache.http --> <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.3.2</version> </dependency> <!-- JSON LIB -> added for testing only --> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.2.4</version> </dependency> <!-- Hibernate --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.30</version> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> <version>4.3.6.Final</version> </dependency> <!-- for JPA, use hibernate-entitymanager instead of hibernate-core --> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-entitymanager</artifactId> <version>4.3.6.Final</version> </dependency>
我清理了所有其他版本的maven repo。 有什么想法吗?
答案 0 :(得分:5)
确保已从类路径中删除了所有以前版本的Hibernate。
在github上查看它,我不明白为什么会出现这种情况,除非您遇到版本冲突导致的类加载器问题,因为{/ 1}}存在于您正在/正在使用的两个版本中,这表明给我一个类加载器问题
答案 1 :(得分:1)
您可以尝试使用以下代码
你确定你的类路径中没有多个版本的hibernate jar。
public class Connector {
private static SessionFactory sessionFactory;
private static SessionFactory buildSessionFactory() {
try {
// Create the SessionFactory from hibernate.cfg.xml
Configuration configuration = new Configuration();
configuration.configure("hibernate.cfg.xml");
System.out.println("Hibernate Configuration loaded");
//apply configuration property settings to StandardServiceRegistryBuilder
StandardServiceRegistryBuilder ssrb = new StandardServiceRegistryBuilder().applySettings(configuration.getProperties());
System.out.println("Hibernate serviceRegistry created");
sessionFactory = configuration.buildSessionFactory(ssrb.build());
return sessionFactory;
}
catch (Throwable ex) {
// Make sure you log the exception, as it might be swallowed
System.err.println("Initial SessionFactory creation failed." + ex);
throw new ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory() {
if(sessionFactory == null) {
sessionFactory = buildSessionFactory();
}
return sessionFactory;
}
public static Session getSession(){
return getSessionFactory().openSession();
}
}
答案 2 :(得分:0)
试试这个核心;
如果你有apache tomcat,你需要在apache / lib中添加ojbc6
<!-- ORACLE JDBC driver, need install yourself -->
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc6</artifactId>
<version>11.2.0</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-core</artifactId>
<version>3.6.3.Final</version>
</dependency>
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.12.1.GA</version>
</dependency>