In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article is about what Inverse is in Hibernate. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.
Inverse is the basic concept of Hibernate two-way relationship, of course, for most entities, we do not need two-way association, more likely to choose one-way association, and most of us generally use one-to-many relationship, and the other end of one-to-many two-way association: many-to-one Inverse attribute does not exist, in fact, it defaults to Inverse=false. Thus, it prevents the random setting of Inverse on one-to-many ends without making mistakes. But the improper setting of Inverse does have a big performance impact, which we must pay attention to.
This article has analyzed in detail the impact of improper Hibernate Inverse settings: http://www.Hibernate.org/155.html, after reading this article, it is necessary to write some more summary:
1) the side mentioned in Hibernate Inverse actually refers to the concept of a class or table, and two-way association means that both parties can access each other's applications.
2) the term relationship maintenance is still a little vague or obscure. We generally say that class An or table A (the table here refers to many-to-many join tables) is responsible for maintaining the relationship. In fact, what I mean here is that when I apply to update, create, delete (read needless to say, two-way references appear for the convenience of reading) Class An or A table, the SQL statement created at this time must be responsible for ensuring the correct modification of the relationship.
3) the side side of Inverse=false (side actually refers to the class element where Inverse=false is located) is responsible for maintaining relationships, while the Inverse=true side does not need to maintain these relationships.
4) when we say that the improper setting of Hibernate Inverse will lead to poor performance, we actually mean that the improper setting of Inverse will result in redundant repetitive SQL statements and even lead to JDBC exception's throw. This is what we must pay attention to when building entity class relationships. Generally speaking, Inverse=true is recommended. If both parties set Inverse=false in a two-way association, it will cause both parties to update the same relationship repeatedly. However, if both sides set up Inverse=true, neither side will maintain the update of the relationship. Fortunately, one end of one-to-many: many-to-one defaults to Inverse=false, which avoids this kind of error. However, many-to-many does not have this default setting, so many people often use Inverse=true on both ends of many-to-many. As a result, the data of the join table is not recorded at all, just because they are not responsible for maintaining the relationship. Therefore, the setting of * * in two-way association is Inverse=true on one end and Inverse=false on the other. Generally speaking, the Inverse=false will be put on the side of the many, so someone asked, there are many on both sides of the many-to-many, where on earth is the Inverse? In fact, Hibernate establishes many-to-many relationships by separating them into two one-to-many relationships, joining a join table in the middle. So GM has an one-to-many relationship, and it can also be said that one-to-many is a basic part of many-to-many.
If you look at the many-to-many definition below, you will know more about the relationship between "many-to-many" and "one-to-many": when we pay attention to the characteristics of the tag, we know that it defines a many-to-many relationship, not.
In many pairs, because one side maintains the relationship and the other does not maintain the relationship, we must be careful to avoid establishing a relationship with classes that do not maintain the relationship in the application, because the relationship established in this way will not be stored in the database. An example is given based on the mapping file code above:
Package org.Hibernate.auction; import java.util.*; / * * @ author Administrator * * To change the template for this generated type comment go to * Window > Preferences > Java > Code Generation > Code and Comments * / public class TestA {int id; String name; Set testBs=new HashSet (); public TestA () {} public TestA (int id) {setId (id);} public int getId () {return id;} public void setId (int id) {this.id=id } public String getName () {return name;} public void setName (String name) {this.name=name;} public Set getTestBs () {return testBs;} public void setTestBs (Set s) {testBs=s;} public void addTestB (TestB tb) {testBs.add (tb);} public static void main (String [] args) {} public class TestB {int id; String name; Set testAs=new HashSet () Public TestB () {} public TestB (int id) {setId (id);} public int getId () {return id;} public void setId (int id) {this.id=id;} public String getName () {return name;} public void setName (String name) {this.name=name;} public Set getTestAs () {return testAs;} public void setTestAs (Set s) {testAs=s } public void addTestA (TestA ta) {testAs.add (ta);} public static void main (String [] args) {}}
Test the code:
Public void doTest () throws Exception {TestA a1=new TestA (1); TestA a2=new TestA (2); TestA a3=new TestA (3); TestB b1=new TestB (1); TestB b2=new TestB (2); TestB b3=new TestB (3); a1.addTestB (b1); a1.addTestB (b2); a1.addTestB (b3); b2.addTestA (A1); b2.addTestA (a2); Session s = factory.openSession () S = factory.openSession (); Session session = factory.openSession (); session.save (A1); session.flush (); session.close ();}
The data of the join table after the test is:
Testa testb
1 1
1 2
1 3
According to the Inverse rules, the database does not store the relationships established by the code: b2.addTestA (A1); b2.addTestA (a2), because TestB is not responsible for maintaining these relationships, so the resulting sql statements naturally do not have operations on the Testa_ test B table. Assuming that these methods are really needed in application, we can modify the method of TestB to make them pay attention to performing the appropriate operation in the maintainer class so that the relationship can be saved in the database, as shown below:
/ * * Created on 2004-7-25 * * To change the template for this generated file go to * Window > Preferences > Java > Code Generation > Code and Comments * / package org.Hibernate.auction; import java.util.*; / * @ author Administrator * * To change the template for this generated type comment go to * Window > Preferences > Java > Code Generation > Code and Comments * / public class TestB {int id; String name; Set testAs=new HashSet () Public TestB () {} public TestB (int id) {setId (id);} public int getId () {return id;} public void setId (int id) {this.id=id;} public String getName () {return name;} public void setName (String name) {this.name=name;} public Set getTestAs () {return testAs;} public void setTestAs (Set s) {testAs=s } public void addTestA (TestA ta) {testAs.add (ta); ta.addTestB (this);} public static void main (String [] args) {}}
Then the data of the join table after the test execution is:
Testa testb
1 2
1 3
1 1
2 2
The test passed.
Thank you for reading! This is the end of this article on "what is Inverse in Hibernate?". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!
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.