Hibernate annoation (Eight association mappings)

Source: Internet
Author: User
Tags final

Onetoone: One-way

1, primary Key association:

Use @onetoone in association

SQL statements: (class code See the same code as before)

Java code

create table A (id integer not null auto_increment, aname varchar(255), b_id integer, primary key (id))
create table B (id integer not null auto_increment, bname varchar(255), primary key (id))
alter table A add index FK41FCD34905 (b_id), add constraint FK41FCD34905 foreign key (b_id) references B (id) 

Can be associated by using @primarykeyjoincolumn

2 bidirectional:

Used in the associated party

@OneToOne (Cascade=cascadetype.all)

@JoinColumn (name= "B")

Used by the associated party

@OneToOne (mappedby= "B")

Final sql:

Java codecreate table A (id integer not null auto_increment, aname varchar(255), b integer, primary key (id))
create table B (id integer not null auto_increment, bname varchar(255), primary key (id))
alter table A add index FK41FCA54B4F (b), add constraint FK41FCA54B4F foreign key (b) references B (id)

If you do not write

@OneToOne (mappedby= "B") will also generate a field in the association

Final code:

Java code

create table A (id integer not null auto_increment, aname varchar(255), i_id integer, primary key (id))
create table B (id integer not null auto_increment, bname varchar(255), a_id integer, primary key (id))
alter table A add index FK41FCD6779E (i_id), add constraint FK41FCD6779E foreign key (i_id) references B (id)
alter table B add index FK42FCD2D4A5 (a_id), add constraint FK42FCD2D4A5 foreign key (a_id) references A (id)

If you do not write @joincolumn (name= "B"), the default is the associated property name + underscore +id

Final sql:

Java code

create table A (id integer not null auto_increment, aname varchar(255), b_id integer, primary key (id))
create table B (id integer not null auto_increment, bname varchar(255), primary key (id))
alter table A add index FK41FCD34905 (b_id), add constraint FK41FCD34905 foreign key (b_id) references B (id)

You can use @joincolumn (referencedcolumnname= "bname") to let the primary association not associate the primary key that is placed by the association

Final SQL

Java code

create table A (id integer not null auto_increment, aname varchar(255), b_bname varchar(255), primary key (id))
create table B (id integer not null auto_increment, bname varchar(255), primary key (id), unique (bname))
alter table A add index FK41E47CD6BD (b_bname), add constraint FK41E47CD6BD foreign key (b_bname) references B (bname)

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.