Joincolumn in one-way relationship

Source: Internet
Author: User

1. One-to-one relationship between person and address:

There is no special annotation in address.

In person, there is a foreign key pointing to address in the database.

You can also add a comment to specify the name of a foreign key column, as shown below:
@ Onetoone (cascade = cascadetype. All, optional = true)
@ Joincolumn (name = "addressid") // comment out the foreign key pointing to another table in this table.
Public Address getaddress (){
Return address;
}
If we do not add one, we can also use it,In JBoss, it will automatically help you generate the class name you point to this class with an underscore and an ID column, that is, the default column name is address_id..
If the primary key is associated, you can use the following annotations:
@ Onetoone (cascade = {cascadetype. All })
@ Primarykeyjoincolumn
Public Address getaddress (){
Return homeaddress;
}
It indicates that the association between two tables is based on the primary keys of the two tables.

---------------------------------------------------------------------

2. One-to-multiple unidirectional relationship between person and phone:

Phone does not contain special comments.

In person:

 
@ Onetoetype (cascade = cascadetype. All)@ Joincolumn (name ="Personid") // Annotate the foreign key that the other table points to the current table.PublicList <phone> getphones (){ReturnPhones ;}

 

We can find @ joincolumn (name ="Personid")
It indicates one-to-many, one refers to the class itself, and multiple refers to this Member, that is, a class can correspond to multiple members.
In one-to-many mode, the maintenance end of the ing relationship is on the one-to-one or two-way side, that is, the phone, because the database needs to behave like this, in addition, you only need to enable the phone to start a foreign key pointing to the person. It is impossible to point to the phone in the person. This is different from a pair. One-to-one can start a foreign key pointing to the other Party on either side. but one-to-many is not working..

Here, @ joincolumn refers to the name of the foreign key column in phone,

It does not refer to the foreign key column name in the table as a comment in one-to-one..Pay special attention to this..

If it is a one-to-many bidirectional relationship, this annotation will be applied to multiple sides, although the annotation is still in the person class, however, the function is to create a foreign key named personid in the phone, because there are many foreign keys pointing to a few.
If you do not add the annotation @ joincolumn (name = "personid"), JBoss will automatically generate an intermediate table for you,

It indicates the relationship between the person and the phone table.. It will do the following:

 
Create Table person_phone (person_id int, phone_id INT); alter table person_phone add constraint person_phone_unique unique (phone_id); alter table person_phone add constraint personrefphone foreign key (person_id) References person (ID ); alter table person_phone add constraint personrefphone2 foreign key (phone_id) References phone (ID );

So we 'd better specifyProgramProduces more definite behaviors,Generally, it is recommended to generate another intermediate table, because this will not affect the structure of the original two tables. It is often used in legacy systems. Sometimes, some tables have been created before, and it is unlikely to change the table structure, therefore, the table in the middle is very important at this time. It can build a more clear and manageable relationship without interfering with the original table.

Therefore, we recommend that you use an intermediate table to establish a one-to-many Association.

Certificate ---------------------------------------------------------------------------------------------------------------------------------------------

3. Multi-to-one unidirectional relationship between person and country:

There is no special annotation in country.

The person annotation is as follows:

 
@ Manytoone@ Joincolumn (name ="CountryID")PublicCountry getcountry (){ReturnCountry ;}In a one-to-one relationship, the maintenance end of the relationship is on the many side, and the many sides are the main control side, with a foreign key pointing to the other side.

Because the master side is person, the foreign key is also built on the person, because it is multiple sides. Of course, we can also save @ joincolumn here. What will happen then? Will it generate an intermediate table like one-to-many unidirectional? The truth is, no,If @ joincolumn is removed here, a column in the person table will be generated pointing

The foreign key of country, which is different from one-to-many.In one-to-many unidirectional mode, if we do not add the @ joincolumn comment in person, JBoss will generate an intermediate table for us. This table will have a column pointing to the person primary key, A column points to the phone primary key. So we should not save some things for the sake of program behaviors.
In fact, many-to-one is a little one-to-one. On the master side, from the perspective of person, it corresponds to a country, however, this country is shared by many persons, but one-to-one does not have this restriction.

------------------------------------------------------------------

4. Multi-to-multiple unidirectional relationship between person and project:

Project does not have special annotations.

Person:

 
@ Manytoyun
 
PublicList <project> getprojects (){ReturnProjects ;}

It needs to set an intermediate table to maintain the relationship. In the database, it is only different from many-to-many bidirectional, except in the programming logic.

 
// Similar to this: @ jointable (name = "personandflight", joincolumns ={@ joincolumn (name = "personid ")},
// Inversejoincolumns ={@ joincolumn (name = "flightid ")})

In fact, this declaration is not necessary. When we do not use @ jointable to declare it, JBoss will also automatically generate a table for connection for us,

By default, the table name on the master side is underlined with "_" and the table name on the reverse side is added.

Similar

 
@ Manytomany (cascade = cascadetype. All) @ jointable (name ="Personandflight",
 
Joincolumns = {@ joincolumn (name ="Personid")},
 
Inversejoincolumns = {@ joincolumn (name ="Flightid")})PublicList <flight> getflights (){ReturnFlights ;}
 
 
 
-------------------------------------------------------------------------
 
There is no mappedby in a unidirectional relationship, and the primary prosecution is equivalent to having a foreign key pointing to the other party.
1. One-to-one and multiple-to-one @ joincolumn annotations are all in the "primary prosecution" and are the foreign key names that direct the table to the External table.
 
2. The one-to-many @ joincolumn annotation indicates the foreign key name pointing to the table in the External table in the "under prosecution", that is, the one side.
 
3. In many-to-many scenarios, joincolumns writes the foreign key name of the table in the middle table,
 
Inversejoincolumns writes the foreign key name of another table in the middle table.

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.