eclipse - gwt hibernate 程序中的异常

标签 eclipse hibernate gwt rpc

我正在尝试制作一个简单的 GWT RPC Hibernate 程序,将用户添加到 MySQL 数据库。我正在使用 Eclipse EE。该应用程序已成功将用户添加到数据库,但在编译时会引发异常。
这是我的应用程序的异常(exception)和来源。

异常(exception):

Exception in thread "UnitCacheLoader" java.lang.RuntimeException: Unable to read from byte cache
    at com.google.gwt.dev.util.DiskCache.transferFromStream(DiskCache.java:166)
    at com.google.gwt.dev.util.DiskCacheToken.readObject(DiskCacheToken.java:87)
    at sun.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at java.io.ObjectStreamClass.invokeReadObject(Unknown Source)
    at java.io.ObjectInputStream.readSerialData(Unknown Source)
    at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
    at java.io.ObjectInputStream.readObject0(Unknown Source)
    at java.io.ObjectInputStream.defaultReadFields(Unknown Source)
    at java.io.ObjectInputStream.readSerialData(Unknown Source)
    at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
    at java.io.ObjectInputStream.readObject0(Unknown Source)
    at java.io.ObjectInputStream.readObject(Unknown Source)
    at com.google.gwt.dev.javac.PersistentUnitCache.loadUnitMap(PersistentUnitCache.java:493)
    at com.google.gwt.dev.javac.PersistentUnitCache.access$000(PersistentUnitCache.java:92)
    at com.google.gwt.dev.javac.PersistentUnitCache$UnitCacheMapLoader.run(PersistentUnitCache.java:122)
Caused by: java.io.StreamCorruptedException: unexpected EOF in middle of data block
    at java.io.ObjectInputStream$BlockDataInputStream.refill(Unknown Source)
    at java.io.ObjectInputStream$BlockDataInputStream.read(Unknown Source)
    at java.io.ObjectInputStream.read(Unknown Source)
    at java.io.InputStream.read(Unknown Source)
    at com.google.gwt.dev.util.DiskCache.transferFromStream(DiskCache.java:154)
    ... 16 more

入口点类:
package rpctest.client;

import rpctest.shared.FieldVerifier;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class Rpctest implements EntryPoint {

    final TextBox firstName = new TextBox();
    final TextBox lastName = new TextBox();
    final Button ans = new Button("Add User");
    final Label label1 = new Label("First Name");
    final Label label2 = new Label("Last Name");
    //final Label errorLabel = new Label();
    private VerticalPanel mainpanel = new VerticalPanel();
    private HorizontalPanel addpanel1 = new HorizontalPanel();
    private HorizontalPanel addpanel2 = new HorizontalPanel();
    private final RpctestServiceAsync calNumbers = GWT
            .create(RpctestService.class);

    /**
     * This is the entry point method.
     */
    public void onModuleLoad() {

        addpanel1.add(label1);
        addpanel1.add(firstName);
        addpanel2.add(label2);
        addpanel2.add(lastName);
        mainpanel.add(addpanel1);
        mainpanel.add(addpanel2);
        mainpanel.add(ans);

        ans.addClickHandler(new ClickHandler() {

            @Override
            public void onClick(ClickEvent event) {

            String name1 = firstName.getValue();    
            String name2 = lastName.getValue();

            calNumbers.addUser(name1,name2,
                new AsyncCallback<String>() {
                public void onFailure(Throwable caught) {
                    // Show the RPC error message to the user
                        Window.alert("check your inputs");
                    }

                @Override
                public void onSuccess(String result) {
                // TODO Auto-generated method stub
                    Window.alert("User is ->"+result);
                }
            });}
        });
        // We can add style names to widgets
        //sendButton.addStyleName("sendButton");

        // Add the nameField and sendButton to the RootPanel
        // Use RootPanel.get() to get the entire body element

        /*RootPanel.get("nameFieldContainer").add(nameField);
         * 
        RootPanel.get("sendButtonContainer").add(sendButton);
        RootPanel.get("errorLabelContainer").add(errorLabel);*/
        RootPanel.get().add(mainpanel);

    }
}

接口(interface):
import com.google.gwt.user.client.rpc.RemoteService;
import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;

@RemoteServiceRelativePath("testService")
public interface RpctestService extends RemoteService {

