In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains the "Hibernate image file is what", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in-depth, together to study and learn "what is the Hibernate image file" bar!
Hibernate image file instance
We have benefited a lot from switching to such a pure object id. Our implementation of the equals () and hashCode () methods is simpler and easier to read. These methods are no longer error-prone and work with Collection either before or after saving the object. Hibernate can also be faster because it no longer needs to read a sequence value from the database before saving a new object. In addition, the newly defined equals () and hashCode () are universal to an object that contains an id object. This means that we can move these methods to an abstract parent class. We no longer need to re-implement equals () and hashCode () for every domain object, and we no longer need to consider which fields are combined and unchanged for a class. We simply inherit this abstract class. Of course, we don't have to force our domain object to inherit a parent class, so we define an interface to ensure the flexibility of the design.
Public interface PersistentObject {public String getId (); public void setId (String id); public Integer getVersion (); public void setVersion (Integer version);} public abstract class AbstractPersistentObject implements PersistentObject {private String id = IdGenerator.createId (); private Integer version; public String getId () {return id;} public void setId (String id) {this.id = id;} public Integer getVersion () {return version;} public void setVersion (Integer version) {this.version = version } public boolean equals (Object o) {if (this = = o) return true; if (o = = null | |! (o instanceof PersistentObject)) {return false;} PersistentObject other = (PersistentObject) o; / / if the id is missing, return false if (id = = null) return false; / / equivalence by id return id.equals (other.getId ());} public int hashCode () {if (id! = null) {return id.hashCode () } else {return super.hashCode ();}} public String toString () {return this.getClass () .getName () + "[id=" + id + "]";}}
Now we have a simple and efficient way to create domain objects. They inherit AbstractPersistentObject, a parent class that automatically assigns them an id when they are created * and implements the methods equals () and hashCode () appropriately. The domain object also has a reasonable default implementation of the toString () method, which can be selectively overridden. If this is a test object or an example object for a query example, the id value can be changed or set to null. Otherwise, it should not be changed. If for some reason we need to create a domain object that inherits from another class, the object should implement the PersistentObject interface instead of inheriting the abstract class.
Public class Person extends AbstractPersistentObject {/ / Person-specific fields and behavior here}
The Hibernate image file will not change since the previous example. Instead of bothering Hibernate to understand the abstract parent class, we just need to make sure that the mapping file for each persistent object contains an id entry (and an assigned generator) and a version tag with the unsaved- value= "null" attribute. The astute reader may have noticed that every time a persistent object is instantiated, its id value is assigned. This means that when Hibernate creates a saved object in memory, even though the object already exists and is read from the database, it will also get a new id. This is not a problem because Hibernate then calls the object's setId () method, replacing the newly allocated id with the saved real id. The rest of the id generator is not a problem because the algorithm that implements it is lightweight (that is, it doesn't involve the database).
So far so good, but we left out an important detail: how to implement IdGenerator.createId (). We can define some standards for our ideal key-value generator (key-generation) algorithm.
◆ keys can be generated lightweight without involving the database.
Even if the ◆ spans different virtual machines and different machines, the key value must be guaranteed.
◆ keys can be generated by other programs, programming languages and databases if possible, or at least compatible with them
Thank you for your reading, these are the contents of "what is the Hibernate image file". After the study of this article, I believe you have a deeper understanding of what the Hibernate image file is, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.