3-Implementation of multiple-to-multiple associations in hibernate

Source: Internet
Author: User

Hibernate multi-to-many test records --

Test records for multiple-to-multiple ing:

1. Jeff class:
Public class Jeff {
Private int ID;
Private int version;
Private string name;
Private set heyhey = new hashset ();
// Omit getter setter
}
Ii. heyhey class
Public class heyhey {
Private int ID;
Private int version;
Private string name;
Private set heyhey = new hashset ();
// Omit getter setter
}
Iii. Jeff table:
Id int not-Null
Version int not-Null
Name varchar null
Iv. heyhey table:
Id int not-Null
Version int not-Null
Name varchar null
V. jeff_heyhey table, that is, the intermediate table:
Jeffid int not-Null
Heyheyid int not-Null
6. HBM files are modified during testing. Original code:
Jeff. HBM. xml file:
<Hibernate-Mapping
>
<Class
Name = "com. botwave. CNCD. Business. entity. Jeff"
Table = "Jeff"
Lazy = "true"
Optimistic-lock = "version"
>

<ID
Name = "ID"
Column = "ID"
Type = "int"
Length = "8"
Unsaved-value = "0"
>
<Generator class = "Identity">
</Generator>
</ID>

<Version
Name = "version"
Column = "version"
Type = "Java. Lang. Integer"
/>

<Set
Name = "heyhey"
Table = "jeff_heyhey"
Lazy = "true"
Cascade = "Save-Update"
>

<Key
Column = "jeffid"
>
</Key>

<Role-to-Role
Class = "com. botwave. CNCD. Business. entity. heyhey"
Column = "heyheyid"
Outer-join = "Auto"
/>

</Set>
<Property
Name = "name"
Type = "Java. Lang. String"
Update = "true"
Insert = "true"
Column = "name"
Length = "20"
/>

</Class>

</Hibernate-mapping>

Heyhey. HBM. xml file:

<Hibernate-Mapping
>
<Class
Name = "com. botwave. CNCD. Business. entity. heyhey"
Table = "heyhey"
Lazy = "true"
Optimistic-lock = "version"
>

<ID
Name = "ID"
Column = "ID"
Type = "int"
Length = "8"
Unsaved-value = "0"
>
<Generator class = "Identity">
</Generator>
</ID>

<Version
Name = "version"
Column = "version"
Type = "Java. Lang. Integer"
/>

<Set
Name = "Jeff"
Table = "jeff_heyhey"
Lazy = "true"
Inverse = "true"
Cascade = "Save-Update"
>

<Key
Column = "heyheyid"
>
</Key>

<Role-to-Role
Class = "com. botwave. CNCD. Business. entity. Jeff"
Column = "jeffid"
Outer-join = "Auto"
/>
</Set>
<Property
Name = "name"
Type = "Java. Lang. String"
Update = "true"
Insert = "true"
Column = "name"
Length = "20"
/>
</Class>

</Hibernate-mapping>
-----------------------------------------
First case: in the Set element of the heyhey. HBM. xml file, set the inverse element to true, and the other to the default one, that is, false.
The test code is as follows:
Jeffservice JS = new jeffserviceimpl ();
Heyhey hey1 = new heyhey ();
Jeff jeff1 = (Jeff) Js. findobjectbyid (1 );
Hey1.setname ("unknow ");
Jeff1.getheyhey (). Add (hey1 );
Sess. saveorupdate (jeff1 );
This is the code for updating an existing entity. The test result is as follows:
Hibernate: Select jeff0 _. ID as id0 _, jeff0 _. version as version0 _, jeff0 _. Name as name0 _ from Jeff jeff0 _ Where jeff0 _. ID =?
Hibernate: Select heyhey0 _. heyheyid as heyheyid __, heyhey0 _. jeffid as jeffid _ from jeff_heyhey heyhey0 _ Where heyhey0 _. jeffid =?
Hibernate: insert into heyhey (version, name) values (?, ?)
Hibernate: Update Jeff set version = ?, Name =? Where id =? And version =?
Hibernate: insert into jeff_heyhey (jeffid, heyheyid) values (?, ?)
Analysis: The first query is Js. findobjectbyid (1) is generated. The second query is generated by jeff1.getheyhey (), the intermediate table is queried, and the third, fourth, and fifth sentences are sess. saveorupdate (jeff1); generated.
When the session finds that jeff1 needs to be updated, its heyhey element has changed. Yu Jia first persisted hey1, so the third sentence, then update your own table (this statement does not work here because the data in this table cannot be updated), and insert a data record in the intermediate table.

