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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:23:53+00:00 2026-05-23T19:23:53+00:00

I’m starting a new project, trying to do things right this time(so more than

  • 0

I’m starting a new project, trying to do things right this time(so more than one question), I might need some help, I’m not sure what I’m doing wrong :

  1. Spring context
  2. Controller
  3. Service Interface
  4. Service Implementation
  5. DAO interface
  6. DAO implementation

I want to utilize spring MVC as much as possible, how do I make session opening/closing handled by @Transactional?

How do I catch the exceptions(i.e. non existing record or database failed) if any. i.e My database doesn’t accept duplicate entries like this one :

com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry

How can I catch this?

And for every next request I make I get this exception :

org.hibernate.AssertionFailure: null id in com.test.spring.ws.service.impl.TestObject entry (don't flush the Session after an exception occurs)

What I’m doing wrong? Can anyone suggest some improvements in my project?

  • 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-23T19:23:54+00:00Added an answer on May 23, 2026 at 7:23 pm

    Component Scan

    First things first: you’re using @Controller, @Service, @Repository, and an @Autowired, but you don’t do anything with them. I recommend using classpath scanning. Remove the “testServiceDAO” and “testService” beans from your spring context file, and instead use:

    <context:component-scan base-package="com.test.spring.ws"/>
    

    That will find and create those beans by their annotations instead of requiring you to declare them in the XML. Add @Autowired to the testServiceDAO field in your service and to the sessionFactory field in your DAO. Remove the setters for these fields. They’re no longer needed. The component-scan tag will also do the autowiring for you. To use the context namespace, you need to add it to your root element. For example:

    <beans xmlns="http://www.springframework.org/schema/beans"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:context="http://www.springframework.org/schema/context"
         xsi:schemaLocation="
             http://www.springframework.org/schema/beans
             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
             http://www.springframework.org/schema/context
             http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    

    Transaction Management

    To use @Transactional, as Sean said, you need to add an element to your spring context file:

    <tx:annotation-driven/>
    

    Since your transaction manager bean is named “transactionManager”, it will find it automatically. You also need to add the “tx” namespace to your root element, so it should look something like:

    <beans xmlns="http://www.springframework.org/schema/beans"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:context="http://www.springframework.org/schema/context"
         xmlns:tx="http://www.springframework.org/schema/tx"
         xsi:schemaLocation="
             http://www.springframework.org/schema/beans 
             http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
             http://www.springframework.org/schema/tx 
             http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
             http://www.springframework.org/schema/context
             http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    

    For this to have any chance of working, you need to remove both session.beginTransaction() and session.close() from your DAO method. Opening your own transaction in that way is mixing programmatic and declarative transaction demarcation, and the declarative way is usually better. Also, you should never ever close a session in a DAO in a real project. That will get you into all kinds of trouble.

    Exception Handling

    Your MySQLIntegrityConstraintViolationException, being a database-specific exception, would be caught by Hibernate and wrapped in a ConstraintViolationException, which is what would come out of your DAO; however, since your DAO is a @Repository now, you can benefit from Spring’s exception translation. With this, the Hibernate exception will be caught by Spring and translated to a DataIntegrityViolationException. Database exception handling is always fun!

    Session Management

    Are you using an OpenSessionInViewFilter or OpenSessionInViewInterceptor? If so, a Hibernate session is opened when a request is first received and closed after the response is written. If not, then the session doesn’t start until a transaction begins (at an @Transactional method), and it’s closed when that transaction finishes. With the filter/interceptor, you can do things in the “view” tier that require calling back to the database–specifically when you have lazy relationships or lazy-loaded objects that you need for rendering the view. If the session isn’t available–as it isn’t if it only exists for the length of the transactional service method–you can’t do those things in the view and you’ll get the infamous LazyInitializationException.

    As for the “don’t flush the Session after an exception occurs” error you’re getting, I don’t see anything immediately that would make me think that should happen. Perhaps something in your web-tier spring context is misconfigured, or maybe there’s some weird interplay in how you’re handling the transaction and session directly in the DAO.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.

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.