Java framework --- hibernate (one-to-many) ing, java --- hibernate

Source: Internet
Author: User

Java framework --- hibernate (one-to-many) ing, java --- hibernate

One-to-multiple relationships can be classified into one-way and two-way relationships.

One-to-multiple relationship

One way is to find the other party only from one side, usually from the master class to find the class (table) with the foreign key ). For example, a mother can have multiple children, and the child has the mother's primary key as the foreign key. The relationship between mothers and children is one-to-many. If you want to perform operations on the mother information and reflect the information on the child information, you can configure cascade = "all" on the collection attribute of the mother configuration file ", indicates cascade Update Configuration of associated entities.

"Master" end: Multiple ends

<? Xml version = "1.0"?> <! DOCTYPE hibernate-mapping PUBLIC "-// Hibernate/Hibernate DTD ing DTD 3.0 // EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 

One end:

<?xml version="1.0"?><!DOCTYPE hibernate-mapping PUBLIC        "-//Hibernate/Hibernate Mapping DTD 3.0//EN"        "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

One-to-multiple unidirectional ing: the multi-to-one ing is maintained at one end of the multi-node, and the one-to-many ing is maintained by one end.

"Master" side: one end maintains this relationship, and uses the set tag to add the field ClazzId to the t_student table for association.

One-to-Multiple Association ing utilizes the principle of multi-to-one association ing:

Multi-to-one association ing: Add a foreign key to multiple ends and point to one end.

One-to-Multiple Association ing: Add a foreign key to one end on multiple ends, and maintain a relationship that is directed to multiple

That is to say, the one-to-many and multi-to-one ing policies are the same, but the station angles are different.

Disadvantages of the relationship maintained at one end: If the clazzId field in the t_student table is set to non-empty, it cannot be saved.

One-to-multiple bidirectional

One-to-multiple bidirectional. Each Student has a clazz reference during bidirectional Association, so the clazz. hbm. xml file configuration is somewhat different.

<? Xml version = "1.0"?> <! DOCTYPE hibernate-mapping PUBLIC "-// Hibernate/Hibernate DTD ing DTD 3.0 // EN" "http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd"> 

Use <set> On a one-end set to add a foreign key to the target table to point to one-end.

Use the <allow-to-one> label on multiple ends

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.