Chapter 06 ing One-to-multiple bidirectional associations and cascade and inverse attributes

Source: Internet
Author: User
Tags set set

When a class is associated with a class, you can easily navigate from one object to another. Or navigate to a group of objects through a set. For example:

If you want to obtain the dept object associated with the given EMP object, you only need to call the following method:

Dept dept = EMP. getdept (); // navigate from the EMP object to the associated dept object

Take dept (department) and EMP (employee) as examples:

1. Configure bidirectional one-to-Multiple Association

Add a set-type EMPs attribute to the dept class.

    private Set<Emp> emps=new HashSet<Emp>();    public Set<Emp> getEmps() {    return emps;}    public void setEmps(Set<Emp> emps) {    this.emps = emps;

How to map the EMPs attribute of the set type in the ing file. The dept table does not have fields directly corresponding to the EMPs attribute. Therefore, you must use the <set> element instead of the <property> element to map EMPs attributes:

<Set name = "EMPs"> <key column = "deptno"> </key> <! -- Multiple EMP Foreign keys --> <one-to-many class = "EMP"/> </set>

Resolution:

<Set> element name attribute: Set the attribute name of the persistence class. This is the EMPs attribute of the dept class.

The <set> element also contains two child elements:

① <Key> element: the column attribute sets the foreign key of the table corresponding to the associated persistence class.

② <One-to-learn> element: sets the class attribute and the associated persistence class.

Hibernate obtains the following information based on the above ing code:

① <Set> the element indicates that the EMPs attribute of the dept class is of the Java. util. Set set type.

② <One-to-hand> sub-elements indicate that the EMPs set stores a group of EMP objects.

③ <Key> the child element indicates that the EMP table uses the foreign key deptno to refer to the dept table.

Dept. HBM. XML code:

<? 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"> 

EMP. HBM. xml:

<? 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"> 

Then write the test class: test to get the employee name with deptno 1.

Public class Test2 {session; transaction TX; @ after public void aftertest () {Tx. commit (); hibernateutil. closesession () ;}@ before public void initdata () {session = hibernateutil. getsession (); Tx = session. begintransaction ();}/** one-to-multiple bidirectional association test */@ test public void onetomanydoubletest () {// obtain the dept = (Dept) Session of the employee set. load (Dept. class, 1); set <EMP> EMPs = Dept. getemps (); For (EMP: EMPs) {system. out. println (EMP. getempname ());}}

Ii. Cascade attributes

None: when the session is used to manipulate the current object, other associated objects are ignored. It is the default value of the cascade attribute.

Save-Update: when the current object is saved or updated through the SAVE (), update (), and saveorupdate () Methods of the session, cascade stores all newly created objects in the transient state of the Association, and cascade updates all objects in the correlated free state.

Delete: when the current object is deleted using the delete () method of the session, all associated objects are deleted cascade.

When the current object is deleted using the delete () method of the session, all associated objects are deleted cascade.

ALL: contains the save-update and Delete actions.

Resolution:

Cascade: When we save Persistent Object A, it automatically saves Persistent object B.

Q: Where is the cascade attribute written?

Note: When one-to-one or multiple-to-one operations are performed, they are directly written on the tag, while others are written on the set tag.

How does one automatically add employees when adding departments?

Analysis: Cascade (cascade) mode can be used.

 

Test: two-way Association adds the newly created employee object to the Department through add ()

Public class test3 {session; transaction TX; @ after public void aftertest () {Tx. commit (); hibernateutil. closesession () ;}@ before public void initdata () {session = hibernateutil. getsession (); Tx = session. begintransaction ();}/** cascade */@ test public void onetest () {// construct a department dept = new dept (); Dept. setdeptname ("Finance Department"); // construct an employee EMP = new EMP (); EMP. setempname ("Zhang San"); // specifies the department of the employee (EMP. setdept (Dept); // Dept of the employee in the setxxx department. getemps (). add (EMP); // save session. save (Dept); Session. save (EMP );}}

Iii. Inverse attributes under the <set> element (reverse)

The inverse attribute specifies the direction of the link.

If inverse is set to false, it is the active party, and the active party is responsible for maintaining the association relationship. The default value is false.

Note: inverse determines whether to reflect the changes made to the objects in the database. Therefore, inverse only applies to the set, that is, it is only valid for one-to-Minus or minus-to-minus (because only these two associations contain a set, one-to-one and others-to-one only contain a reference of the other party ).

Same code:

NOTE: If I specify a department for an employee and add the employee to the Department set. If reverse is not set at this time, the following SQL statement is generated:

Inverse is set to true and is not responsible for maintaining associations.

The second insert statement has already specified its own part in the employee table, and there is no need to send an update command to the database.

After inverse is set to true, the generated statement is shown in.

 

Chapter 06 ing One-to-multiple bidirectional associations and cascade and inverse attributes

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.