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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:12:52+00:00 2026-06-04T07:12:52+00:00

Can someone explain why the lastAccessed date does not get saved to the database

  • 0

Can someone explain why the “lastAccessed” date does not get saved to the database in this example and how I can get it to save to the DB? My understanding is that the do object is an attached object after the save() call and therefore all modifications should be persisted automatically.

Note: “myDate” is persisted correctly, so all other spring configuration seems to be correct.

@Transactional(readOnly = false)
public DateObject getOrCreateDateObject(Date myDate) {
    DateObject do = null;

    do = getCurrentDateObject();  // For my tests, this has been returning null

    if (do == null) {
        // create a new object
        do = new DateObject();
        do.setDate(myDate);
        sessionFactory.getCurrentSession().save(do);
    }

    // This does not persist to the database
    do.setLastAccessed(new Date());

    return do;
}

I have also tried some of the following combinations (and more) after the save() call. None of these work:

sessionFactory.getCurrentSession().merge(do);  // tried before and after do.setDate(d2)

sessionFactory.getCurrentSession().update(do);

sessionFactory.getCurrentSession().saveOrUpdate(do);

sessionFactory.getCurrentSession().flush();

DateObject doCopy = (DateObject)sessionFactory.getCurrentSession().load(DateObject.class, do.getId());
sessionFactory.getCurrentSession().merge(doCopy);
doCopy.setLastAccessed(new Date());

I’m hoping this is an easy answer that I’m just not seeing. Thank you for your help!

Edit #1 05/22/2012

As requested, here is the mapping for this entity, specified in src/main/resources/META-INF/dateobject.hbm.xml. I can see that the columns are created in the database using “SELECT * FROM dateObjects” in the mysql client. MY_DATE is populated correctly, but LAST_ACCESSED is set to NULL.

<class name="com.example.entity.DateObject" table="dateObjects">
    <id name="id" column="DATE_OBJECT_ID">
        <generator class="identity" />
    </id>
    <property name="date" type="date" column="MY_DATE" />
    <property name="lastAccessed" type="date" column="LAST_ACCESSED" />
</class>

Edit #2 05/24/2012

I have a working SSCCE at https://github.com/eschmidt/dateobject. The interesting thing is that the web client (calling localhost:8080/view/test) shows that lastAccessed is set correctly, but when I check the database with the MySQL client, it shows that lastAccessed is NULL. With this complete set of code, can anybody see why the database wouldn’t update even though the method is marked @Transactional?

  • 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-04T07:12:52+00:00Added an answer on June 4, 2026 at 7:12 am

    If you’re absolutely certain that after running that code, do.date is stored in the db and do.lastAccessed isn’t, then your connection and transaction are obviously set up correctly. My first guess would be incorrect mappings, since that’s the simplest solution. You don’t happen to have an @Transient on the field, the getter, or the setter for lastAccessed, do you? (Assuming, of course, that you’re using annotations to map your domain objects.)

    If you could provide an SSCCE, I’ll bet I or someone else can give you a definitive answer.

    Update: It’s hard trimming a full application down to the smallest possible code that demonstrates a problem. The upshot is that you’ll likely find the answer while you’re at it. I have lots of sample projects in github that might help guide you if you just need a few nudges in the right direction. basic-springmvc might be closest to what you’re doing, but it uses annotations instead of xml for mappings. It’s also a Spring MVC project. It’s a lot simpler to start a Spring context manually in a main class than to worry about a whole servlet container and the multiple contexts that Spring MVC wants you to have. spring-method-caching, for one, has an example of doing that.

    As for the mapping you posted, it looks fine, though it’s been a long while since I touched an XML mapping. Are you using field or property access? That could possibly have a bearing on things. Also, are there any custom listeners or interceptors in the SessionFactory that might be twiddling with your objects?

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

Sidebar

Related Questions

Can someone explain why the HashMap acts like it does in this example: Simple
Can someone explain what does this statement do? #define CONST_SIG (void (*) () )
Can someone explain why everything in WPF is blurry? Is this something that can
Can someone explain what this ajax code does? function ajaxProgress(){ //Math.random() is for bitchy
Can someone explain the logic behind this for loop... I just don't get it
Can someone explain this code please? how is it that the function bar accepts
Can someone explain how does this website accesses the contents of the clipboard and
Can someone explain clearly why this implementation (from SO 3965054) of min_of_list works in
Can someone explain why this code doesn't work? Intent i = new Intent(this.context, SomeClass.class);
Can someone explain why the console acts this way? I posted this question once,

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.