In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "what is the Hibernate mapping association". The content of 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 what the Hibernate mapping association is.
Hibernate mapping relationships are the same as those of things in our real world. For example, in the UML language, take the relationship between customer Customer and order Order as an example. A customer can send multiple orders, while an order can only belong to one customer, which is an one-to-many association, so it can be an one-way association. If two or two kinds of association relations are included at the same time, it becomes a two-way association. In a relational database, only the foreign key references the primary key. So relational databases actually support one-to-one, or one-to-many one-way relationships. In the relationship between classes. The many-to-one relationship best matches the foreign key reference primary key relationship in the database. So if an one-way association is used for a many-to-one one-way association from an order to a customer, a customer attribute is defined in the order class. Indicates which customer the order belongs to, and the customer class does not need to define the collection properties that hold the order. Let's write a simple example.
/ / first define the access method of the customer class public class Customer implements Sreializable {private Long id; private String name; / / omitted attribute} / / then define the access method of the omitted attribute of the order class public class Order implements Sreializable {private Long id; private String orderName; private Customer customer; / /. Pay attention to the access method of Customer. }
All the properties of the Customer class correspond to all the properties of the CUSTOMERS table one-to-one, which is relatively easy to create. Let's take a look at the mapping file for the Order class.
Because the customer attribute is of type Customer, while the CUSTOMER_ID of the ORDERS table is of integer type, it does not match. So we can't define it with ordinary elements, but we need to use elements to configure it.
< many-to-one>The element is responsible for establishing the mapping between the customer attribute of the Order order class and the CUSTOMER_ID foreign key field in the database.
◆ name: sets the attribute name of the mapping file
◆ column: sets the foreign key name of the table corresponding to the persistence class
◆ class: sets the type of the property of the persistent class, which specifies the specific class, that is, the class where the primary key exists
◆ not-null: set to true means that the customer property is not allowed to be null, and the default is false. This property will affect the bhm2ddl tool. It will set the CUSTOMER_ID foreign key of the ORDERS table to not allow empty constraints, but will not affect the hbm2java tool to grow java source code. It also affects the behavior of the Hibernate runtime, checking whether the customer property is null when saving the Order object. The database file compiled with hbm2ddl is as follows:
Create table CUSTOMERS (
ID bigint not null
NAME varchar (15)
Primary key (ID)
);
Create table ORDERS (
ID bigint not null
ORDER_NUMBER varchar (15)
CUSTOMER_ID bigint not null
Primary key (ID)
);
Alter table ORDERS add index FK8B7256E516B4891C (CUSTOMER_ID), add constraint
FK8B7256E516B4891C foreign key (CUSTOMER_ID) references CUSTOMERS (ID)
When we see the results, we can simply understand it as the role of creating foreign keys in the database. The above example simply demonstrates the Hibernate mapping association, while the one-to-many association is a little more complex than this. And you can see that when Hibernate persists a temporary object, by default it does not automatically persist associated with other temporary objects, but throws a TransientObjectException exception. If you want Hibernate to persist the associated object automatically when it persists the object, set the cascade attribute of the element to save-update, which means cascading operation, and the default value of the cascade attribute is none. When this property is set to OK. The database implements the operation of cascading saving updates.
Thank you for your reading, the above is the content of "what is the Hibernate mapping association". After the study of this article, I believe you have a deeper understanding of what the Hibernate mapping association is, 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.