写入数据库时出错

zvokhttg  于 2021-07-09  发布在  Java
关注(0)|答案(1)|浏览(334)

尝试使用hibernate写入db时出现了一些错误。
我遵循此指南,但我想改用mysql:http://www.gwtproject.org/articles/using_gwt_with_hibernate.html
我有一个id和name的用户类(和hibernateMap文件)、一个userservice、一个userserveceasync、一个userserviceinpl和一个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

没人知道我做错了什么(如果对你有帮助的话,我可以把我的课发到这里)非常感谢!
更新日期:2014-03-13
主类

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

                    }

                    });

用户服务.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();
    }
}

用户.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;
    }
    }

网站.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>
qmb5sa22

qmb5sa221#

好吧,正如一些评论者已经说过的,似乎gwt的classloader已经决定找不到org.hibernate.service.serviceregistry的类。
考虑到您是从eclipse中启动gwt,您可以查看项目的引用库,这些库类似于:

或者,更好的是,您用来从eclipse中启动gwt的启动程序的类路径,如:

您是否看到与hibernate对应的jar包含所需的类?

相关问题