Inversion of control in the annotation mode in hibernate

Source: Internet
Author: User

All know hibernate when you configure relationships with XML (One-to-many,many-to-many) You can add control reversal properties Inverse=false (default)

The relationship between the two tables will be given to each other for maintenance.

Inverse=true that the party does not maintain the relationship, has the other side to maintain the relationship

However, it should be noted that the one-to-one relationship is not controlled inversion, but only by the foreign key to maintain the relationship.

This results in the setting of the relationship, only the foreign key party settings, remove the relationship, only from the foreign key side to remove the relationship, delete the record, only the foreign key side, in the deletion of the primary key side (Save,remove,delete)

If there is control reversal, it is more convenient, the two tables can be set up with each other, the relationship between each other, delete, delete the primary key side, the foreign key side of the foreign key will be null () but this is not recommended)

So how to set up control inversion in hiebernate annotation mode (without XML configuration), how to set up correlation between Onetomany, Manytomany?

Of course, inverse has no effect on get/query.

General control reversal (Maintenance relationship) to the multi-party (outside the building) to maintain, students remember the classroom easy, classroom mind student trouble

Principle:

1.one-to-one does not add control inversion, the relationship is maintained by the foreign key

2. The relationship is maintained by multiple parties,

Primary table side does not maintain primary key relationships

<set name= "Employees" inverse= "true" >
<key column= "DepartmentID" ></key>
<one-to-many class= "Employee"/>
</set>

Sub-table side (foreign key side) many to one does not set the inverse property (default is Inverse=false) you write it and you make a mistake. In annotation, many to a cannot write Mappedby (default is controlled by each other)

Foreign key-side maintenance relationship in one-to-one bidirectional association

@JoinColumn (name= "", unique= ".") Set FOREIGN key Inverse=false

@OneToOne (Cascade=cascadetype.all) @JoinColumn (name= "pid", unique=true) public Idcard Getidcard () {return idcard;}

Primary Key Square mappedby= "sub-table foreign Key Object"//inverse=true

Many-to-many one side maintains a foreign key relationship.

So the other side of the relationship is set @manytomany () do not set Mappedby to maintain relationships with each other (but not recommended)

Mappedby ===inverse=true

Many-to-one not write

Inversion of control in the annotation mode in hibernate

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.