2011-11-27 5 views
4

J'essaye de faire un programme simple de GWT RPC Hibernate qui ajoute un utilisateur à la base de données MySQL. J'utilise Eclipse EE. L'application ajoute avec succès l'utilisateur à la base de données mais elle déclenche une exception lors de la compilation. Voici l'exception & source de mon application.exception dans le programme gwt hibernate

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 

classe 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.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 de classe de mise en œuvre:

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

} 

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

fichier de mapping:

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

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

classe 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 { 
    // 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; 
} 
} 
+0

L'exception que vous avez collée possède une trace de pile uniquement à partir de packages Java.io ou com.google, c'est-à-dire des bibliothèques ou des sources système. Pourriez-vous ajouter la trace de la pile à partir de cette partie qui pointe vers une classe que vous avez écrite, puis collez le code source de celle-ci. Cela donnera quelques pistes –

+0

@enterprize Avez-vous l'exception même si vous ne fermez pas explicitement la session? –

+0

@Darthenius: exception générée lors de la compilation du programme. – enterprize

Répondre

0

Vous a manqué d'espace disque. Le compilateur GWT n'a pas pu trouver d'espace temporaire.

6

Ce problème est probablement dû au manque d'espace disque. Il est beaucoup plus probable que vous ayez créé un utilisateur différent ou que, pour une raison ou une autre, il existe des fichiers temporaires non valides qui causent le problème.

Regardez dans votre répertoire GWT les répertoires "gwt-unitCache" et "reports". Supprime-les. Ensuite, exécutez ant propre, test de fourmi; cela devrait résoudre le problème ~

La raison pour laquelle il a «soudainement commencé à fonctionner» était certainement que vous aviez fait quelque chose comme un clone git ou effacé le répertoire, en supprimant ces fichiers. :)

4

Supprimer ./src/main/ gwt-unitCache.