In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "what is the advanced set mapping of Hibernate". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is the Hibernate advanced set mapping"?
Hibernate advanced set mapping is mainly divided into ordered set, two-way association, two-way association, involving ordered set class, triple association (Ternary associations), and usage.
1. Ordered set (Sorted collections)
Hibernate advanced collection mapping supports collections that implement java.util.SortedMap and java.util.SortedSet. You must specify a comparator in the mapping file:
The values allowed in the sort attribute include unsorted,natural and the name of a class that implements java.util.Comparator.
The act of classifying a set is in fact like java.util.TreeSet or java.util.TreeMap.
If you want the database to sort the collection elements itself, you can use the order-by attribute in the set,bag or map mapping. This solution can only be implemented in jdk1.4 or later versions of jdk (via LinkedHashSet or LinkedHashMap). It does the sorting in the SQL query, not in memory.
Note: the value of this order-by attribute is a SQL sort clause, not HQL!
Associations can also be sorted by arbitrary criteria at run time using the collection filter ().
SsortedUsers = s.createFilter (group.getUsers (), "order by this.name") .list ()
two。 Bidirectional association (Bidirectional associations)
A two-way association allows access to the other end through either end of the association. In Hibernate, two types of bidirectional associations are supported:
◆ one-to-many (one-to-many)
The Set or bag value is at one end, and the single value (not the set) is at the other end.
◆ many-to-many (many-to-many)
Both ends have set or bag values
To create a two-way many-to-many association, simply map two many-to-many associations to the same database table and define one end as inverse (which side you choose, but it cannot be an index collection).
Here is an example of a two-way association of many-to-many; each category can have many items, and each items can belong to many categories:
......
If only the reverse side of the association is changed, the change will not be persisted. This means that Hibernate has two representations in memory for each two-way association, one from A to B and the other from B to A. If you think back to the Java object model, how we created many-to-many relationships in Java makes it easier for you to understand:
Category.getItems (). Add (item); / / The category now "knows" about the relationship item.getCategories (). Add (category); / / The item now "knows" about the relationship session.persist (item); / / The relationship won''t be saved! session.persist (category); / / The relationship will be saved
The non-reverse side is used to save the in-memory representation to the database.
To create an one-to-many two-way association, you can map an one-to-many association as a many-to-one association to a field in the same table, and define inverse= "true" on the "many" side.
.... ....
Defining inverse= "true" on the "one" side does not affect the cascading operation, the two are orthogonal concepts!
3. Two-way association, involving ordered collection classes
Special consideration should be given to the two-way association in which one end is or. If an attribute in a subclass is mapped to an index field, no problem, we can still use inverse= "true" on the collection class map:
.... ....
However, if there is no such attribute in the subclass, we cannot consider the association to be a true two-way association (information asymmetry, there is some information that is not available at one end of the association). In this case, we cannot use inverse= "true". We need to use this:
.... ....
Note that in this mapping, the value side of the collection class in the association is responsible for updating the foreign key. TODO: Does this really result in some unnecessary update statements?
4. Triple correlation (Ternary associations)
There are three possible ways to map a triple association. * use a Map and use an association as its index:
The second approach is to simply re-model the association as an entity class. This makes the method we use most often.
* one option is to use compound elements, which we will discuss later
5. Use
If you fully believe in our view that federated primary keys (composite keys) are bad, and that entities should use (inorganic) self-generated substitute identifiers (surrogate keys), you may find it a little strange that the many-to-many associations and value sets we have shown so far are mapped to tables with federated primary keys! Now, this is highly debatable; it seems that a simple relational table does not benefit from alternative identifiers (although there may be some benefit from using a collection of combined values). However, Hibernate provides a (a little bit experimental) function that allows you to put many-to-many associations and value sets to get a table that uses alternative identifiers.
Attribute lets you use bag semantics to map a List (or Collection).
You can understand that the artificial id generator is like an entity class! Each row of the collection has a different artificial keyword. However, Hibernate does not provide any mechanism for you to get artificial keywords for a particular line.
Pay attention to the update performance is much higher than the normal one! Hibernate can effectively navigate to different rows and update or delete separately, just like dealing with a list, map, or set.
In the current implementation, it is not supported to use the identity identifier generator strategy to generate identifiers for collections.
At this point, I believe that you have a deeper understanding of "what Hibernate advanced set mapping is". 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.