例外在gwt hibernate程序中 [英] exception in gwt hibernate program

查看:181
本文介绍了例外在gwt hibernate程序中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



例外:

 线程UnitCacheLoader中的异常java.lang.RuntimeException:无法从字节缓存中读取
在com.google.gwt.dev.util.DiskCache.transferFromStream(DiskCache.java:166)
com.google.gwt.dev.util.DiskCacheToken.readObject(DiskCacheToken.java:87)
at sun.reflect.GeneratedMethodAccessor2.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(未知源)
在java.lang.reflect.Method.invoke(未知源)
在java.io.ObjectStreamClass.invokeReadObject(未知源)
在java.io.ObjectInputStream.readSerialData(未知源)
在java.io.ObjectInputStream.readOrdinaryObject(未知源)
在java.io.ObjectInputStream.readObject0(未知源)
在java.io.ObjectInputStream.defaultReadFields(未知来源)
在java.io.ObjectInputStream.readSerialData(未知来源)
在java.io.ObjectInputStream.readOrdinaryObject(未知源)
在java.io.ObjectInputStream.readObject0(未知源)
在java.io.ObjectInputStream.readObject(未知来源)
at com .google.gwt.dev.javac.PersistentUnitCache.loadUnitMap(PersistentUnitCache.java:493)
在com.google.gwt.dev.javac.PersistentUnitCache.access $ 000(PersistentUnitCache.java:92)
at com.google.gwt.dev.javac.PersistentUnitCache $ UnitCacheMapLoader.run(PersistentUnitCache.java:122)
导致:java.io.StreamCorruptedException:数据块中间的意外EOF
在java.io $ ObjectDataInputStream $ BlockDataInputStream.refill .io.InputStream.read(未知来源)
在com.google.gwt.dev.util.DiskCache.transferFromStream(DiskCache.java:154)
... 16更多

entrypoint类:

  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.Horizo​​ntalPanel;
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;

/ **
*入口点类定义< code> onModuleLoad()< / code> ;.
* /
public class Rpctest实现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 Horizo​​ntalPanel addpanel1 = new Horizo​​ntalPanel();
private Horizo​​ntalPanel addpanel2 = new Horizo​​ntalPanel();
private final RpctestServiceAsync calNumbers = GWT
.create(RpctestService.class);

/ **
*这是入口点方法。
* /
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 catch){
//向用户显示RPC错误消息
Window.alert(检查输入);
}

@Override
public void onSuccess(String result){
// TODO自动生成的方法stub
Window.alert(User is - >+ result);
}
});}
});
//我们可以向窗口小部件添加样式名称
//sendButton.addStyleName(\"sendButton);

//将nameField和sendButton添加到RootPanel
//使用RootPanel.get()获取整个body元素

/*RootPanel.get( nameFieldContainer)添加(名称字段)。
*
RootPanel.get(sendButtonContainer)。add(sendButton);
RootPanel.get(errorLabelContainer)。add(errorLabel); * /
RootPanel.get()。add(mainpanel);

}
}

strong>

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


包rpctest.client;

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

public interface RpctestServiceAsync {

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

}

服务实施类: / p>

  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 {

事务trns = null;
会话session = HibernateUtil.getSessionFactory()。openSession();
try {
trns = session.beginTransaction();

用户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类: / strong>

 包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;
}
}

映射文件 / p>

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

< hibernate-mapping>
< class name =hibDomain.Usertable =users>
< id name =idtype =intcolumn =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.0encoding =utf-8?> 
<!DOCTYPE hibernate-configuration PUBLIC
- // Hibernate / Hibernate配置DTD 3.0 // EN
http://www.hibernate.org/dtd/hibernate-configuration -3.0.dtd>
< hibernate-configuration>
< session-factory>
<! - 数据库连接设置 - >
< 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连接池(使用内置) - >
< property name =connection.pool_size> 1< / property>

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

<! - 启用Hibernate的自动会话上下文管理 - >
< property name =current_session_context_class>线程< / property>

<! - 禁用二级缓存 - >
< property name =cache.provider_class> org.hibernate.cache.NoCacheProvider< / property>

<! - 将所有执行的SQL回显到stdout - >
< property name =show_sql> true< / property>

<! - 启动时删除并重新创建数据库模式 - >
< property name =hbm2ddl.auto> update< / property>

<! - 映射文件 - >
<映射资源=user.hbm.xml/>


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

Util类:

  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 {
//从hibernate.cfg.xml创建SessionFactory
返回新的Configuration()。configure()。buildSessionFactory();
}
catch(Throwable ex){
//确保记录异常,因为它可能被吞下
System.err.println(初始SessionFactory创建失败。 + ex);
抛出新的ExceptionInInitializerError(ex);
}
}
public static SessionFactory getSessionFactory(){
return sessionFactory;
}
}


解决方案

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



更有可能您以不同的用户身份构建,或者由于其他原因,存在现有的无效临时文件导致问题。



查看您的GWT目录中的目录gwt-unitCache和报告。删除它们。然后运行蚂蚁清洁,蚂蚁测试;这应该解决问题〜



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


I am trying to make a simple GWT RPC Hibernate program that adds a user to MySQL database. I am using Eclipse EE. The application is successfully adding user to database but it raises exception when compiled. Here is the exception & source of my application.

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

entrypoint class:

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

    }
}

interfaces:

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

}

service Implementation class:

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 class:

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

mapping file:

<?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 file:

<?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>

Util class:

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

解决方案

This unlikely to be related to running out of disk space.

Much more likely you either built as a different user or for some other reason there are existing, invalid, temporary files which are causing the problem.

Look in your GWT dir for the directories "gwt-unitCache", and "reports". Delete them. Then run ant clean, ant test; that should solve the problem~

Almost certainly the reason it 'suddenly started worked' was you did something like a fresh git clone or cleared out the directory, deleting these files. :)

这篇关于例外在gwt hibernate程序中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