Gwt JPA&RequestFactory:外键的持久性
在我的JPA实体类中,我有:Gwt JPA&RequestFactory:外键的持久性,gwt,jpa,jpa-2.0,requestfactory,Gwt,Jpa,Jpa 2.0,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 = Ca
@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;
// ...
}
在我的GWT观点中,我有:
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");
}
});
/////////////////////
没有用户约束的用户和预订持久性工作正常。但是使用上面的代码,我想/必须将ID为12的用户分配给新创建的预订。但我无法将新预订分配给已存在的用户ID 12。我得到以下错误:
[EL警告]:2011-07-05
18:48:45.464-UnitOfWork267787945-Exception
[EclipseLink-4002]日食
持久性服务-
2.3.0.v20110604-r9504:org.eclipse.persistence.exceptions.DatabaseException
内部异常:
org.firebirdsql.jdbc.FBSQLException:
GDS例外。335544665违反
主键或唯一键约束
表USERS2上的INTEG_388
这是因为ID为12的用户已经存在,JPA希望创建一个具有相同ID的新用户,而不仅仅是创建一个以用户ID为12作为外键的新预订
我如何告诉RequestFactory不要创建新用户,而只是将现有用户的用户ID分配给新的预订条目 我认为你对这个问题的分析是正确的
与其创建用户,不如使用EntityManager find或getReference检索现有用户项。我认为您对问题的分析是正确的
与其创建用户,不如使用EntityManager find或getReference来检索现有的用户项。您能再详细说明一下吗?请记住,我只能通过RequestFactory的代理类与域类对话。我已尝试将Booking.java中的setUserUser{this.user=user;}替换为setUserInteger id{UserController UserController=new UserControllerImpl;this.user=UserController.getReferenceid;}但我也有同样的错误。我不熟悉GWT,我快速阅读文档时遇到了示例requestFactory.employeeRequest.findEmployeeemployeeId,我猜是使用find而不是create,但从概念上讲,这听起来像是您需要的:查找现有条目,而不是创建新条目。是的,findUser就是解决方案!我的GWT代码:Request persistRequest=context.setUserReferenceuserId.usingbookingProxy;我的服务器端代码预订实体:this.setUserUser.FindUserId;entityController.createthis;你能再详细一点吗?请记住,我只能通过RequestFactory的代理类与域类对话。我已尝试将Booking.java中的setUserUser{this.user=user;}替换为setUserInteger id{UserController UserController=new UserControllerImpl;this.user=UserController.getReferenceid;}但我也有同样的错误。我不熟悉GWT,我快速阅读文档时遇到了示例requestFactory.employeeRequest.findEmployeeemployeeId,我猜是使用find而不是create,但从概念上讲,这听起来像是您需要的:查找现有条目,而不是创建新条目。是的,findUser就是解决方案!我的GWT代码:Request persistRequest=context.setUserReferenceuserId.usingbookingProxy;我的服务器端代码预订实体:this.setUserUser.FindUserId;entityController.createthis;