我尝试使用Hibernate写入数据库时遇到了一些错误。
我遵循本指南,但我想改用MySQL:http://www.gwtproject.org/articles/using_gwt_with_hibernate.html
我有一个带有ID和名称的用户类(以及hibernate映射文件),UserService,UserServceAsync,UserServiceImpl和HibernateUtil类。
当我尝试将用户添加到数据库时,我得到以下内容:
mar 11, 2014 2:38:47 EM com.google.appengine.tools.development.ApiProxyLocalImpl log
Allvarlig: javax.servlet.ServletContext log: Exception while dispatching incoming RPC call
com.google.gwt.user.server.rpc.UnexpectedException: Service method 'public abstract java.lang.Long com.coma.comaexjobb.client.UserService.saveUser(com.coma.comaexjobb.shared.Users)' threw an unexpected exception: java.lang.NoClassDefFoundError: org/hibernate/service/ServiceRegistry
at com.google.gwt.user.server.rpc.RPC.encodeResponseForFailure(RPC.java:389)
at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:579)
at com.google.gwt.user.server.rpc.RemoteServiceServlet.processCall(RemoteServiceServlet.java:265)
at com.google.gwt.user.server.rpc.RemoteServiceServlet.processPost(RemoteServiceServlet.java:305)
at com.google.gwt.user.server.rpc.AbstractRemoteServiceServlet.doPost(AbstractRemoteServiceServlet.java:62)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166)
at com.google.appengine.api.socket.dev.DevSocketFilter.doFilter(DevSocketFilter.java:74)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.tools.development.ResponseRewriterFilter.doFilter(ResponseRewriterFilter.java:123)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.tools.development.HeaderVerificationFilter.doFilter(HeaderVerificationFilter.java:34)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.api.blobstore.dev.ServeBlobFilter.doFilter(ServeBlobFilter.java:63)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.apphosting.utils.servlet.TransactionCleanupFilter.doFilter(TransactionCleanupFilter.java:43)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.tools.development.StaticFileFilter.doFilter(StaticFileFilter.java:125)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at com.google.appengine.tools.development.DevAppServerModulesFilter.doDirectRequest(DevAppServerModulesFilter.java:366)
at com.google.appengine.tools.development.DevAppServerModulesFilter.doDirectModuleRequest(DevAppServerModulesFilter.java:349)
at com.google.appengine.tools.development.DevAppServerModulesFilter.doFilter(DevAppServerModulesFilter.java:116)
at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
at com.google.appengine.tools.development.DevAppEngineWebAppContext.handle(DevAppEngineWebAppContext.java:97)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at com.google.appengine.tools.development.JettyContainerService$ApiProxyHandler.handle(JettyContainerService.java:487)
at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
at org.mortbay.jetty.Server.handle(Server.java:326)
at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
at org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:938)
at org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:755)
at org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:218)
at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
at org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:409)
at org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)
Caused by: java.lang.NoClassDefFoundError: org/hibernate/service/ServiceRegistry
at com.coma.comaexjobb.server.UserServiceImpl.saveUser(UserServiceImpl.java:26)
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.google.appengine.tools.development.agent.runtime.Runtime.invoke(Runtime.java:115)
at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:561)
... 40 more
Caused by: java.lang.ClassNotFoundException: org.hibernate.service.ServiceRegistry
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at com.google.appengine.tools.development.IsolatedAppClassLoader.loadClass(IsolatedAppClassLoader.java:216)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 47 more
Anynone知道我做错了什么? (如果有帮助的话,我可以在这里发布课程)非常感谢!
更新2014-03-13
MainClass
public void onModuleLoad() {
final UserServiceAsync userAsync = (UserServiceAsync) GWT.create(UserService.class);
final Button addRecord = new Button("Add Record");
RootPanel.get("topDiv").add(addRecord);
// Setup click handler to add selected record to selected account.
addRecord.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
Long test= 123456L;
Users user = new Users(test);
// Persist the record to the account.
userAsync.saveUser(user,
new AsyncCallback<Long>() {
@Override
public void onFailure(Throwable caught) {
//Window.alert("Failed to save records to account.");
System.out.println("FAILURE");
}
@Override
public void onSuccess(Long result) {
// TODO Auto-generated method stub
}
});
UserService.java
package com.coma.comaexjobb.client;
import java.util.List;
import com.coma.comaexjobb.shared.Users;
import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;
@RemoteServiceRelativePath("hibbeServlet")
public interface UserService extends RemoteService {
public List<Users> getUsers();
public Long saveUser(Users user);
}
UserServiceAsync.java
package com.coma.comaexjobb.client;
import java.util.List;
import com.coma.comaexjobb.shared.Users;
import com.google.gwt.user.client.rpc.AsyncCallback;
public interface UserServiceAsync {
public void getUsers(AsyncCallback<List<Users>> callback);
public void saveUser(Users user, AsyncCallback<Long> callback);
}
UserServiceImpl.java
package com.coma.comaexjobb.server;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Session;
import com.coma.comaexjobb.Util.HibernateUtil;
import com.coma.comaexjobb.client.UserService;
import com.coma.comaexjobb.shared.Users;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.sun.java.swing.plaf.windows.resources.windows;
public class UserServiceImpl extends RemoteServiceServlet implements
UserService {
@Override
public List<Users> getUsers() {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
List<Users> users = new ArrayList<Users>(session.createQuery("from Users").list());
session.getTransaction().commit();
return users;
}
@Override
public Long saveUser(Users user) {
Session session = HibernateUtil.getSessionFactory().getCurrentSession();
session.beginTransaction();
session.save(user);
session.getTransaction().commit();
return user.getId();
}
}
Users.java
package com.coma.comaexjobb.shared;
public class Users implements java.io.Serializable {
private Long UsersID;
private String name;
public Users() {
}
public Users(Long id) {
this.UsersID = id;
}
public Long getId() {
return this.UsersID;
}
public void setId(Long id) {
this.UsersID = id;
}
}
HibernateUtil.java
package com.coma.comaexjobb.Util;
import org.hibernate.SessionFactory;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;
import org.hibernate.service.ServiceRegistry;
public class HibernateUtil {
private static SessionFactory sessionFactory;
private static ServiceRegistry serviceRegistry;
public static SessionFactory getSessionFactory() {
Configuration configuration = new Configuration();
configuration.configure();
serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
configuration.getProperties()).build();
sessionFactory = configuration.buildSessionFactory(serviceRegistry);
return sessionFactory;
}
}
的web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee">
<!-- Servlets -->
<servlet>
<servlet-name>hibbeServlet</servlet-name>
<servlet-class>com.coma.comaexjobb.server.UserServiceImpl</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hibbeServlet</servlet-name>
<url-pattern>/comaexjobb/hibbeServlet</url-pattern>
</servlet-mapping>
<!-- Default page to serve -->
<welcome-file-list>
<welcome-file>ComaExjobb.html</welcome-file>
</welcome-file-list>
</web-app>
答案 0 :(得分:0)
好吧,正如一些评论者已经说过的那样,似乎GWT的类加载器已经决定它找不到org.hibernate.service.ServiceRegistry的类。
鉴于您正在从Eclipse中启动GWT,您可以查看项目的引用库,它们看起来像:
或者更好的是,你用来从日食中启动GWT的启动器的类路径,如:
您是否看到与Hibernate对应的JAR包含所需的类?