JPA& RequestFactory:外键的持久化

发布于 2024-11-18 08:35:53 字数 1615 浏览 4 评论 0原文

在我的 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;

// ...
}

在我的 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");
        }
    });

/////////////////////

UsersBookings< /code> 没有用户约束的持久性工作正常。但通过上面的代码,我想要/必须将 ID 为“12”的用户分配给新创建的预订。但我无法将新的预订分配给已经存在的用户 ID 12。我收到以下错误:

[EL警告]:2011-07-05 18:48:45.464--UnitOfWork(267787945)--异常 [EclipseLink-4002](Eclipse 持久性服务 - 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 分配给新的预订条目?

In my JPA Entity class I have:

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

// ...
}

In my GWT View I have:

    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 and Bookings persistence without the User Constraint works fine. But with the above code I want/have to assign the user with the ID "12" to a newly created booking. But I cannot assign a new booking to an already existing user ID 12. I get the following error:

[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"

It's because an user with the ID "12" already exists and JPA wants to create a new user with the same ID instead of just creating a new booking with the user ID "12" as its foreign key.

How can I tell RequestFactory NOT to create a new user but instead just to assign the user ID of an already existing user to a new booking entry?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

垂暮老矣 2024-11-25 08:35:53

我认为你对问题的分析是正确的。

不要创建用户,而是使用 EntityManager find() 或 getReference() 来检索现有的用户项。

I think your analysis of the problem is correct.

Instead of creating a User, instead use an EntityManager find() or getReference() to retrieve the existing User item.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文