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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T07:16:56+00:00 2026-06-13T07:16:56+00:00

I’m trying to test my optimistic locking implementation of my application. However the result

  • 0

I’m trying to test my optimistic locking implementation of my application. However the result is not what I expect. The steps I take to test are the following

  • load an entity from the database
  • set the version attribute to one less than present in the database
  • change another attribute thats just a string to something else
  • save the entity

I expected a staleException now, however the entity just gets saved and the version increases to the next in line.

Here is a small extract of my save code

   public <T extends DomainObject> T save(T objectToSave) {
        Session currentSession = null;
        try {
            currentSession = sessionFactory.getCurrentSession();
            currentSession.save(objectToSave);
            return objectToSave;
        } catch (Exception ex) {
            logger.error("save error",ex);
        }
        return null;
    }

I load objects by id with named queries thru my entire application with following code

@SuppressWarnings("unchecked")
public <T extends Object> List<T> query(Class<T> returnClass, String query, List<String> namedParams, List<? extends Object> params, Integer limit) {
    Session currentSession = null;
    try {
        currentSession = sessionFactory.getCurrentSession();
        Query namedQuery = currentSession.getNamedQuery(query);
        if(limit != null){
            namedQuery.setMaxResults(limit);
        }
        namedQuery.setCacheable(true);
        if (namedParams != null && namedParams.size() > 0) {
            addParams(namedQuery, namedParams, (List<Object>) params);
        }
        return namedQuery.list();
    } catch (Exception ex) {
        logger.error("query error",ex);
    }
    return null;
}

And this is the configuration of my sessionfactory

<bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
    <property name="entityInterceptor" ref="auditInterceptor" />
    <property name="dataSource" ref="dataSource" />
    <property name="hibernateProperties">
        <value>
            hibernate.dialect=${hibernate.dialect}
            hibernate.show_sql=${hibernate.show_sql}
            hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
            hibernate.cache.region.factory_class=net.sf.ehcache.hibernate.EhCacheRegionFactory
            hibernate.cache.use_query_cache=true
            hibernate.cache.use_second_level_cache=true
            hibernate.cache.provider_configuration_file_resource_path=ehcache.xml
            hibernate.generate_statistics=true
            hbm2ddl.auto=${hbm2ddl.auto}
            hibernate.c3p0.min_size=${hibernate.c3p0.min_size}
            hibernate.c3p0.max_size=${hibernate.c3p0.max_size}
            hibernate.c3p0.timeout=${hibernate.c3p0.timeout}
            hibernate.c3p0.max_statements=${hibernate.c3p0.max_statements}
            hibernate.c3p0.idle_test_period=${hibernate.c3p0.idle_test_period}
        </value>
    </property>
    <property name="schemaUpdate">
        <value>true</value>
    </property>
    <property name="annotatedClasses">
        <list>
            <value>com.mbalogos.mba.domain.site.Site</value>
        </list>
    </property>
</bean>          

Is there anything I missed on the configuration of the sessionFactory or do I test it completly wrong and should I test it in a different way ?

Thanks in advance

  • 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-13T07:16:57+00:00Added an answer on June 13, 2026 at 7:16 am

    Regressing the version field is dangerous as it is Hibernate controlled (if you’re using the JPA versioning functionality). Load in an entity, detach it (maybe use session.evict(obj)), alter an attribute. Load in the same entity, keep it attached, alter it, save it. Reattach the first entity and attempt to save it (I think Merge will do this). You should then see the StaleObjectStateException exception.

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

Sidebar

Related Questions

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
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to select an H1 element which is the second-child in its group
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I need a function that will clean a strings' special characters. I do NOT

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.