In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "how to understand Hibernate persistence technology", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to understand Hibernate persistence technology.
In general applications, the classes used to implement business problem entities (such as Customer and Order in e-commerce applications) are persistence classes. You cannot assume that all instances of persistent classes are in a persistent state-- the state of an instance may also be transient or unmanaged.
Hibernate works better if these Hibernat persistence classes follow some simple rules, also known as the simple traditional Java object (POJO:Plain Old Java Object) programming model. But these rules are not necessary. In fact, Hibernate3 makes almost no assumptions about your persistence class. You can express the domain model in other ways: for example, using the tree structure of Map instances.
A simple POJO example illustrates Hibernate persistence
Most Java programs need a persistent class to represent cats.
Package eg; import java.util.Set; import java.util.Date; public class Cat {private Long id; / / identifier private Date birthdate; private Color color; private char sex; private float weight; private int litterId; private Cat mother; private Set kittens = new HashSet (); private void setId (Long id) {this.id=id;} public Long getId () {return id } void setBirthdate (Date date) {birthdate = date;} public Date getBirthdate () {return birthdate;} void setWeight (float weight) {this.weight = weight;} public float getWeight () {return weight;} public Color getColor () {return color } void setColor (Color color) {this.color = color;} void setSex (char sex) {this.sex=sex;} public char getSex () {return sex;} void setLitterId (int id) {this.litterId = id;} public int getLitterId () {return litterId } void setMother (Cat mother) {this.mother = mother;} public Cat getMother () {return mother;} void setKittens (Set kittens) {this.kittens = kittens;} public Set getKittens () {return kittens } / / addKitten not needed by Hibernate public void addKitten (Cat kitten) {kitten.setMother (this); kitten.setLitterId (kittens.size ()); kittens.add (kitten);}}
Here are four main rules to follow:
1. Implement a default (that is, no parameter) constructor (constructor)
Cat has a no-argument constructor. All persistence classes must have a default constructor (which may not be public) so that Hibernate can instantiate them using Constructor.newInstance (). We strongly recommend that in Hibernate, for run-time proxy generation, the constructor is at least visible within the package.
two。 Provide an identity attribute (identifier property) (optional)
Cat has an attribute called id. This property maps the primary key field of the database table. This property can be called by any name, and its type can be any primitive type, primitive wrapper type, java.lang.String, or java.util.Date. If your legacy database table has a federated primary key, you can even use a user-defined class that has these types of properties. See the following section on federated identifiers. )
The identifier property is optional. You can leave it alone and let Hibernate track the identification of objects. But we don't recommend it.
In fact, some features work only for classes that declare identifier attributes:
Propagating reconnection of managed objects (cascading updates or cascading merging)-see Section 10.11, "propagating persistence (transitive persistence)"
◆ Session.saveOrUpdate ()
◆ Session.merge ()
We recommend that you declare consistent identity attributes for persistent classes. We also recommend that you use a type that can be empty (that is, not the original type).
3. Use non-final classes (optional)
Proxies is an important function of Hibernate, which depends on the condition that the persistent class is either non-final or implements an interface that all methods declare as public.
You can use Hibernate to persist a final class that does not implement any interface, but you cannot use proxies to delay associative loading, which limits your choice of performance optimization.
You should also avoid declaring public final methods in non-final classes. If you want to use a class with a public final method, you must explicitly disable the proxy by setting the lazy= "false".
4. Declare an accessor (accessors) and a mutable flag (mutators) for the persistent field (optional)
Cat declares access methods for all of its persistent fields. Many other ORM tools persist instance variables directly. We believe that it would be better to introduce an indirect layer (originally "indirect", indirection) between the relational database schema and the internal data structure of the class. By default, Hibernate persists JavaBeans-style properties, recognizing method names in the form of getFoo,isFoo and setFoo. If necessary, you can have direct field access to certain properties.
Property does not need to be declared as public. Hibernate can persist a get/set method with default, protected, or private for Hibernate persistence of the property.
User extension framework documentation in the TODO:property and proxy packages.
At this point, I believe you have a deeper understanding of "how to understand Hibernate persistence technology". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.