2

JPAを使用して単純なDB接続を作成しようとしています。正常に動作しますが、クライアントに例外をスローしようとすると、エラーが発生します。

[ERROR] [browsereditor] - Line 210: No source code is available for type javax.persistence.EntityExistsException; did you forget to inherit a required module?

[ERROR] [browsereditor] - Line 212: No source code is available for type javax.persistence.EntityNotFoundException; did you forget to inherit a required module?

開発モードではエラーは発生せず、正常にコンパイルされますが、アプリモジュールがロードされると、エラーが発生する場所があります。

サーバー/作曲家とクライアント/プレゼンターのクラスに必要なインポートがあります

import javax.persistence.EntityExistsException;
import javax.persistence.EntityNotFoundException;

また、クラスパスとビルドパスに次のjarファイルを追加しました。

javax.persistence.jar

jpa-annotations-source.jar(http://code.google.com/p/google-web-toolkit/issues/detail?id=1830#c14)

gwt.xmlにも追加してみました

<source path='client'/>
<source path='shared'/>
<source path='server'/>

Eclipseにソースコードの場所を教える方法について何かアイデアはありますか?

ありがとう

コードは次のとおりです。

//サーバーのComposer.classからcomposerを作成します

    public static Composer createComposer(String name)
        throws EntityExistsException {
    Composer comp = new Composer();
    comp.setName(name);
    comp.setId(1);

    EntityManager entityManager = entityManager();
    entityManager.getTransaction().begin();
    entityManager.persist(comp);
    entityManager.getTransaction().commit();
    entityManager.close();

    return comp;
}

/// Presenter.classのcreateComposer(上記)からのリクエストを起動します

req.fire(new Receiver<ComposerProxy>() {

                        public void onSuccess(ComposerProxy arg0) {

                            ComposerProxy comp;
                            comp = arg0;
                        }

                        public void onFailure(Throwable caught)
                                throws Throwable {
                            // Convenient way to find out which exception
                            // was thrown.
                            try {
                                throw caught;
                            } catch (EntityExistsException e) {

                            } catch (EntityNotFoundException e) {

                            }
                        }});
                }});


[ERROR] [browsereditor] - Line 210: No source code is available for type javax.persistence.EntityExistsException; did you forget to inherit a required module?
[ERROR] [browsereditor] - Line 212: No source code is available for type javax.persistence.EntityNotFoundException; did you forget to inherit a required module?
4

2 に答える 2

0

クライアント側のGWTコードEntityExistsExceptionなどのタイプを使用することはできません。EntityNotFoundException

これらはプレーンなJavaクラスであり、GWTはそれらをJavaScriptに変換する方法を知りません。

クライアント側のコードでは、外部ライブラリのごく限られた部分しか使用できません。これらのライブラリ(たとえば、視覚化など)は、クライアント側向けに特別に設計および準備されており、アプリケーションのモジュールでGWTモジュールを継承する必要があります。

あなたが本当にやりたいことはそのようなことだと思います:

public void onFailure(ServerFailure failure) throws Throwable {
    if(failure.getExceptionType().equals("javax.persistence.EntityExistsException")){
          ...
    }else if(failure.getExceptionType().equals("javax.persistence.EntityNotFoundException")){
       ...
    }
}

サーバー側の例外のタイプはStringとして読み取ることができるため、ReceiverおよびServerFailureについてはJavadocを参照してください。

于 2012-06-02T12:09:29.507 に答える
0

助けてくれてありがとうピョートル。

これが私が最終的にやったことのコードです:

クライアントのコード

req.fire(new Receiver<ComposerProxy>() {

                        public void onSuccess(ComposerProxy arg0) {

                            ComposerProxy comp;
                            comp = arg0;
                        }

                        public void onFailure(ServerFailure failure) {

                            serverError.getServerError(failure,
                                    "onAddButtonClicked");

                        }

                    });

エラーを処理するクラスを作成しました

public class ServerError {

public ServerError() {
}

public void getServerError(ServerFailure failure, String message) {
    // Duplicate Key Error
    if (failure.getMessage().contains(
            "IntegrityConstraintViolationException")) {

        Window.alert("Duplicate Key " + message);
        return;
    }
    // Connection Error
    if (failure.getMessage().contains("NonTransientConnectionException")) {
        Window.alert("Connection error ");
        return;
    }
    // TimeOut Error
    if (failure.getMessage().contains("TimeoutException")) {
        Window.alert("Timeout Error" + message);
        return;
    }
    // Other Error
    else {
        Window.alert("Duplicate Key " + message);
        return;
    }

}
}

サーバー内のサービス

public static Composer createComposer(String name) throws Throwable {
    EntityManager entityManager = entityManager();
    Composer comp = new Composer();

    try {
        comp.setName(name);
        comp.setId(1);

        entityManager.getTransaction().begin();
        entityManager.persist(comp);
        entityManager.getTransaction().commit();

    } catch (Exception e) {

        log.error("Error in Composer::createComposer( " + name + ") //"
                + e.toString());
        throw e;
    } finally {
        entityManager.close();
    }
    return comp;
}

私が見つけた 1 つの問題は、変数 'ServerFailure failure' には、failure.message の情報しか含まれていないことです。他のすべての変数は null です。

于 2012-06-08T16:21:28.710 に答える