In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "how Hibernate and objects work together". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Hibernate is a complete object / relational mapping solution, which provides the function of object state management (state management), so that developers no longer need to pay attention to the details of the underlying database system.
In other words, compared with the common JDBC/SQL persistence layer scenarios that need to manage SQL statements, Hibernate uses a more natural object-oriented perspective to persist data in Java applications.
In other words, developers using Hibernate should always focus on the state of objects, regardless of the execution of SQL statements. These details are already in the hands of Hibernate and need to be understood only when developers are tuning the performance of the system.
1. Hibernate object status (object states)
Hibernate defines and supports the following object states (state):
Instantaneous (Transient)-objects created by the new operator and not yet associated with the Hibernate Session are considered instantaneous (Transient). Instantaneous (Transient) objects are not persisted to the database, nor are they given a persistence identity (identifier). If a reference to a Transient object is not maintained in the program, it will be destroyed by the garbage collector (garbage collector). You can make it into a Persistent state using Hibernate Session. (Hibernate automatically executes the necessary SQL statements)
Persistent-an instance of persistence (Persistent) has a corresponding record in the database and has a persistence identity (identifier). Instances of Persistent may have just been saved or just loaded, either way, by definition, the object remains in this state only for the associated Session lifecycle.
Hibernate detects any changes to objects in the Persistent state and synchronizes the object data (state) with the database (synchronize) when the current operation unit (unit of work) completes execution. Developers do not need to execute UPDATE manually. There is also no need to manually execute DELETE statements to change an object from a Persistent state to a Transient state.
Detached when the Session associated with a persistent (Persistent) object is turned off, the object becomes Detached. References to Detached objects are still valid, and objects can continue to be modified. If the Detached object is re-associated to a new Session, it will again become Persistent (changes in the Detached will be persisted to the database).
This feature makes possible a programming model, that is, a long-running operation unit (unit of work) that gives the user time to think (user think-time). We call it an application transaction, which is an operation unit (unit of work) from the user's point of view.
Next, let's discuss in detail the states and the transition between states (state transitions) (and the Hibernate method that triggers the state transition).
two。 Make objects persistent
Hibernate believes that the newly instantiated object of the persistence class (persistent class) is Transient. We can associate a Transient object with a session to become Persistent.
DomesticCat fritz = new DomesticCat (); fritz.setColor (Color.GINGER); fritz.setSex ('M'); fritz.setName ("Fritz"); Long generatedId = (Long) sess.save (fritz)
If the persistence identity (identifier) of the Cat is of type generated, the identifier is automatically generated and assigned to the cat when save () is called. If the persistence identity (identifier) of the Cat is of type assigned, or a compound primary key (composite key), then the identity (identifier) should be manually assigned to the cat before calling save (). You can also use persist () instead of save () according to the semantics defined in EJB3 early draft.
Additionally, you can use an overloaded version of the save () method.
DomesticCat Competition = new DomesticCat (); pk.setColor (Color.TABBY); pk.setSex ('F'); pk.setName ("Competition"); pk.setKittens (new HashSet ()); pk.addKitten (fritz); sess.save (Competition, new Long (1234))
If the objects you persist have associated objects (associated objects) (such as the kittens collection in the example above), then the order in which these objects (contention and kittens) are persisted is arbitrary (that is, you can persist kittens or contention first), unless you have NOT NULL constraints on the foreign key column. Hibernate does not violate foreign key constraints, but if you persist objects in the wrong order, you may violate NOT NULL constraints.
Usually you don't worry about these details, because you will probably use Hibernate's propagating persistence (transitive persistence) feature to automatically save those objects associated with them. In this way, even violations of NOT NULL constraints will not occur. Hibernate will take care of everything. Communicative persistence (transitive persistence) will be discussed later in this chapter.
3. Load object
If you know the persistence identity (identifier) of an instance, you can use the load () method of Session to get it. Another parameter to load () is the .class object of the specified class. This method creates a persistent instance of the specified class and loads its data (state) from the database.
Cat fritz = (Cat) sess.load (Cat.class, generatedId); / / you need to wrap primitive identifiers long pkId = 1234; DomesticCat Competition = (DomesticCat) sess.load (Cat.class, new Long (pkId))
In addition, you can load the data (state) onto the specified object instance (overwriting the original data of that instance).
Cat cat = new DomesticCat (); / / load pk's state into cat sess.load (cat, new Long (pkId)); Set kittens = cat.getKittens ()
Note that if there are no matching database records, the load () method may throw an unrecoverable unrecoverable exception. If the class mapping uses a proxy, the load () method returns an uninitialized proxy and does not access the database until you call a method of the proxy. This is useful if you want to create an association in an object that points to another object, but you don't want to load the associated object at the same time as the object is loaded from the database. If batch-size is set for the corresponding class mapping relationship, this operation allows multiple objects to be loaded in a batch (because proxies are returned, there is no need to fetch data for all objects from the database).
If you are not sure if a matching row exists, you should use the get () method, which accesses the database immediately and returns null if there is no corresponding row.
Cat cat= (Cat) sess.get (Cat.class, id); if (cat==null) {cat= new Cat (); sess.save (cat, id);} return cat
You can even choose a LockMode and use SQL's SELECT. FOR UPDATE mounts the object. Please consult the API documentation for more information.
Cat cat = (Cat) sess.get (Cat.class, id, LockMode.UPGRADE)
Note that any associated objects or contained collections are not returned as FOR UPDATE unless you specify lock or all as the cascade style of the association.
You can use the refresh () method to force the loading of an object and its collection at any time. This method is useful if you use the database trigger function to handle certain properties of the object.
That's all for sess.save (cat); sess.flush (); / / force the SQL INSERT sess.refresh (cat); / / re-read the state (after the trigger executes) "how Hibernate works with objects". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.