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

  • Home
  • SEARCH
  • 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 4623672
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T03:01:21+00:00 2026-05-22T03:01:21+00:00

I’m using Spring/Hibernate combination for my project, with standard CRUD operations. I wonder, is

  • 0

I’m using Spring/Hibernate combination for my project, with standard CRUD operations.

I wonder, is it reasonable to validate object existence before its deletion or update? If it is, where is the most appropriate place to do – service or dao layer?

EDIT:
Sorry, I didn’t make the point at first when I asked this question. The only motive for existence checking is to throw friendly exception to client of service (no DAO specific).

Problem is that I ‘have to do’ existence checking because my service method below is transactional, and besides that, I’m using HibernateTemplate and DaoSupport helper classes for session manipulation in DAO objects.

According to mentioned, Hibernate exception (in case of deleting non-existing instance for example) is thrown at commit time, which is out of my reach, because (I suppose) commit is executed by PlatformTransactionManager in proxy object, and I have no opportunity to handle that exception in my service method and re-throw some friendly exception to the client.

But even if I keep my strategy to check existence before deletion, bad stuff is that I have problems with NonUniqueObjectException in the case that instance exist, because I re-attach (in delete-time) already loaded instance (in read-time due existence checking).

For example:

//Existence checking in this example is not so important
public void delete(Employee emp){  
    Employee tempEmp=employeeDao.read(emp.getId());  
    if(tempEmp==null)  
        throw new SomeAppSpecificException();  
}  
//Existence checking in this example is maybe 'more logical'    
public void save(Assignment a){  
    Task t=taskDao.read(a.getTask().getId());
    if(t==null)  
        throw new FriendlyForeignKeyConstraintException(); 
    Employee e=employeeDao.read(a.getEmployee().getId());  
    if(e==null)  
        throw new EmployeeNotFoundExc();
    //...some more integrity checking and similar...  
    assignmentDao.save(a); 
}

The point is that I just want to throw friendly exception with appropriate message in the case of mentioned situations (integrity violations and similar).

  • 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-05-22T03:01:21+00:00Added an answer on May 22, 2026 at 3:01 am

    In Hibernate terms, both update and delete operations (and corresponding methods on Session) deal with persisted entities. Their existence is, therefore, implied and verifying it again in your code is kind of pointless – Hibernate will do that on its own and throw an exception if that’s not the case. You can then catch and handle (or rethrow) that exception.

    On a side note (based on your sample code), it’s not necessary to explicitly read the instance you’re going to delete. Session provides load() method that will return proxy instance suitable for passing to delete() method without actually hitting the database. It assumes that instance associated with given PK exists and will fail (throw an exception) later if that’s not the case.

    Edit (based on question clarification):

    When you say you want to throw “friendly” exception to the client, the definitions of “friendly” and “client” become important. In most real-life scenarios your transaction would span across more than a simple atomic “save()” or “delete()” method on one of your services.

    If the client is local and you don’t need separate transactions within a single client interaction (typical scenario – web app running in the same VM with service layer), it’s usually a good idea to initiate / commit transaction there (see Open Session In View, for example). You can then catch and properly handle (including wrapping / re-throwing, if needed) exceptions during commit. Other scenarios are more complicated, but ultimately the exception will be propagated to your “top level” client; it’s just that unwrapping it may prove to be complicated if you need to present the “root” cause to the client in a “friendly” way.

    The bottom line, though, is that it’s up to you. If you’d rather fail fast with your own exception (at the expense of writing some boilerplate code), there’s nothing really wrong with that.

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

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
i got an object with contents of html markup in it, for example: string

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.