Hibernate Association Mappings

Source: Internet
Author: User

Take a simple two-class example:
User (int id, String name)
Group (int id, String name)

Relationship model with no associated relationship:
T_user (id int PK, name varchar)
T_group (id int PK, name varchar)

one, many-to-one, and a-to-many association mappings (multiple users have the same group)

These relational mappings have the same relationship model:
T_user (id int PK, name varchar, gid int fk->t_group (ID))
T_group (id int PK, name varchar)

1, many-to-one One-way association

Entity Model:
Bean. User (int id, String name, Group Group)
Bean. Group (int id, String name)

<!--bean/user.hbm.xml-->
<!--bean/group.hbm.xml-->
2, one-to-many-way association (almost unused)

Entity Model:
Bean. User (int id, String name)
Bean. Group (int id, String name, Set users)

<!--bean/user.hbm.xml-->
<!--bean/group.hbm.xml-->

Adding a collection to a group can also use list (<list>), note that you cannot specify that a type is a specific hashset or ArrayList, only an interface set or list.
The collection label can use the Order-by property to specify the sort:

<set name= "Users" order-by= "id desc" >
3. Bidirectional correlation

Entity Model:
Bean. User (int id, String name, Group Group)
Bean. Group (int id, String name, Set users)

<!--bean/user.hbm.xml-->
<!--bean/group.hbm.xml-->

In a bidirectional association, adding "Inverse=true" to <set> can reverse the maintenance relationship: Hibernate will discard the maintenance from one end. This means that user and group relationships must be maintained by user, and Hibernate will not maintain this relationship when the group is operating.

<set name= "Users" inverse= "true" >

Example of Operation Group:

            Session.begintransaction ();            User user = new user ();            User.setname ("Zhang San");                        Group Group = new Group ();            Group.setname ("admin");            Group.setusers (New HashSet ());            Group.getusers (). Add (user);                        Session.save (user);            Session.save (group);            Session.gettransaction (). commit ();

When inverse= "True" is not configured, hibernate outputs the Add user and group, and updates the user's statement:

Hibernate:insert into T_user (name, GID) VALUES (?,?) Hibernate:insert into T_group (name) VALUES (?) Hibernate:update T_user set gid=? where id=?
With inverse= "True" configured, Hibernate simply outputs the statements that add user and group, and does not update the user, discarding the maintenance of the relationship:
Hibernate:insert into T_user (name, GID) VALUES (?,?) Hibernate:insert into T_group (name) VALUES (?)
The relationship should be maintained from the user side at this time:
            Session.begintransaction ();            Group Group = new Group ();            Group.setname ("admin");                        User user = new user ();            User.setname ("Zhang San");            User.setgroup (group);                        Session.save (group);            Session.save (user);            Session.gettransaction (). commit ();

Because the foreign key is listed in the T_user table, it is necessary to operate multiple tables from the group side, so it is more reasonable and more efficient to maintain relationships from the user side. The above code outputs two SQL statements, while inserting the data also maintains the relationship:

Hibernate:insert into T_group (name) VALUES (?) Hibernate:insert into T_user (name, GID) VALUES (?,?)

two or one-to-one affinity mapping (one group per user)

According to the mapping method, it can be divided into Primary Key association map and unique Foreign Key association mapping . The primary key association is to maintain the primary key consistency of the two tables and, if necessary, to add a foreign key constraint on the primary key; The unique foreign Key association is similar to a many-to-one association, which joins a foreign key column for a table, but a one-to-a association adds a unique constraint to the foreign key at the same time.

1. Primary Key correlation mapping

The relationship model generated by the primary Key association:
T_user (id int PK fk->t_group (ID), name varchar)
T_group (id int PK, name varchar)

1.1. Primary key one-way Association

Entity Model:
Bean. User (int id, String name, Group Group)
Bean. Group (int id, String name)

<!--bean/user.hbm.xml-->
<!--bean/group.hbm.xml-->

The primary key association is maintained by hibernate and is not dependent on the database. If you need to generate a foreign key constraint on the database side, you can use constrained:

<one-to-one name= "group" constrained= "true"/>

1.2. Primary key bidirectional correlation

Entity Model:
Bean. User (int id, String name, Group Group)
Bean. Group (int id, String name, user user)

<!--bean/user.hbm.xml-->
<!--bean/group.hbm.xml-->

2. Unique FOREIGN key correlation mapping

Unique foreign Key association-generated relational model:
T_user (id int PK, name varchar, gid int fk->t_group (ID))
T_group (id int PK, name varchar)

2.1. Unique foreign key one-way Association

Entity Model:
Bean. User (int id, String name, Group Group)
Bean. Group (int id, String name)

<!--bean/user.hbm.xml-->
<!--bean/group.hbm.xml-->

2.2. Unique FOREIGN key bidirectional correlation

Entity Model:
Bean. User (int id, String name, Group Group)
Bean. Group (int id, String name, user user)

<!--bean/user.hbm.xml-->
<!--bean/group.hbm.xml-->
three, many-to-many association mappings (each user has multiple groups, each group also has multiple users)

A many-to-many association mapping relationship uses an intermediate table representation. Hibernate automatically generates composite primary keys and foreign KEY constraints when the relational model is exported.

Relational Model:
T_user (id int PK, name varchar)
T_group (id int PK, name varchar)
T_user_group (userid int fk->t_user (id), groupid int fk->t_group (id), PK (userid, GroupID) )

1, many-to-many one-way association

Entity Model:
Bean. User (int id, String name, Set groups)
Bean. Group (int id, String name)

<!--bean/user.hbm.xml-->
<!--bean/group.hbm.xml-->
1, many-to-many one-way association

Entity Model:
Bean. User (int id, String name, Set groups)
Bean. Group (int id, String name, Set users)

<!--bean/user.hbm.xml-->
<!--bean/group.hbm.xml-->

A many-to-many bidirectional association can also set inverse= "true" in <set> on one end of the relationship that you do not want to maintain, but there must be a side that can be maintained, that is, you can only set a single.

Hibernate Association Mappings

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.