In the second case, the configuration file remains unchanged. The Code is as follows:
Transaction Tx = sess. begintransaction ();
Jeffservice JS = new jeffserviceimpl ();
Heyheyservice HS = new heyheyserviceimpl ();
Heyhey hey1 = (heyhey) HS. findobjectbyid (7 );
Jeff jeff1 = (Jeff) Js. findobjectbyid (1 );
Hey1.setname ("Haha ");

Jeff1.getheyhey (). Add (hey1 );
System. Out. println ("Before update ");
Sess. saveorupdate (jeff1 );

TX. Commit ();
The test results are as follows:
Hibernate: Select heyhey0 _. ID as id0 _, heyhey0 _. version as version0 _, heyhey0 _. Name as name0 _ from heyhey heyhey0 _ Where heyhey0 _. ID =?
Hibernate: Select jeff0 _. ID as id0 _, jeff0 _. version as version0 _, jeff0 _. Name as name0 _ from Jeff jeff0 _ Where jeff0 _. ID =?
Hibernate: Select heyhey0 _. heyheyid as heyheyid __, heyhey0 _. jeffid as jeffid _ from jeff_heyhey heyhey0 _ Where heyhey0 _. jeffid =?
Hibernate: Update Jeff set version = ?, Name =? Where id =? And version =?
Hibernate: insert into jeff_heyhey (jeffid, heyheyid) values (?, ?)
Analysis: the first sentence is used to search for two existing entities, the third sentence is generated when getheyhey (), and the fourth sentence is generated when the updated object is generated. The fourth sentence also does not work.

Case 3: Set the inverse element to true in the Set element of the Jeff. HBM. xml file, and set the other to the default value, that is, false.
The test code is the same as the first one.
The test results are as follows:
Hibernate: Select jeff0 _. ID as id0 _, jeff0 _. version as version0 _, jeff0 _. Name as name0 _ from Jeff jeff0 _ Where jeff0 _. ID =?
Hibernate: Select heyhey0 _. heyheyid as heyheyid __, heyhey0 _. jeffid as jeffid _ from jeff_heyhey heyhey0 _ Where heyhey0 _. jeffid =?
Hibernate: insert into heyhey (version, name) values (?, ?)
Hibernate: Update Jeff set version = ?, Name =? Where id =? And version =?
Analysis: This time, Hibernate did not add data to the intermediate table. This is because the inverse element is changed. To be studied.

In the fourth case, the configuration file is the same as the preceding one, and the test code is the same as that in the second case.
Test results:
Hibernate: Select heyhey0 _. ID as id0 _, heyhey0 _. version as version0 _, heyhey0 _. Name as name0 _ from heyhey heyhey0 _ Where heyhey0 _. ID =?
Hibernate: Select jeff0 _. ID as id0 _, jeff0 _. version as version0 _, jeff0 _. Name as name0 _ from Jeff jeff0 _ Where jeff0 _. ID =?
Hibernate: Select heyhey0 _. heyheyid as heyheyid __, heyhey0 _. jeffid as jeffid _ from jeff_heyhey heyhey0 _ Where heyhey0 _. jeffid =?
Hibernate: Update heyhey set version = ?, Name =? Where id =? And version =?
Hibernate: Update Jeff set version = ?, Name =? Where id =? And version =?
Analysis: At this time, both tables are updated, but no data is inserted into the intermediate table. It is an inverse element. Pay attention to this situation first. Study it slowly.

In the fifth case, the configuration file is the same as above, and the code is:
Transaction Tx = sess. begintransaction ();
Jeffservice JS = new jeffserviceimpl ();
Heyheyservice HS = new heyheyserviceimpl ();
Heyhey hey1 = new heyhey ();
Jeff jeff1 = new Jeff ();
Hey1.setname ("Ahah ");
Jeff1.setname ("haefe1 ");

Jeff1.getheyhey (). Add (hey1 );
System. Out. println ("Before update ");
Sess. saveorupdate (jeff1 );

TX. Commit ();
Creates two objects and then establishes a relationship. Result:
Hibernate: insert into Jeff (version, name) values (?, ?)
Hibernate: insert into heyhey (version, name) values (?, ?)
Still not inserted to the intermediate table.

In the sixth case, the configuration is changed back. The Code is the same as above. Result:
Hibernate: insert into Jeff (version, name) values (?, ?)
Hibernate: insert into heyhey (version, name) values (?, ?)
Hibernate: insert into jeff_heyhey (jeffid, heyheyid) values (?, ?)
This time we can see the inserted intermediate table.

