Skip to main content

Working with Objects

  • Chapter
  • First Online:
Hibernate Recipes

Abstract

This chapter discusses the states an object goes through during its life cycle. The session is responsible for managing object state in Hibernate, and the EntityManager does the same in Java Persistence. Hibernate provides a persistence mechanism that is transparent to the entities and the application; the application has to know the state of an object.

This is a preview of subscription content, log in via an institution to check access.

Access this chapter

Chapter
USD 29.95
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
eBook
USD 29.99
Price excludes VAT (USA)
  • Available as EPUB and PDF
  • Read on any device
  • Instant download
  • Own it forever
Softcover Book
USD 16.99
Price excludes VAT (USA)
  • Compact, lightweight edition
  • Dispatched in 3 to 5 business days
  • Free shipping worldwide - see info

Tax calculation will be finalised at checkout

Purchases are for personal use only

Institutional subscriptions

Notes

  1. 1.

    The removed reference is still present; you can still work with it in the JVM. It is just seen as being in a removed state with respect to the database, and modifications to the instance may not have the effect you intend.

  2. 2.

    Important note: changes to persistent objects (i.e., managed by the Session, as described in this chapter) will be persisted by the Transaction when it is committed. Rolling back the Transaction avoids any changes to the database.

  3. 3.

    An alternative is using Session.persist(Object), which modifies the instance much as save() does, but doesn’t return the primary key, acting just like the JPA persist() method does.

  4. 4.

    You knew this already, because you already read the previous footnote.

  5. 5.

    You can also use Session.byId() to accomplish the same kind of operation, as some of our previous recipes have done (e.g., Chapter 7, Recipe 3, is only one example). They work effectively the same way as get() and load() do, and, in fact, end up with getReference() and load() method calls in the end. They just offer you a way to specify lock options as part of the instance retrieval process.

  6. 6.

    In a perfect world, every bookshop has a lot of children and teenagers as users.

  7. 7.

    Book4 was chosen for consistency’s sake.

  8. 8.

    Yes, SQL is a general-purpose query language, but it tends to be specific for each database, whereas HQL and JPAQL try to isolate you from the database-specific SQL extensions.

  9. 9.

    Ottinger, Linwood, Minter (2014). Beginning Hibernate, 3rd Ed. New York: Apress. See http://www.apress.com/9781430265177 for more details.

Author information

Authors and Affiliations

Authors

Rights and permissions

Reprints and permissions

Copyright information

© 2015 Joseph Ottinger

About this chapter

Cite this chapter

Ottinger, J., Guruzu, S., Mak, G. (2015). Working with Objects. In: Hibernate Recipes. Apress, Berkeley, CA. https://doi.org/10.1007/978-1-4842-0127-5_10

Download citation

Publish with us

Policies and ethics