简体   繁体   English

防止JPA实体被保留

[英]Prevent JPA Entity from being persisted

I am trying to prevent an entity from being persisted without throwing an exception. 我试图防止一个实体被持久化而不会引发异常。 Lets say that we have a table holding address rows called Address. 可以说,我们有一个表,其中包含称为Address的地址行。 In the Address Entity I have the following method: 在地址实体中,我具有以下方法:

@PrePersist
@PreUpdate
private void prePersist() {

    if (areAllFieldsInTheEntityEmpty()) {
        throw new PersistenceException("You can not save an empty address!");
    }
}

Is there a way to prevent the address from being saved(so no row containing only nulls/empty string are inserted) without raising an exception? 有没有一种方法可以防止在不引发异常的情况下保存地址(因此,不会插入仅包含空值/空字符串的行)?

The difference with Is there a way to prevent null values from being persisted while allowing others through? 与的区别是否存在一种在允许其他值通过的同时防止持久保留null值的方法?

My table can store rows with all the columns except the PK being nulls and that is by design because it allows creation of order then attaching an empty shipping/billing address to it to be filled later. 我的表可以存储行,并且除PK以外的所有列都为空,这是设计使然,因为它允许创建订单,然后向其附加一个空的送货/开票地址,以便稍后填充。

However I am creating a new end point that accepts order details(to be able to create order) which I validate. 但是,我正在创建一个新的端点,该端点接受我验证的订单详细信息(以便能够创建订单)。 The shipping/billing address for such an order can be empty and such an order can be created, but this time the requirement is not to create empty shipping/billing addresses and I want to reuse the current code(which does not assumes that shipping/billing addresses can be empty) and only prevent the address from being saved when all its fields are empty. 此类订单的送货/账单地址可以为空,可以创建此类订单,但是这次的要求不是创建空的送货/账单地址,而是我想重用当前代码(这并不假定送货/帐单地址可以为空),并且仅当其所有字段为空时才阻止保存该地址。

Let me try to explain my problem with another approach. 让我尝试用另一种方法来解释我的问题。 If all my Address entities can be created ONLY by calling AddressFacade.createOrUpdate(address) I will be fine putting the null/empty checks as part of that method. 如果只能通过调用AddressFacade.createOrUpdate(address)创建我的所有Address实体,则可以将null / empty检查作为该方法的一部分。 However if cascading operation creates/updates "empty" Address row I have no way to prevent the action without setting the null constrains in the DB. 但是,如果级联操作创建/更新“空”地址行,那么在DB中未设置空约束的情况下,我将无法阻止该操作。

What I understand from your problem statement is that you want to prevent error at runtime for not-null constraints and other foreign key constraints when essential columns of a given row you are inserting are null. 从问题陈述中我了解到的是,当要插入的给定行的基本列为not-null constraints时,您希望在运行时not-null constraints和其他foreign key constraints错误。 Then simple solution I think is : Apply null/empty check on all those fields before entityManager.persist call which can cause error if you insert them null/empty in DB. 然后,我认为一种简单的解决方案是: entityManager.persist调用之前对所有这些字段应用null / empty检查,如果在数据库中插入null / empty,则可能导致错误。 This will prevent error and row won't be inserted in the table. 这样可以防止错误,并且不会在表中插入行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM