Hibernate Association ing and hibernate Association

Source: Internet
Author: User

Hibernate Association ing and hibernate Association
This document serves as a learning note for the hibernate course for Ma soldiers at Beijing shangxue.com.
Hibernate ing is mainly divided into one-to-one, one-to-many, one-to-one, and many-to-many. At the same time, it must be one-way and two-way.
OK. Don't worry about the name. Let's look at the example.


One-to-one husband is an entity, and his wife is also an entity.
A husband has only one wife and only one wife.
The above relationship is called one-to-one.
What is unidirectional.
Check the Code:

Package com. bjsxt. hibernate; @ Entitypublic class Husband {private int id; private String name; private Wife wife; @ Id @ GeneratedValue public int getId () {return id ;} @ OneToOne @ JoinColumn (name = "wifeId") public Wife getWife () {return wife ;}// omit get set} package com. bjsxt. hibernate; @ Entitypublic class Wife {private int id; private String name; @ Id @ GeneratedValue public int getId () {return id;} // omit get set}


Look at the code above. My husband has a reference from his wife, but my wife does not.
In other words, at the code level, we can get his wife based on her husband, but we cannot get her husband based on her wife. (This is unidirectional)

Let's look at the code it runs.
package com.bjsxt.hibernate;import org.hibernate.cfg.AnnotationConfiguration;import org.hibernate.tool.hbm2ddl.SchemaExport;public class Test {    public static void main(String[] args) {        new SchemaExport(new AnnotationConfiguration().configure()).create(false, true);        }}
In the hibernate configuration file
Add
<property name="hbm2ddl.auto">update</property>
Result
19:41:04,229 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 -    create table Husband (        id integer not null auto_increment,        name varchar(255),        wifeId integer,        primary key (id)    )19:41:04,549 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 -    create table Wife (        id integer not null auto_increment,        name varchar(255),        primary key (id)    )19:41:04,880 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 -    alter table Husband        add index FKAEEA401B109E78ED (wifeId),        add constraint FKAEEA401B109E78ED        foreign key (wifeId)        references Wife (id)

Add a field wifeId to husband and then use it as a foreign key to reference the id field of the wife table.
As you can see, the @ JoinColumn (name = "wifeId") meta tag indicates the name of the associated field in the husb and table.
What if there is no such meta tag?
The associated field is: wife_id. That is, tableName_primarykey.
If you write @ onetoone to the Wife (there is no onotoone tag in husband), we can get husband from the wife (at the code level ).



For one-to-one bi- ing, if I want to get the wife from husband, I also want to get the husband from wife. What should we do?
Copy the labels in husb and.
package com.bjsxt.hibernate;@Entitypublic class Wife {    private int id;    private String name;    private Husband husband;        @Id    @GeneratedValue    public int getId() {        return id;    }        @OneToOne    @JoinColumn(name="husbandId")    public Husband getHusband() {        return husband;    }}

OK. In the above Code, wife can also get husb and.
Let's look at the SQL statement generated by hibernate.
create table Husband (        id integer not null auto_increment,        name varchar(255),        wifeId integer,        primary key (id)    )19:53:20,487 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 -    create table Wife (        id integer not null auto_increment,        name varchar(255),        husbandId integer,        primary key (id)    )19:53:20,824 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 -    alter table Husband        add index FKAEEA401B109E78ED (wifeId),        add constraint FKAEEA401B109E78ED        foreign key (wifeId)        references Wife (id)19:53:21,421 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 -    alter table Wife        add index FK292331CE01A6E1 (husbandId),        add constraint FK292331CE01A6E1        foreign key (husbandId)        references Husband (id)
You can see that there is a foreign key in the wife and a foreign key in the husb and!
This is not redundant.
Change the wife class to the following format:
    @OneToOne(mappedBy="wife")    public Husband getHusband() {        return husband;    }
This mappedBy means that I (the Wife class) and the husband class are one-to-one, but the associated Foreign keys are controlled by the getWife method of the husband class.
Run it.
20:03:18,611 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 -    create table Husband (        id integer not null auto_increment,        name varchar(255),        wifeId integer,        primary key (id)    )20:03:18,618 ERROR org.hibernate.tool.hbm2ddl.SchemaExport:348 - Unsuccessful: create table Husband (id integer not null auto_increment, name varchar(255), wifeId integer, primary key (id))20:03:18,618 ERROR org.hibernate.tool.hbm2ddl.SchemaExport:349 - Table 'husband' already exists20:03:18,619 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 -    create table Wife (        id integer not null auto_increment,        name varchar(255),        primary key (id)    )20:03:18,933 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 -    alter table Husband        add index FKAEEA401B109E78ED (wifeId),        add constraint FKAEEA401B109E78ED        foreign key (wifeId)        references Wife (id)
In the database, there is no foreign key in the wife table.
However, at the code level, we can still obtain husb and from the Wife class.

Note the above @ OneToOne (mappedBy = "wife ")
Only two-way association is required. It is best to write mappedBy on both tables. Only one foreign key is required for the two tables.



Many-to-one, one-way Association everyone has many dreams, but each dream only belongs to a specific person.
First, no matter how the java code is implemented, in the database.
The two tables above, one is person, which is id, name
Then there is the dream table, an id and a description.
Which table are their foreign keys in?
Put it in dream. In other words, there is a field in the dream table called personId.
There is no need to explain the cause.
All one-to-many, many-to-one relationships. In the database, foreign keys are placed on multiple sides.
Why. Think for yourself.


View code
Package com. bjsxt. hibernate; @ Entitypublic class Dream {private int id; private String description; private Person person; @ Id @ GeneratedValue public int getId () {return id ;} @ ManyToOne @ JoinColumn (name = "personId") public Person getPerson () {return person;} // omit get set} package com. bjsxt. hibernate; @ Entitypublic class Person {private int id; private String name; @ Id @ GeneratedValue public int getId () {return id;} // omit get set}
From the code level, we can see "many-to-one"
It is the person that I can obtain from multiple parties (dream ).
Because it is one-way, you cannot get all his dream from person.
OK. The generated table contains the personid in dream.
20:20:21,970 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 -    create table Dream (        id integer not null auto_increment,        description varchar(255),        personId integer,        primary key (id)    )20:20:22,264 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 -    create table Person (        id integer not null auto_increment,        name varchar(255),        primary key (id)    )20:20:22,765 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 -    alter table Dream        add index FK3F397E3C1409475 (personId),        add constraint FK3F397E3C1409475        foreign key (personId)        references Person (id)



An example of one-to-many one-way association or a person-to-dream relationship.
What should I do if I want to get all his dreams through a person at the code level?
First, delete the reference of person in the dream class.
package com.bjsxt.hibernate;@Entitypublic class Person {    private int id;    private String name;    private Set<Dream> dreams=new HashSet<>();        @Id    @GeneratedValue    public int getId() {        return id;    }    @OneToMany    @JoinColumn(name="psrsonId")    public Set<Dream> getDreams() {        return dreams;    }    }


OK.
The table creation statement is the same as the preceding statement.
Why is the set class installed with dream? No list rows, no map rows?
Yes.
But remember, the records in the database are unordered and not equal.
Which one do you use?


@ JoinColumn (name = "abc ")
Both Add a foreign key named abc to the other.


The following example shows one-to-multiple bidirectional Association (Multiple-to-one bidirectional Association). I want to get the person from dream, he also wants to get all his dream from person.
Let's look at the Code:
package com.bjsxt.hibernate;@Entitypublic class Dream {    private int id;    private String description;        private Person person;        @Id    @GeneratedValue    public int getId() {        return id;    }    public void setId(int id) {        this.id = id;    }    @ManyToOne    @JoinColumn(name="personId")    public Person getPerson() {        return person;    }}package com.bjsxt.hibernate;@Entitypublic class Person {    private int id;    private String name;    private Set<Dream> dreams=new HashSet<>();    @Id    @GeneratedValue    public int getId() {        return id;    }    @OneToMany(mappedBy="person")    public Set<Dream> getDreams() {        return dreams;    }}
OK. In the code, we can get the person through dream or get the dream through person.



The example of multi-to-many one-way Association is very simple. One teacher can teach multiple students, and one student can be taught by multiple teachers.
One way is to say that either the teacher can "know" his students or the students can know their teachers.
Let's first look at the code that the teacher can "know" his students.

Package com. bjsxt. hibernate; @ Entitypublic class Student {private int id; private String name; @ Id @ GeneratedValue public int getId () {return id ;}} OK, no reference from the teacher in the Student. Package com. bjsxt. hibernate; @ Entitypublic class Teacher {private int id; private String name; private Set <Student> students = new HashSet <Student> (); @ Id @ GeneratedValue public int getId () {return id ;}@ manytoable @ JoinTable (name = "t_s", joinColumns ={@ JoinColumn (name = "teacher_id ")}, inverseJoinColumns ={@ JoinColumn (name = "student_id")}) public Set <Student> getStudents () {return students ;}}
Result:
21:10:35,854 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 -    create table Student (        id integer not null auto_increment,        name varchar(255),        primary key (id)    )21:10:36,192 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 -    create table Teacher (        id integer not null auto_increment,        name varchar(255),        primary key (id)    )21:10:36,643 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 -    create table t_s (        teacher_id integer not null,        student_id integer not null,        primary key (teacher_id, student_id)    )21:10:36,947 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 -    alter table t_s        add index FK1BF68BF77BA8A (teacher_id),        add constraint FK1BF68BF77BA8A        foreign key (teacher_id)        references Teacher (id)21:10:37,588 DEBUG org.hibernate.tool.hbm2ddl.SchemaExport:377 -    alter table t_s        add index FK1BF68AEDC6FEA (student_id),        add constraint FK1BF68AEDC6FEA        foreign key (student_id)        references Student (id)21:10:38,263  INFO org.hibernate.tool.hbm2ddl.SchemaExport:268 - schema export complete

Let's take a look at the generated t_s table and the field names in it to understand the role of @ JoinTable.
It specifies the table name and field of the third table in manytomany.
JoinColumns refers to the field name pointing to your own class.
InverseJoinColumns inverse indicates the reverse direction. Therefore, this label is used to execute fields of another class.
What would it look like if there is no @ JoinTable label? Try it by yourself.
In addition, the above Code is from the teacher to the student. You can try again from the student to the teacher.


Multi-to-many bidirectional Association

In the preceding example
Change student and add the reference of teacher to it.

Package com. bjsxt. hibernate; @ Entitypublic class Student {private int id; private String name; private Set <Teacher> teachers = new HashSet <> (); @ Id @ GeneratedValue public int getId () {return id;} // Add mappedBy @ ManyToMany (mappedBy = "students") public Set <Teacher> getTeachers () {return teachers ;}}


Glt likes dlf

Dlf likes glt

OneToOne



Related Article

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.