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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T00:00:16+00:00 2026-05-14T00:00:16+00:00

Describe please a typical lifecycle of a Hibernate object (that maps to a db

  • 0

Describe please a typical lifecycle of a Hibernate object (that maps to a db table) in a web app.
Suppose, you create a new instance of an object and persist in the db.
But during the app lifetime you’ll be working on a detached object and finally
you need to update it in the database, for example on exit.
How does it look like with hibernate and spring?

p.s. Can transactions and sessions live between servlet transitions? So that we opened 1 session and use it in all servlets without a need to reopen it?

I’ll try to give a descriptive example.
Suppose, when the app starts, the log record is created. this can be done at once,
Log log = new Log(…) and then something like save(log) — log corresponds to a table LOG.

then, as the application processes user inputs and keeps going, new data is being accumulated.

and after the second step we could add something to a log object, a collection for example:

// now we have a tracking of what user chosen: Set thisUserChoice,
// so we can update the persistent object, we have new data now !
// log.userChoices = thisUserChoice.

Here occurs the nature of my question. How are we supposed to deal with it, if we want to
update the database whenever new data is gotten from a user?

In a relational model we can work with a row id, so we could get this record and update some other data of the row.

In Hibernate we are also able to load a object by its id.

But is IT THE WAY TO GO? IS ANYTHING BETTER?

  • 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-14T00:00:17+00:00Added an answer on May 14, 2026 at 12:00 am

    You could do everything in a single session. But that’s like doing everything in a single class. It could make sense from a beginner’s point of view, but nobody does it like that in practice.

    In a web app, you can normally expect to have several threads running at once, each dealing with a different user. Each thread would typically have a separate session, and the session would only have managed instances of the objects that were actually needed by that user. It’s not that you can completely ignore concurrency in your own code, but it’s useful to have hibernate’s help. If you were to do everything with one session, you would have to do all the concurrency management yourself.

    Hibernate can also manage the concurrency if you have multiple application servers talking to a single database. The separate JVMs can’t possibly share the same session in this case…

    The lifecycle is described in the hibernate documentation (which I’m sure you’ve seen).

    Whenever a request comes from the web client to the server, the first thing you should do is load the relevant objects (see section 10.3) so that you have persistent, not detached entities to deal with. Then, you do whatever operations are required. When the session closes (ie. when the server returns the response to the client), it will write any updates to the database. Or, if your operation involves creating new entities, you’ll have to create transient ones (with new) and then call persist() or save() (see section 10.2). That will result in a managed entity — you can make more changes to it, and hibernate will record those changes when the session closes.

    I try to avoid using detached objects. But if I have to (perhaps they’re stored in the user’s session), then whenever they might need to be saved to the database, you’ll have to use update() (see section 10.6). This converts it into a managed object, and so the session will save any changes to the database when it’s closed.

    Spring makes it very easy to generate a new session for each request. You would normally tell Spring to create a sessionFactory, and then every request will be given its own session. Search for “spring hibernate tutorial” and you’ll find several examples.

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

Sidebar

Related Questions

Please note: In each step I describe below I'm logged in as the same
Can someone describe what a symbol table is within the context of C and
Is there a way to describe the module's data in a similar way that
Sometimes it's difficult to describe some of the things that us programmers may think
How would you describe and promote WCF as a technology to a non-technical client/manager/CEO/etc?
How would you describe the running time of this sort, given a function sorted
Let me describe the behavior I get: Load a user from the database: this
Is it better to describe improvements using percentages or just the differences in the
Accidentally I may forget to describe some parameters or exception throwing (or something else)
I'm not sure how to describe this problem, so I think an example is

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.