In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to achieve Hibernate many-to-many". Friends who are interested may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "how to achieve Hibernate many-to-many"!
Assuming that there are two categories of User and Server, a User can be authorized to use more than one Server, and the user who is authorized to use it is also recorded on the Server, even if there is a many-to-many relationship between User and Server.
In programming, it is basically not recommended to establish a many-to-many relationship between User and Server directly, which will make User and Server depend on each other, usually through an intermediary type to negotiate the many-to-many relationship between users, so as to avoid the interdependence of users.
If you must directly establish a many-to-many relationship between User and Server, Hibernate is also supported. Basically, as long as you understand the two physical mappings of the previous two, it is not difficult to establish a many-to-many relationship in configuration.
First, take a look at the User and Server categories we have designed:
Java code
Package onlyfun.caterpillar
Import java.util.*
Publicclass User {
Privatelong id
Private String name
Private Set servers = new HashSet ()
Publiclong getId () {
Return id
}
Publicvoid setId (long id) {
This.id = id
}
Public String getName () {
Return name
}
Publicvoid setName (String name) {
This.name = name
}
Public Set getServers () {
Return servers
}
Publicvoid setServers (Set servers) {
This.servers = servers
}
}
Java code
Package onlyfun.caterpillar
Import java.util.*
Publicclass Server {
Privatelong id
Private String address
Private Set users = new HashSet ()
Publiclong getId () {
Return id
}
Publicvoid setId (long id) {
This.id = id
}
Public String getAddress () {
Return address
}
Publicvoid setAddress (String address) {
This.address = address
}
Public Set getUsers () {
Return users
}
Publicvoid setUsers (Set users) {
This.users = users
}
}
Each of these uses HashSet to preserve their relationships, and on many-to-many relational mapping, we can establish a single-to-many relationship, which directly links the mapping, and by setting the inverse= "true", the maintenance of the relationship is handed over to one of the parties to maintain. The result of this, in the writing of the source code, is also more important than the maintenance of objects that conform to the Java, that is, both parties have to set to the other party for reference.
First, let's take a look at User.hbm.xml:
Xml code
Xmlversion= "1.0"? >
PUBLIC "- / / Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
Id >
Property >
Set >
Class >
Hibernate-mapping >
In the database, the many-to-many relationship between the database tables is done through an intermediary's data table. For example, in this example, the USER table and the USER_SERVER table are one-to-many, while USER_SERVER is many-to-one to SERVER, thus completing the many-to-many relationship from USER to SERVER. In the USER_SERVER database table, there will always be a USER_ ID and server _ ID as the host, and USER_ID as an external reference to USER. SERVER_ID serves as an outside reference to SERVER.
Take a look at the Server.hbm.xml mapping file:
Xml code
Xmlversion= "1.0"? >
PUBLIC "- / / Hibernate/Hibernate Mapping DTD//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
Id >
Set >
Class >
Hibernate-mapping >
The configuration is similar to User.hbm.xml, except that inverse= "true" is added, which means that the maintenance of the relationship is left to the other end. Note that our cascade in User and Server is set to save-update. In many-to-many relationships, cascade such as all and delete are meaningless. Because many-to-many, the included child objects cannot be deleted because the parent object is deleted, because there may be other parent objects involved in this child object.
We use the following program to test:
Java code
Import onlyfun.caterpillar.*
Import net.sf.hibernate.*
Import net.sf.hibernate.cfg.*
Publicclass HibernateTest {
Publicstaticvoid main (String [] args) throws HibernateException {
SessionFactory sessionFactory = new Configuration (). Configure (). BuildSessionFactory ()
Server server1 = new Server ()
Server1.setAddress ("PC-219")
Server server2 = new Server ()
Server2.setAddress ("PC-220")
Server server3 = new Server ()
Server3.setAddress ("PC-221")
User user1 = new User ()
User1.setName ("caterpillar")
User user2 = new User ()
User2.setName ("momor")
User1.getServers () add (server1)
User1.getServers () add (server2)
User1.getServers () add (server3)
Server1.getUsers () add (user1)
Server2.getUsers () add (user1)
Server3.getUsers () add (user1)
User2.getServers () add (server1)
User2.getServers () add (server3)
Server1.getUsers () add (user2)
Server3.getUsers () add (user2)
Session session = sessionFactory.openSession ()
Transaction tx= session.beginTransaction ()
Session.save (user1)
Session.save (user2)
Tx.commit ()
Session.close ()
SessionFactory.close ()
}
}
Note that because inverse= "true" is set, the cross reference between User and Server must be configured separately to see how it is actually stored in the database:
Select * FROM USER
+-+ +
| | USER_ID | NAME |
+-+ +
| | 1 | caterpillar |
| | 2 | momor |
+-+ +
Select * FROM USER_SERVER
+-+ +
| | SERVER_ID | USER_ID |
+-+ +
| | 1 | 1 |
| | 2 | 1 |
| | 3 | 1 |
| | 1 | 2 |
| | 2 | 2 |
+-+ +
Select * FROM SERVER
+-+ +
| | SERVER_ID | address |
+-+ +
| | 1 | PC-219 |
| | 2 | PC-221 |
| | 3 | PC-220 |
+-+ +
At this point, I believe you have a deeper understanding of "how to achieve Hibernate many-to-many". 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.