In the seventh case, You want to retrieve some records from a table, create an object for the other table, and save the record set and associated object of the former. The Code is as follows:
Transaction Tx = sess. begintransaction ();
Jeffservice JS = new jeffserviceimpl ();
Heyheyservice HS = new heyheyserviceimpl ();
Integer [] as = new integer [3];
As [0] = new INTEGER (1 );
As [1] = new INTEGER (2 );
As [2] = new INTEGER (3 );
List heyheys = HS. findobjectsbyids (AS); // you can find several existing records.
Jeff jeff1 = new Jeff ();
Jeff1.setname (" ");
Set set = new hashset ();
Set. Add (heyheys );

Jeff1.setheyhey (SET );
System. Out. println (jeff1.getheyhey ());
System. Out. println ("Before update ");
Sess. saveorupdate (jeff1 );

TX. Commit ();
The result is as follows:
Hibernate: select this. ID as id0 _, this. version as version0 _, this. Name as name0 _ from heyhey this where this. ID in (?, ?, ?)
[[COM. botwave. CNCD. business. entity. heyhey @ 12696c2, Com. botwave. CNCD. business. entity. heyhey @ 98350a] // print out what is in heyhey.
Before update
Hibernate: insert into Jeff (version, name) values (?, ?)
Net. SF. hibernate. mappingexception: No persister for: Java. util. arraylist // throw an exception and cannot persist the List class.

In the eighth case, the objective is the same as that in the seventh case. The Code is as follows:
Transaction Tx = sess. begintransaction ();
Jeffservice JS = new jeffserviceimpl ();
Heyheyservice HS = new heyheyserviceimpl ();
Integer [] as = new integer [3];
As [0] = new INTEGER (1 );
As [1] = new INTEGER (2 );
As [2] = new INTEGER (3 );
List heyheys = HS. findobjectsbyids ();
Jeff jeff1 = new Jeff ();
Jeff1.setname (" ");
For (INT I = 0; I Jeff1.getheyhey (). Add (heyheys. Get (I ));
}
System. Out. println (jeff1.getheyhey ());
System. Out. println ("Before update ");
Sess. saveorupdate (jeff1 );

TX. Commit ();
This is different from the seventh case. When setting the heyheys attribute of jeff1, get and add them one by one. The result is:
Hibernate: select this. ID as id0 _, this. version as version0 _, this. Name as name0 _ from heyhey this where this. ID in (?, ?, ?)
[COM. botwave. CNCD. Business. entity. heyhey @ c24193, Com. botwave. CNCD. Business. entity. heyhey @ 739f3f]
Before update
Hibernate: insert into Jeff (version, name) values (?, ?)
Hibernate: insert into jeff_heyhey (jeffid, heyheyid) values (?, ?)
Data and intermediate table are successfully inserted.

Conjecture: Can the multi-to-many relationship be associated during the vo period, copied to the PO, and then saved?

In the ninth case, the above conjecture is realized. The configuration remains unchanged. The Code is as follows:
Transaction Tx = sess. begintransaction ();
Jeffservice JS = new jeffserviceimpl ();
Heyheyservice HS = new heyheyserviceimpl ();
Integer [] as = new integer [3];
As [0] = new INTEGER (1 );
As [1] = new INTEGER (2 );
As [2] = new INTEGER (3 );
List heyheys = HS. findobjectsbyids ();
Jeffform JF = new jeffform ();
Jeff jeff1 = new Jeff ();
JF. setname ("your VC ");
For (INT I = 0; I JF. getheyhey (). Add (heyheys. Get (I ));
}
System. Out. println (JF. getheyhey ());
Commonutil. copyproperties (jeff1, JF );
System. Out. println ("Before update ");
Sess. saveorupdate (jeff1 );

TX. Commit ();
Analysis: commonutil. copyproperties (jeff1, JF); uses the copyproperties method of the beanutils component. The structure of jeffform is exactly the same as that of Jeff.
Result: hibernate: select this. ID as id0 _, this. version as version0 _, this. Name as name0 _ from heyhey this where this. ID in (?, ?, ?)
[COM. botwave. CNCD. Business. entity. heyhey @ c24193, Com. botwave. CNCD. Business. entity. heyhey @ 739f3f]
Before update
Hibernate: insert into Jeff (version, name) values (?, ?)
Hibernate: insert into jeff_heyhey (jeffid, heyheyid) values (?, ?)
Same success !!

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.