One or one-to-many (@OneToMany)
1, unidirectional one-to-many models
Assume that multiple address information can be obtained through a single customer entity.
For a one-to-many entity relationship, the table structure has two design strategies, foreign key associations and table associations, respectively.
(1) Mapping Policy---foreign KEY Association
In the database table the Customer and table structure address definitions are as follows:
?
123456789101112131415 |
create table customer (
id
int
(20)
not null auto_increment,
name varchar
(100),
primary key
(id)
)
create table address (
id
int
(20)
not null auto_increment,
province
varchar
(50),
city
varchar
(50),
postcode
varchar
(50),
detail
varchar
(50),
customer_id
int
(20),
primary key (id)
)
|
Note that the foreign key is defined on more than one side, which is the Address table.
At this point, the table customer map is the entity Customereo, with the following code:
?
12345678 |
@Entity
@Table
(name=
"customer"
)
public class CustomerEO
implements java.io.Serializable {
@OneToMany
(cascade={ CascadeType.ALL })
@JoinColumn
(name=
"customer_id"
)
private Collection<AddressEO> addresses =
new ArrayList<AddressEO>();
...
}
|
The definition code for note @onetomany is as follows:
?
1234567 |
@Target
({METHOD, FIELD})
@Retention
(RUNTIME)
public @interface OneToMany {
Class targetEntity()
default void
.
class
;
CascadeType[] cascade()
default {};
FetchType fetch()
default LAZY;
String mappedBy()
default ""
;
}
|
There are a few questions to consider when using:
A, the Targetentity property represents the entity type that is associated by default. If a specific type is specified in the collection class, you do not need to use targetentity. Otherwise, you specify Targetentity=addresseo.class.
B, the Mappedby property is used to mark when the entities are bidirectional.
(2) Mapping Policy---Table association
In the Address table above, remove the customer_id field and add a table ref_customer_address, as follows:
?
12345 |
--客户地址关系表 create table ref_customer_address ( customer_id int (20) not null , address_id int (20) not null unique ) |
At this time, the table customer is mapped to a Customereo entity with the following code:
?
1234567891011 |
@Entity
@Table
(name =
"customer"
)
public class CustomerEO
implements java.io.Serializable {
...
@OneToMany
(cascade = { CascadeType.ALL })
@JoinTable
(name=
"ref_customer_address"
,
joinColumns={
@JoinColumn
(name=
"customer_id"
,referencedColumnName=
"id"
)},
inverseJoinColumns={
@JoinColumn
(name=
"address_id"
,referencedColumnName=
"id"
)})
private Collection<AddressEO> addresses =
new ArrayList<AddressEO>();
...
}
|
Table Association @jointable, defined as follows:
?
123456789 |
@Target
({METHOD,FIELD})
public @interface JoinTable {
String name()
default ""
;
String catalog()
default ""
;
String schema()
default ""
;
JoinColumn[] joinColumns()
default {};
JoinColumn[] inverseJoinColumns()
default {};
UniqueConstraint[] uniqueConstraints
default {};
}
|
which
A, the tag is similar to @table, and is used to label the table used for the association.
B, the Name property is the table name that connects two tables. The default table name is: "Table name 1" + "-" + "Table Name 2", the above example default table name is Customer_address.
The C, Joincolumns property represents the foreign key field of the saved association in the table in the save relationship.
The D, Inversejoincolumns property is similar to the Joincolumns property, but it holds another foreign key field that holds the relationship.
(3) Default Association
Add a constraint to two tables at the bottom of the database, as follows:
?
1234567 |
create table customer_address (
customer_id
int
(20)
not null
,
address_id
int
(20)
not null unique
)
alter table customer_address
add constraint fk_ref_customer
foreign key (customer_id)
references customer (id);
alter table customer_address
add constraint fk_ref_address
foreign key (address_id)
references address (id);
|
In this way, you only need to Customereo in the callout @onetomany!
Two, many to one @manytoone
1, one-way many-to-one model.
(1) Foreign Key Association
Configure the Addresseo entity as follows:
?
12345678910 |
@Entity
@Table
(name=
"address"
)
public class AddressEO
implements java.io.Serializable {
@ManyToOne
(cascade = { CascadeType.ALL })
@JoinColumn
(name=
"customer_id"
)
private CustomerEO customer;
// ...
}
|
The @ManyToOne is defined as follows:
?
1234567 |
@Target
({METHOD,FIELD})
@Retention
(RUNTIME)
public @interface ManyToOne {
Class targetEntity()
default void
.
class
;
CascadeType[] cascade()
default {};
FetchType fatch()
default EAGER;
boolean optional()
default true
;
}
|
(2) Default Association
Constraints on the related fields defined by the database script, using @manytoone directly after creating the foreign key
Third, advanced one-to-many and multi-pair mapping
That is, two-way association model, after the two-way association is determined, many of the Addresseo use @manytoone, and the Customereo entity is modified to:
?
123456789 |
@Entity
@Table
(name=
"customer"
)
public class CustomerEO {
@OneToMany
(mappedBy=
"customer"
)
private Collection<AddressEO> addresses =
new ArrayList<AddressEO>();
// ...
}
|
Where the value of the Mappedby property in the @OneToMany tag is the property name of the Customereo entity referenced in the Addresseo entity.
Iv. Many-to-many (@ManyToMany)
And a one-to-many type, not repeating. The @ManyToMany tag is defined as follows:
?
1234567 |
@Target
({METHOD, FIELD})
@Retention
(RUNTIME)
public @interface ManyToMany {
Class targetEntity()
default void
.
class
;
CascadeType[] cascade()
default {};
FetchType fecth()
default LAZY;
String mappedBy()
default ""
;
}
|
V. Finally, talk about the choice of the collection class
The collection classes that you can use in a mapping relationship are collection, set, list, and map, and see below how to choose.
1, the definition uses the interface, initializes the use concrete class.
such as collection can be initialized to ArrayList or HashSet;
Set can be initialized to HashSet;
List can be initialized to ArrayList;
Map can be initialized to HashMap.
2. Selection of collection classes
The collection class is the parent class of Set and list, which can be used when the set or list is not determined;
Objects in the set set cannot be duplicated and are unordered;
Objects in the list collection can have duplicates and can be sorted;
The Map collection is a collection with key and value values.
Lectures @OneToMany, @ManyToOne and @manytomany