Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How Hibernate implements one-to-one, one-to-many and many-to-one association preservation

2025-01-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how to realize one-to-one, one-to-many and many-to-one association preservation in Hibernate". In daily operation, it is believed that many people have doubts about how to implement one-to-one, one-to-many and many-to-one association preservation in Hibernate. The editor consulted all kinds of materials and sorted out simple and useful operation methods. I hope it will be helpful to answer the questions of "how to achieve one-to-one, one-to-many and many-to-one association preservation in Hibernate"! Next, please follow the editor to study!

Catalogue

One-to-one association save:

Message table entity class configuration: (main table)

Message content table configuration: (from table)

Message table hbm.xml configuration: (main table)

Message list hbm.xml configuration: (from table)

One to many / many to one save

Activity table entity class configuration: (main table)

Custom enrollment feature entity class configuration: (from table)

Active table hbm.xml configuration: (master table)

Registration information form: (from table)

One-to-one association save:

Note: message status table: record the user's message information, such as the message person openid, message time, etc. (master table)

User message content table: record the user's message content, id and status table one-to-one (from the table)

Message table entity class configuration: (main table)

Add an one-to-one associated slave table

/ / one to oneprivate com.rhtcms.cms.entity.main.CustomerMessageContent customerMessageContent;// corresponding content table information message content table configuration: (from table)

Add an one-to-one associated master table:

/ / one to oneprivate com.rhtcms.cms.entity.main.CustomerMessage customerMessage;// corresponding to customer message status table message table hbm.xml configuration: (main table)

1. The main table id is randomly generated by uuid.

two。 Set up the slave table for one-to-one

Message list hbm.xml configuration: (from table)

1. The primary key generation strategy uses: foreign uses the primary key of another associated object as the primary key of that object. Mainly used in one-to-one relationships.

two。 Set the master table of one-to-one

CustomerMessage

Save method:

Train of thought: 1. When saving, first set the master table data, save the master table first.

2: set slave table data, the key step is to set the master table just saved, and then save the slave table

CustomerMessage customerMessageSave = mng.save (customerMessage); / / Save the state table information and get uuidCustomerMessageContent customerMessageContent = new CustomerMessageContent (); / / first filter the messages and feedback by sensitive words String wordCensor = cmsMng.replaceSensitivity (questionText); customerMessageContent.setQuestionText (wordCensor); / / set the content table content customerMessageContent.setCustomerMessage (customerMessageSave); / / set the properties contentMng.save (customerMessageContent) of the master table in the slave table; / / save the content table information one-to-many / many-to-one

Description: 1. Activity list: save activity information, record activity registration information, such as scene, organizer, registration time. (master table)

two。 Custom registration information form, due to the uncertainty of registration elements, eg: registration elements are, phone number, name, ticket id basis. (from the table)

Activity table entity class configuration: (main table)

1. Add custom enrollment information from the table

Private Set signs = new HashSet (); Custom enrollment feature entity class configuration: (from table)

1. Add an associated activity table entity class

Private Activity activity;// activity table information activity table hbm.xml configuration: (master table)

1. The primary key adopts the "native" strategy (hilo+identity+sequence chooses one of three)

Ps: choose one of the three generators I dentity, sequence and hilo according to the ability of the underlying database to automatically generate identifiers, which is suitable for development across database platforms.

two。 Configure one-to-many registration information from the table

Registration information form: (from table)

Configure many-to-one: name: main table many-to-one attribute, class: master table entity class column: many-to-one field

Save method:

Preserve ideas

1: the save method takes two parameters: parameter 1: data of the master table, and parameter 2: data of the slave table.

two。 The implementation layer first saves the master table, and then traverses the slave table. Again, the slave table is set up first, and then the slave table is saved.

ActivityMng.save (bean,set) / /. The following is the save method bean = dao.save (bean); / / update by January 23,2021 10:36:56 zqy optimized save interface for (ActivitySign activitySign: set) {activitySign.setActivity (bean); signDao.save (activitySign);} this, on "how to achieve one-to-one, one-to-many and many-to-one association preservation" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report