    String addUser(String firstName,String lastName) throws IllegalArgumentException;
}


package rpctest.client;

import com.google.gwt.user.client.rpc.AsyncCallback;

public interface RpctestServiceAsync {

    void addUser(String firstName, String lastName,
            AsyncCallback<String> callback);

}

服务实现类:
package rpctest.server;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import org.hibernate.Session;
import org.hibernate.Transaction;
import hibDomain.User;
import rpctest.client.RpctestService;

public class RpctestServiceImpl extends RemoteServiceServlet  implements RpctestService {

        public String addUser(String name1, String name2)
            throws IllegalArgumentException {

              Transaction trns = null;
              Session session = HibernateUtil.getSessionFactory().openSession();
              try {
               trns = session.beginTransaction();

               User user = new User();

               user.setFirstName(name1);
               user.setLastName(name2);

               session.save(user);

               session.getTransaction().commit();
              } catch (RuntimeException e) {
               if(trns != null){
                trns.rollback();
               }
               e.printStackTrace();
              } finally{
               session.flush();
               session.close();
              }

            return name1; 
    }

}

pojo类:
package hibDomain;

public class User {
 private Integer id;
 private String firstName;
 private String lastName;

 public Integer getId() {
  return id;
 }
 public void setId(Integer id) {
  this.id = id;
 }
 public String getFirstName() {
  return firstName;
 }
 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }
 public String getLastName() {
  return lastName;
 }
 public void setLastName(String lastName) {
  this.lastName = lastName;
 }
}

映射文件:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping>
 <class name="hibDomain.User" table="users" >
  <id name="id" type="int" column="id" >
   <generator class="native"/>
  </id>

  <property name="firstName">
   <column name="first_name" />
  </property>
  <property name="lastName">
   <column name="last_name"/>
  </property>
 </class>
</hibernate-mapping>

cfg 文件:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
 "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
 <session-factory>
  <!-- Database connection settings -->
  <property name="connection.driver_class">com.mysql.jdbc.Driver</property>
  <property name="connection.url">jdbc:mysql://localhost/userdata</property>
  <property name="connection.username">root</property>
  <property name="connection.password"></property>

  <!-- JDBC connection pool (use the built-in) -->
  <property name="connection.pool_size">1</property>

  <!-- SQL dialect -->
  <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

  <!-- Enable Hibernate's automatic session context management -->
  <property name="current_session_context_class">thread</property>

  <!-- Disable the second-level cache -->
  <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

  <!-- Echo all executed SQL to stdout -->
  <property name="show_sql">true</property>

  <!-- Drop and re-create the database schema on startup -->
  <property name="hbm2ddl.auto">update</property>

  <!-- Mapping files -->
  <mapping resource="user.hbm.xml"/>


 </session-factory>
</hibernate-configuration>

实用类:
package rpctest.server;

import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;

public class HibernateUtil {
 private static final SessionFactory sessionFactory = buildSessionFactory();
 private static SessionFactory buildSessionFactory() {
  try {
   // Create the SessionFactory from hibernate.cfg.xml
   return new Configuration().configure().buildSessionFactory();
  }
  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() {
  return sessionFactory;
 }
}

最佳答案

这不太可能与磁盘空间不足有关。

您更有可能是作为不同的用户构建的,或者由于某些其他原因存在导致问题的现有、无效的临时文件。

在 GWT 目录中查找目录“gwt-unitCache”和“reports”。删除它们。然后运行ant clean、ant test;那应该可以解决问题了~

几乎可以肯定它“突然开始工作”的原因是你做了一个新的 git clone 或清除目录,删除这些文件。 :)

关于eclipse - gwt hibernate 程序中的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8282724/

相关文章:

java - 带过滤功能的 jetty

java - 编译时如何告诉eclipse添加导出

php: 自动缩进整个代码?

stored-procedures - 如何在 Hibernate 中创建临时表?

java - 无法加载请求的类 org.hsqldb.jdbcDriver

gwt - 流口水 Maven 依赖与 GWT 冲突

java - 在 SmartGWT 中创建具有小部件的表单

java - Windows 8 上的 lockAndInitHandle 错误

java - 映射一组枚举 - Hibernate

gwt - GWT CellTable 中的键盘导航