In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "Hibernate one-to-many, many-to-one, many-to-many, one-to-one relationship". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "Hibernate one-to-many, many-to-one, many-to-many, one-to-one relationship."
The three instantiation classes used in this article are Customer (save customer basic information), CustomerDetails (save customer details), and CustomerBack (intermediate table, save the association between the two tables).
The corresponding table function is the same as above. The main table in the description refers to Customer, the child table refers to CustomerDetails, and the intermediate table refers to CustomerBack
One to many
< set name= CustomerDetails "table=" customer_details "inverse=" false "cascade=" all "sort=" unsorted "> < key column=" cust_Id "/ > < one to many class=" com.hibernate.otm.CustomerDetails "/ > < / set >
This code needs to be placed in the master table configuration in one-to-many
◆ name: the name of the collection property (which can also be understood as the sub-property name in one-to-many)
◆ table (optional, default is the name of the attribute): the name of the collection table (which can be understood as the name of an one-to-many sub-table)
◆ inverse (optional, default is false): Mark this collection as one end of the direction in a two-way association relationship
◆ cascade (optional: default is none): cascading operations to child entities
◆ sort (optional): specifies the sort order of the collection
For others such as schema,lazy,order-by,where,outer-join,batch-size,access, please refer to the relevant books.
Column= "cust_Id" in key, cust_Id is not a field of the main table, but a field that holds the main table in the child table.
For example, if the primary key of the primary table is cust_Id, in order to save the primary key of the primary table in the child table, also build a cust_Id in cust_Id,key that is the cust_Id in the sub-table.
Class in one-to-many is an one-to-many child table instance class.
Many to one
< many to one name= Customer "class=" Customer "cascade=" all "outer join=" false "update=" false "insert=" false "column=" cust_Id "not null=" true "/ >
Many-to-one, not like one-to-many, but directly used many-to-one. This code needs to be placed in the many-to-one child table configuration (that is, the CustomerDetails configuration)
◆ outer-join: for external links
◆ update,insert: specifies whether the corresponding field is included in the SQL statement for UPDATE and / or INSERT.
◆ column: the associated field name. (field names corresponding to multiple pairs of main classes)
◆ not-null: whether it is null.
Many to many
< set name= "CustomerBack" table= "customer_back" cascade= "all" > < key column= "cust_Id" / > < many to many class= "CustomerDetails" column= "custD_id" / > < / set >
Many-to-many configuration is somewhat similar to one-to-many configuration. This code is also placed in the same position as one-to-many code.
In many-to-many relationships, intermediate tables are often used to associate two tables with many relationships. For example, if there is a many-to-many relationship between Customer and CustomerDetails, then another table, such as the CustomerBack table, is often used to store their relationship, that is, the intermediate table.
◆ name: the name of the attribute (which can be understood as the name of the intermediate table)
◆ table: the name of the table (which can be understood as the table name of the intermediate table)
Cust_Id in ◆ key refers to the cust_Id of the intermediate table. In order to preserve the relationship between Customer and CustomerDetails, cust_Id (the primary key of Customer) and custD_Id (the primary key of CustomerDetails) are built in the intermediate table respectively.
Class in ◆ many-to-many refers to another instantiated class in many-to-many, and column refers to the field name that CustomerDetails associates with the intermediate table. Here, it refers to the middle table.
◆ custD_Id cannot be understood as the primary key of CustomerDetails.
one-for-one
< one to one name= "CustomerDetails" class= "CustomerDetails" cascade= "all" / >
An one-on-one relationship is simpler.
Just put this code in the configuration of the Customer table.
◆ name: attribute name
◆ class: the associated instantiated class name
Thank you for your reading, the above is the content of "Hibernate one-to-many, many-to-one, many-to-many, one-to-one relationship". After the study of this article, I believe you have a deeper understanding of how Hibernate one-to-many, many-to-one, many-to-many, one-to-one relationship can be realized, 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.