gwt hibernate 程序中的异常

2024-05-02

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

例外:

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);

    }
}

接口:

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 克隆或清除目录,删除这些文件。 :)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

gwt hibernate 程序中的异常 的相关文章

  • 透视切换面板在 Eclipse 中消失

    Eclipse 崩溃后 小透视切换窗格从 Eclipse 窗口的右上角消失了 我下载了最新版本并尝试打开它 使用相同的工作区 但按钮仍然消失 这是一个屏幕截图 并放大 有任何想法吗 我仍然可以通过选择 窗口 gt 打开透视图 来切换透视图
  • 如何使用 Hibernate Session.doWork(...) 进行保存点/嵌套事务?

    我正在使用 JavaEE JPA 托管事务与 Oracle DB 和 Hibernate 并且需要实现某种嵌套事务 据我所知 此类事情不受开箱即用的支持 但我应该能够为此目的使用保存点 正如建议的https stackoverflow co
  • 无法在 Eclipse 中连接到虚拟机

    想要改进这篇文章吗 提供此问题的详细答案 包括引用和解释为什么你的答案是正确的 不够详细的答案可能会被编辑或删除 当我尝试在 Eclipse 上调试任何项目时 我突然开始遇到这个奇怪的错误 我不记得有什么改变让这个问题突然出现 Launch
  • Eclipse 快捷方式查找覆盖某个方法的所有子类

    Is there an Eclipse shortcut to see all class overriding the method m Highlight select put cursor on the method name and
  • Eclipse 与 IntelliJ 热部署

    我的应用程序配置 Tomcat 8 Spring Spring MVC Hibernate 在 Eclipse 中 我创建了 Tomcat 服务器 并将我的应用程序添加到资源中 JSP JS CSS 和 JAVA 类热部署的工作原理就是这样
  • 在 Eclipse 插件中:如何以编程方式突出显示 java 编辑器中的代码行?

    我正在尝试开发一个 eclipse 插件 它对 java 代码进行一些文档检查 并在编辑器中突出显示一些代码行 为了实现我的目标 我不想在 eclipse 中创建新的编辑器 我只是想扩展默认的 java 编辑器以在不满足某些预定要求的方法下
  • 是否有用于封闭类型名称的简短版本的 Eclipse 模板变量

    我想在 Eclipse 中为 Java 类创建一个构造函数模板 我有一个适用于大多数课程的版本 尽管它不适用于嵌套在其他类中的类 见类Inner如下 如何获得类名的简短版本 模板不起作用 public newType enclosing t
  • Hibernate 序列乘以 50 生成“@Id”?

    private static final String SEQUENCE my seq Id GeneratedValue strategy GenerationType SEQUENCE generator SEQUENCE Sequen
  • 获取列名称以及 JSON 响应

    我有三个实体类 我编写了包含两个表的联接的查询 表 费用类别 Entity Table name ExpensesCategories public class ExpensesCategories Id GeneratedValue st
  • 使用 Hibernate 和 MySQL、全局和本地进行 Spring 事务管理

    我正在使用 MySQL Server 5 1 Spring 3 0 5 和 Hibernate 3 6 开发 Web 应用程序 我使用 Springs 事务管理 我是新手 所以如果我问一个容易回答的问题 请耐心等待 1 我读到了有关全局 x
  • 模拟器无法加载

    我正在使用 hello android 教程并通过 eclipse 创建 avd 启动模拟器时不使用图像 它只是显示一个黑色的后屏 中间有 ANDROID 字样 并且在 ANDROID 字样的末尾有一个闪烁的光标 我已按照 T 的步骤安装
  • Hibernate 在使用序列时生成负 id 值

    我有一个具有以下定义的类 Id SequenceGenerator name SEQ ACE WORKERS QUEUE STATS ID sequenceName SEQ ACE WORKERS QUEUE STATS ID alloca
  • Spring RESTful控制器方法改进建议

    我是 Spring REST 和 Hibernate 的新手 也就是说 我尝试组合一个企业级控制器方法 我计划将其用作未来开发的模式 您认为可以通过哪些方法来改进 我确信有很多 RequestMapping value user metho
  • 构建动态 ConstraintViolation 错误消息

    我写了一个由自定义实现的验证注释ConstraintValidator 我也想生成非常具体的ConstraintViolation使用消息插值期间验证过程中计算的值的对象 public class CustomValidator imple
  • 将 Boost 库添加到 Windows Eclipse 中的 C++ 项目

    我最近使用安装程序在 Windows 上安装了 Boost 库 我试图链接到 Eclipse 中的库 但运气不佳 我尝试浏览 Project Properties gt C C Build gt Settings gt MinGW C Li
  • Eclipse 中选定单词的括号

    几天前 我觉得这个问题很愚蠢 所以不要将其发布在这里 但即使在搜索了很多之后 我也没有找到合适的解决方案 对于那些使用过的人TextEdit 在 Mac 上 他们会完全知道我在说什么 在编码时 我只想在单词或一行上加上引号或括号 为此 我必
  • 让 Hibernate 和 SQL Server 与 VARCHAR 和 NVARCHAR 良好配合

    我目前正在大型数据库的某些表中启用 UTF 8 字符 这些表已经是 MS SQL 类型 NVARCHAR 此外 我还有几个使用 VARCHAR 的字段 Hibernate 与 JDBC 驱动程序的交互存在一个众所周知的问题 例如 参见在 h
  • org.hibernate.MappingException:实体映射中序列的增量大小设置为 [10],而 ... 大小为 [1]

    更新到 Spring Boot 2 2 和相关的 Hibernate 5 4 x 时我们遇到了问题 我们确实有以下序列生成器 Id GeneratedValue strategy GenerationType SEQUENCE genera
  • Java:在 eclipse 中导出到 .jar 文件

    我正在尝试将 Eclipse 中的程序导出到 jar 文件 在我的项目中 我添加了一些图片和 PDF s 当我导出到 jar 文件时 似乎只有main已编译并导出 我的意愿是如果可能的话将所有内容导出到 jar 文件 因为这样我想将其转换为
  • GWT - 如何组织项目以拥有多个网页以及它们之间的导航

    我是 GET 的新手 顺便说一句 它给我留下了深刻的印象 并且发现它对于像我这样熟悉 C NET 桌面技术并愿意编写 Web 应用程序的人来说非常有吸引力 我根据 GWT Eclipse 向导生成的示例启动了自己的项目 该项目生成带有面板的

随机推荐