custom jpa validation in spring boot
<p>I have an entity like this:</p>
<pre><code>@Entity
@Table(name = &quot;transaction_receiver&quot;)
public class TransactionReceiver implements Serializable, Addressable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @NotNull
    @Column(name = &quot;contact_id&quot;, nullable = false)
    private String contactId;

    @Column(name = &quot;datev_number&quot;)
    private String datevNumber;

    @NotNull
    @Column(name = &quot;receiver&quot;, nullable = false)
    private String receiver;

    @NotNull
    @Size(min = 22, max = 34)
    @Column(name = &quot;iban&quot;, length = 34, nullable = false)
    private String iban;

    @Size(min = 8, max = 11)
    @Column(name = &quot;bic&quot;, length = 11, nullable = false)
    private String bic;

    @NotNull
    @Column(name = &quot;institute&quot;)
    private String institute;

    @Column(name = &quot;company&quot;)
    private String company;
</code></pre>
<p>I need to write a custom validation <em>&quot;you can provide an empty iban, bic and institute, that's ok. But if any of the fields is not empty, the above constraint have to hold&quot;</em></p>
<p>I am looking for the most elegant way to accomplish this.</p>
<p>My current solution is - what I think somehow dirty, but working - to use a <code>@PrePersist</code> statement and throw exceptions from there</p>
<pre><code>    @PrePersist
    public void checkBankData() {
        boolean ibanEmpty = iban == null || iban.isEmpty();
        boolean ibanValid = !ibanEmpty &amp;&amp; iban.length() &gt;= 22 &amp;&amp; iban.length() &lt;= 34;
        boolean bicEmpty = bic == null || bic.isEmpty();
        boolean bicValid = !bicEmpty &amp;&amp; bic.length() &gt;= 8 &amp;&amp; bic.length() &lt;= 11;
        boolean instituteEmpty = institute == null || institute.isEmpty();

        boolean validState = (ibanEmpty &amp;&amp; bicEmpty &amp;&amp; instituteEmpty) || ibanValid &amp;&amp; bicValid;

        if (!validState) {
            throw new IllegalStateException(
                String.format(
                    &quot;bank data is not empty and %s%s%s%s%s&quot;,
                    !ibanValid ? &quot;iban has to be from 22 to 34 chars long&quot; : &quot;&quot;,
                    !ibanValid &amp;&amp; !bicValid ? &quot;and&quot; : &quot;&quot;,
                    !bicValid ? &quot;bic has to be from 8 to 11 chars long&quot; : &quot;&quot;,
                    !ibanValid &amp;&amp; !bicValid &amp;&amp; instituteEmpty ? &quot;and&quot; : &quot;&quot;,
                    instituteEmpty ? &quot;institue must not be empty&quot; : &quot;&quot;
                )
            );
        }
    }
</code></pre>
<p>Which isn't subject to <code>@Valid</code> annotations. A different approach would be defining a custom validator like described here: <a href="http://docs.jboss.org/hibernate/validator/4.1/reference/en-US/html/validator-customconstraints.html" rel="nofollow noreferrer">http://docs.jboss.org/hibernate/validator/4.1/reference/en-US/html/validator-customconstraints.html</a></p>
<p>But this really looks like an overkill for my constraint.</p>
<p>Isn't there any other elegant way?</p>