2011-07-05 14 views
1

:私のGWTビューでJPA&RequestFactory:私は私のJPAエンティティクラスの外部キーの永続

@Entity 
public class Booking { 
@Id 
@SequenceGenerator(name = "BOOKING", allocationSize = 1) 
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "BOOKING") 
private Integer id; 

@ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL) 
@JoinColumn(nullable = false) 
private User user; 

// ... 
} 

私が持っている:

MainRequestFactory.BookingRequest bookingRequest = reqFactory.bookingRequest(); 
    BookingProxy bookingProxy = bookingRequest.create(BookingProxy.class); 
    UserProxy userProxy = bookingRequest.create(UserProxy.class); 

    userProxy.setId(12); 

    bookingProxy.setUser(userProxy); 

    Request<Void> persistRequest = bookingRequest.persist().using(bookingProxy); 
    persistRequest.fire(new Receiver<Void>() { 
     @Override 
     public void onSuccess(Void response) { 
      GWT.log("persisted"); 
     } 
    }); 

//////////を///////////

UsersおよびBookingsユーザー制約のない持続性がうまく機能します。しかし、上記のコードでは、新しく作成された予約にID "12"のユーザーを割り当てたい/しなければなりません。しかし、私は次のエラーを取得する既存のユーザーID 12に新しい予約を割り当てることはできません。

[EL Warning]: 2011-07-05 18:48:45.464--UnitOfWork(267787945)--Exception [EclipseLink-4002] (Eclipse Persistence Services - 2.3.0.v20110604-r9504): org.eclipse.persistence.exceptions.DatabaseException Internal Exception: org.firebirdsql.jdbc.FBSQLException: GDS Exception. 335544665. violation of PRIMARY or UNIQUE KEY constraint "INTEG_388" on table "USERS2"

それだから「12」がすでに存在し、JPAが持つ新しいユーザーを作成したいIDを持つユーザーユーザID「12」を外部キーとして新たな予約を作成するのではなく、同じIDを使用する。

RequestFactory新しいユーザーを作成するのではなく、既存のユーザーのユーザーIDを新しい予約エントリに割り当てる方法を教えてください。

答えて

4

あなたの分析は正しいと思います。

Userを作成する代わりに、EntityManager find()またはgetReference()を使用して既存のUser項目を取得します。

+0

もう少し詳しく説明できますか?私はRequestFactoryのProxyクラスを通してのみドメインクラスと話すことができます。 – Alex

+0

私のBooking.java内の "setUser(user user){this.user = user;}"をsetUser(Integer id){UserController userController = new UserControllerImpl();に置き換えようとしました。 this.user = userController.getReference(id);}しかし、私は同じエラーがあります。 – Alex

+0

私はGWTに精通していませんが、私のドキュメントは、requestFactory.employeeRequest()、findEmployee(employeeId)、create()ではなくfind()を使用しています。必要なもの:新しいエントリを作成するのではなく、既存のエントリを見つけること。 – djna

関連する問題