Hibernate's Mappedby "must read"

Source: Internet
Author: User

"Http://www.cnblogs.com/redcoatjk/p/4236445.html"

One. Mappedby one-way relationships do not need to set this property, bidirectional relationships must be set up to avoid establishing foreign key fields on both sides

Database in the 1-to-many relationship, the association is always maintained by the multi-party is the foreign key built in the multi -party, we in the unilateral object of the @onetomany (mappedby= "")

To maintain relationships by handing over the maintenance of relationships to the properties of multiple objects

For the Mappedby review:

A) The Mappedby attribute is only available on Onetoone,onetomany,manytomany, Manytoone does not exist;

b) The mappedby tag must be defined in the owned side (owned by the party), he points to theowning side (the owning party);

c) The owner of the relationship is responsible for the maintenance of the relationship, establishing a foreign key in the owning party. So use the @joincolumn.

d) Mappedby and joincolumn/jointable are always on the side of mutual exclusion.

Two. The following is my own understanding, has been to hibernate in the maintenance of the relationship is not very familiar with, always do not know what they specifically refer to

The maintenance relationship here, take many to many is the intermediate table, without setting the Cascade, the intermediate table is maintained by the party responsible for maintaining the association relationship

To illustrate:

Game and user Two entity classes, they are many-to-many relationships that have intermediate table T_game-user.

The user is configured with the following:

@ManyToMany (mappedby= "users")

Public list<game> Getgames () {

return games;

}

The game is configured with:

@ManyToMany

@JoinTable (name = "T_game_user",

Joincolumns = {@JoinColumn (name = "game_id", Referencedcolumnname= "GameId")},

Inversejoincolumns = {@JoinColumn (name = "user_id", referencedcolumnname= "id")})

Public list<user> getusers () {

return users;

}

So the game to maintain their association, that is, the intermediate table

form of expression:

1. Because cascade is not configured, add 4 data to Game,user, and then manually add their relationships in the intermediate table

2. Execute the delete user in the program,

Hibernate:delete from T_user where id=?

Hibernate:delete from T_user where id=?

Hibernate:delete from T_user where id=?

Hibernate:delete from T_user where id=?

Deleted user, no impact on intermediate table

3. Perform the delete game in the program

Hibernate:delete from T_game_user where game_id=?

Hibernate:delete from T_game_user where game_id=?

Hibernate:delete from T_game_user where game_id=?

Hibernate:delete from T_game_user where game_id=?

Hibernate:delete from T_game where game_id=?

Hibernate:delete from T_game where game_id=?

Hibernate:delete from T_game where game_id=?

Hibernate:delete from T_game where game_id=?

Has an effect on the intermediate table, which means that the game is maintaining the relationship between them.

Three. Here is a one-to-many maintenance association, (also under the premise of setting Cascade) a one-to-many association is actually simpler, because there is no intermediate table between them, user and group, Their relationship is that the primary key of the one side (group) is the foreign key of the many party (user), which is in the user's table. Because of the presence of foreign keys, the group cannot be deleted until the user is deleted, so the task of maintaining the relationship between them can only be given to the many party (user)

Hibernate's Mappedby "must read"

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.