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.
Access this chapter
Tax calculation will be finalised at checkout
Purchases are for personal use only
Notes
- 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.
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.
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.
You knew this already, because you already read the previous footnote.
- 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.
In a perfect world, every bookshop has a lot of children and teenagers as users.
- 7.
Book4 was chosen for consistency’s sake.
- 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.
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
Rights 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
DOI: https://doi.org/10.1007/978-1-4842-0127-5_10
Published:
Publisher Name: Apress, Berkeley, CA
Print ISBN: 978-1-4842-0128-2
Online ISBN: 978-1-4842-0127-5
eBook Packages: Professional and Applied ComputingApress Access BooksProfessional and Applied Computing (R0)