2

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. 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?

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.

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. 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.

  • check this... https://stackoverflow.com/questions/2088743/is-there-a-way-to-prevent-null-values-from-being-persisted-while-allowing-others – sakura Jul 25 '17 at 07:15
  • 1
    You can check those conditions before you invoke 'save' – talex Jul 25 '17 at 07:16
  • Possible duplicate of [Is there a way to prevent null values from being persisted while allowing others through?](https://stackoverflow.com/questions/2088743/is-there-a-way-to-prevent-null-values-from-being-persisted-while-allowing-others) – talex Jul 25 '17 at 07:19
  • @talex That is exactly what I do not want to do. It means that if someone uses my entity/facade afterwards - he have to do those checks also, or said otherwise he will be responsible for not inserting null/empty rows, which is what I am trying to prevent while failing gracefully... – Антон Антонов Jul 25 '17 at 07:27
  • @АнтонАнтонов Failing gracefully is not god idea. It mean you just lose some changes in your data. Which in turn means that you data can become inconsistent. – talex Jul 25 '17 at 09:13

1 Answers1

2

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. 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. This will prevent error and row won't be inserted in the table.

Vandit Upadhyay
  • 315
  • 3
  • 8
  • No, no that is not my problem. Every column except the PK can be null in the table. I want to "enforce" that rows with all the columns being null can be stored in the DB, without making the consumers of my entity/facade do the null/empty checks themselves . – Антон Антонов Jul 25 '17 at 07:21