Hibernate annotation Properties Mappedby detailed

Source: Internet
Author: User

Mappedby:
1> only onetoone,onetomany,manytomany on the Mappedby attribute, Manytoone does not exist;
The 2>mappedby tag must be defined in the owning party, he points to the owning party;
The meaning of 3>mappedby, should be understood as, the owner can automatically maintain the relationship with the owner, of course, if from the owner, by hand forced to maintain the owner's relationship can also be done;
4>mappedby and joincolumn/jointable always on the side of mutual exclusion, it can be understood that it is because the owner of the association is the owner of the field exists, the owner has already owned the party. Mappedby This party definition joincolumn/jointable always fails, and no corresponding field or table is created.

Here is an example:
Two-way relationship with ID card.
Annotations defined in person:

Comment Code defined in person
    1. @OneToOne (cascade = Cascadetye.all,optional = True)
    2. Public Idcard Getidcard () {
    3. return idcard;
    4. }



Comment Code defined inside the Idcard
    1. @OneToOne (cascade = Cascadetype.all,mappedby = "Idcard", Optional = False)
    2. Public Person Getperson () {
    3. return person;
    4. }



One more Mappedby this method, what does he mean? It indicates that the relationship between the current table and the person is defined in the member of the Idcard, which indicates that this table is the slave table in a pair of relationships, that is, the relationship is maintained in the person table, which is the most important. The person table is the maintainer of the relationship and has the dominant power, which has a foreign key pointing to Idcard.
We can also let the dominance on the Idcard, that is, let him produce a foreign key to the person, which is also possible, but it is better to let someone to maintain the whole relationship, which is more in line with our thinking.
We can also see that the Idcard in person is a comment optional=true, that is, one can not have an identity card, but an identity card is not to be no one, so in Idcard inside the comment person, optional= False, this prevents an empty ID from being logged into the database.

Hibernate annotation Properties Mappedby detailed

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.