Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 963407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:39:58+00:00 2026-05-16T01:39:58+00:00

The question is where it is better (or in other words: where do you

  • 0

The question is where it is better (or in other words: where do you prefer) to put business validation logic of Jpa Entities.

Two ideas are:

  1. In the EntityListener that before save or update would validate the entity
  2. In the service that provides access to jpa persisting methods.

There are pros and cons of both.
When using approach No. 2 it is easier to test as you may just mock the jpa provider and test the validation logic. On the other hand with approach No. 1 the validation would happen at the same moment with validations like @NotNull etc.

I would love to know how do you solve validations in your projects and which is the better way to go.

Thanks.

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T01:39:59+00:00Added an answer on May 16, 2026 at 1:39 am

    Here’s a general thumb rule that I follow:

    When using bean validation, specify
    rules that do not require dependencies
    on other beans. The moment you depend
    on another bean, get your service
    layer to handle that dependency.

    In other words, if you have a reference to a bean inside another, avoid putting in that @NotNull constraint. Your service layer is best used for that, for you’re catching the violation much earlier, and at a more logical point (since other business validations would assume that the beans are available).

    As an example, consider the following entity (apologies for it wont compile)

    @Entity
    public class User
    {
       @Id
       private int id;
       @NotNull
       private String fullName;
       @NotNull
       private String email;
       private Set<Role> roles; //No bean validation constraints here.
       ...
       public boolean mapRoleToUser(Role role)
       { //Validation is done here. Including checks for a null role.
       }
    
    }
    
    @Entity
    public class Role
    {
      @Id
      private int id;
      @NotNull
      private String name;
    }
    

    The service layer in this case, is the one that should validate whether the user has a role attached or not. Verification in the pre-persist or pre-update phase is a bit too late, especially when there is a distinct service layer that has business logic, and the rest of the business logic in the domain model (sadly, I haven’t seen a good enough application with all of the logic in the domain model alone).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question maybe a better fit on The Business of Software forum but despite
Title edits that reflect a better summary of the question are welcome. I'd like
Originally posted on Server Fault , where it was suggested this question might better
QUESTION: Is it better to send large data blobs in JSON for simplicity, or
I'm discarding the old question for a better formulated question. I'm using the twitter
My question is what is better to use in generating columns in SQL. Should
For a better understanding of my question, see the following classes: public interface Invoker<R>
My question is, which one is better to use? How to properly use a
This question is related to this , hopefully better phrased. I would like to
This should be a elementary question but why is better to use something like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.