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 8291113
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T13:01:00+00:00 2026-06-08T13:01:00+00:00

Say you have an instance of your DB entity class MyClass. You have 3

  • 0

Say you have an instance of your DB entity class MyClass.
You have 3 fields in this entity which are all tagged as “Cannot be null”
If you create an object of this class and pass it along to be persisted you DB layer will throw an exception when it tries to persist the object if it does NOT have the 3 fields populated.
What is the best practice in this situation.
Should the constructor, setter methods in MyClass validate that all fields are correct and if not complain.
Or do you allow an invalid object to be created and have the DB layer do the complaining?

  • 1 1 Answer
  • 0 Views
  • 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-06-08T13:01:01+00:00Added an answer on June 8, 2026 at 1:01 pm

    1) You should prevent the client from submitting an invalid data

    2) Even if you do 1, you still need to validate on the server — people will try wacky things with curl and wget…

    3) I would check in the service layer that the incoming data is valid. If it is not, I would return some sort of error code to the client, and not even do the save. For simple cases I would have a validate method on my domain class. For complicated cases, I would create a service method to do the validation, for example, in the case where I need to check the submitted data against data in the DB. There is no 1 right way to do validation that covers all cases.

    The problem with doing the save and catching the exception is it can be difficult to distinguish this error from other errors that can occur. Plus, it’s easy to test validation code, and that the service does the right thing on validation failure. Plus, validation logic is business rules, and it’s always good to be able to easily test your business rules.

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

Sidebar

Related Questions

Let's say we have a class foo which has a private instance variable bar
Let's say I have a class like this : public class MyClass { public
Say, for instance, you have a popup which opens from your main page. The
Let's say I have a MyObject instance which is not initialized: var a:MyObject =
Let's say I have a class Foo with some primitive instance variables. I initialize
Let's say for an instance I have this string: var a=23434,bc=3434,erd=5656,ddfeto='dsf3df34dff3',eof='sdfwerwer34',wer=4554; How should I
Say, for instance, I have a class: public class MyFoo : IMyBar { ...
Let's say you have a server-side entity called MyEntity and you also have all
Say I have an instance variable MyObject that has been allocated and initialized. Then
say I have two 'modules'. For instance the hardware-interface layer of a RS-232 port

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.