Hibernate one-to-multiple connection table bidirectional Association

Source: Internet
Author: User

I. Model Introduction

One person corresponds to multiple addresses ).

2. entity (the getter and setter methods are omitted)

Public class person1ntab_sx {
Private int personid;
Private string name;
Private int age;
Private set addresses = new hashset ();

Public class address1ntab_sx {
Private int addressid;
Private string addressdetail;
Private person1ntab_sx person1ntab_sx;

Iii. Table Model

Mysql> DESC person_1ntab_sx;
+ ---------- + -------------- + ------ + ----- + --------- + ---------------- +
| FIELD | type | null | key | default | extra |
+ ---------- + -------------- + ------ + ----- + --------- + ---------------- +
| Personid | int (11) | no | pri | null | auto_increment |
| Name | varchar (255) | Yes | null |
| Age | int (11) | Yes | null |
+ ---------- + -------------- + ------ + ----- + --------- + ---------------- +

Mysql> DESC address_1ntab_sx;
+ --------------- + -------------- + ------ + ----- + --------- + ---------------- +
| FIELD | type | null | key | default | extra |
+ --------------- + -------------- + ------ + ----- + --------- + ---------------- +
| Addressid | int (11) | no | pri | null | auto_increment |
| Addressdetail | varchar (255) | Yes | null |
+ --------------- + -------------- + ------ + ----- + --------- + ---------------- +

Mysql> DESC join_1ntab_sx;
+ ----------- + --------- + ------ + ----- + --------- + ------- +
| FIELD | type | null | key | default | extra |
+ ----------- + --------- + ------ + ----- + --------- + ------- +
| Addressid | int (11) | no | pri |
| Personid | int (11) | no | pri |
+ ----------- + --------- + ------ + ----- + --------- + ------- +

Iv. generated SQL scripts

/* Formatted on 2007/08/22 52 52 (qp5 v5.50 )*/
Create Table 'address _ 1ntab_sx '(
'Ssid SSID 'int (11) not null auto_increment,
'Address' varchar (255) default null,
Primary Key ('ssid SSID ')
) Engine = InnoDB default charset = GBK;

/* Formatted on 2007/08/22 52 52 (qp5 v5.50 )*/
Create Table 'person _ 1ntab_sx '(
'Personid' int (11) not null auto_increment,
'Name' varchar (255) default null,
'Age' int (11) default null,
Primary Key ('personid ')
) Engine = InnoDB default charset = GBK;

/* Formatted on 2007/08/22 52 52 (qp5 v5.50 )*/
Create Table 'join _ 1ntab_sx '(
'Ssid SSID 'int (11) not null,
'Personid' int (11) not null,
Primary Key ('personid', 'addresside '),
Key 'fk8f869f61f93ddd6 '('personid '),
Key 'fk8f869f61fc0f682a '('ssid SSID '),
Constraint 'fk8f869f61fc0f682a 'foreign key ('ssid SSID') References 'address _ 1ntab_sx '('ssid SSID '),
Constraint 'fk8f869f61f93ddd6 'foreign key ('personid') References 'person _ 1ntab_sx' ('personid ')
) Engine = InnoDB default charset = GBK;

V. ing method


<Hibernate-mapping>
<Class name = "com. lavasoft. SX. _ policn_tab.person1ntab_sx" table = "person_1ntab_sx">
<ID name = "personid">
<Generator class = "Identity"/>
</ID>
<Property name = "name"/>
<Property name = "Age"/>
<! -- Map set attributes and associate them with persistence classes -->
<! -- Table = "join_1ntab_sx" specifies the name of the connection table -->
<Set name = "addresses"
Table = "join_1ntab_sx"
Cascade = "all">
<! -- Column = "personid" specifies the name of the column associated with the current object class in the connection table -->
<Key column = "personid" not-null = "true"/>
<! -- Unique = "true" indicates that the current object class is "1", not "N" -->
<Subtitle-to-duplicate column = "addressid"
Unique = "true"
Class = "com. lavasoft. SX. _ 1_n_tab.address1ntab_sx"/>
</Set>
</Class>
</Hibernate-mapping>

<Hibernate-mapping>
<Class name = "com. lavasoft. SX. _ policn_tab.address1ntab_sx"
Table = "address_1ntab_sx">
<ID name = "addressid">
<Generator class = "Identity"/>
</ID>
<Property name = "addressdetail"/>
<! -- Ing Association attributes. The column attribute specifies the foreign key column name. -->
<Join table = "join_1ntab_sx"
Inverse = "true"
Optional = "true">
<Key column = "addressid"/>
<Role-to-one name = "person1ntab_sx"
Column = "personid"
Cascade = "all"
Not-null = "true"/>
</Join>
</Class>
</Hibernate-mapping>

Vi. Test Methods

Public class test_1ntab_sx {
Public static void main (string [] ARGs ){
Address1ntab_sx Add1 = new address1ntab_sx ();
Address1ntab_sx Add2 = new address1ntab_sx ();
Person1ntab_sx P = new person1ntab_sx ();

Add1.setaddressdetail ("Jing San Road, Zhengzhou City ");
Add2.setaddressdetail ("Suzhou Road, Hefei ");
P. setname ("Wang ");
P. setage (30 );

P. getaddresses (). Add (Add1 );
P. getaddresses (). Add (Add2 );
Add1.setperson1ntab _ SX (P );
Add2.setperson1ntab _ SX (P );

Session session = hibernateutil. getcurrentsession ();
Transaction Tx = session. begintransaction ();
// Session. Save (P );
Session. saveorupdate (Add1 );
Session. saveorupdate (Add2 );
TX. Commit ();
Hibernateutil. closesession ();
}
}

VII. Test Results

1): it is saved normally.
// Session. Save (P );
Session. saveorupdate (Add1 );
Session. saveorupdate (Add2 );

Hibernate: insert into person_1ntab_sx (name, age) values (?, ? )
Hibernate: insert into address_1ntab_sx (addressdetail) values (?)
Hibernate: insert into address_1ntab_sx (addressdetail) values (?)
Hibernate: insert into join_1ntab_sx (personid, addressid) values (?, ? )
Hibernate: insert into join_1ntab_sx (personid, addressid) values (?, ? )


This article is from the "fuyan" blog. For more information, contact the author!

 

This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/songmin3121/archive/2009/05/25/4214685.aspx

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.