• One-way Association # annotation @onetoone is defined as follows:
@Target ({method, FIELD}) @Retention (RUNTIME)
Public @interface Onetoone {
Class targetentity () default void.class;
Cascadetype[] Cascade () default {};
Fetchtype fetch () default EAGER;
Boolean optional () default true;
String mappedby () default "";
}
#
The Targetentity property represents the default associated entity type that defaults to the entity class of the current callout;
#
Cascade PropertyRepresents a cascading style type for an entity that is associated with this entity on a one-to-one scale. The strategy of the cascading style when manipulating an entity.
Description: When defining a relationship, it often involves defining the cascade (cascading processing) attribute, fearing a negative impact.
• No definition, no effect on relational tables
· Cascadetype.persist (Cascade New)
· Cascadetype.remove (Cascade Delete)
· Cascadetype.refresh (Cascade Refresh)
· Cascadetype.merge (cascade Update) Select one or more.
• Another option is to use Cascadetype.all to indicate that all four items are selected
#
Fetch Propertyis the loading mode of the entity, there are two kinds: lazy and eager.
#
The optional property indicates whether the associated entity can have a null value. The default is true to indicate that a null value can exist. If False, use the @joincolumn tag together. #
mappedby attribute is used when associating entities in bidirectional, annotations are in entities that do not save relationships。
Association specified column (@JoinColumn) # @JoinColumn
used to annotate fields in a table,
Unlike @column, it is the field that holds the relationship between the table and the table.;
#
The Name property is used to mark the names of the corresponding fields in the table。 If you do not set the value of name, by default, the value rule for name is as follows:
name= The name of the table associated with + "_" + the field name of the primary key of the associated table # By default, the primary key of the associated entity is generally used as a foreign key. If you do not want to use a primary key as a foreign key, you need to set the Referencedcolumnname property, such as:
@JoinColumn (name= "address_id", Referencedcolumnname= "ref_id")
# @JoinColumn can be used in conjunction with @onetoone, @ManyToOne, or @manytomany tags.
One-to-one One-way Association example:
• Customer entity class Customereo:
@Entity
@Table (name= "Customer")//Main Table
public class Customereo Implement serializable{
@Id
@GeneratedValue (Stragegy=generationtype=auto)
private int id;
@OneToOne (Casade={cascadetypetype.all}) @JoinColumn (name= "address_id")/The associated table is the Address table, and its primary key is ID Private Addresseo address;
....
}
• Address Book class Addresseo:
@Entity
@Table (name= "
Address")//from the table
public class Addresseo Implement serializable{
@Id
@GeneratedValue (Stragegy=generationtype=auto)
private int id;
...//In a single association, you do not need to reference the primary table class in the corresponding class from the table
}
·Bidirectional Association (bidirectional) As in the preceding example,
Modify the Addresseo as follows:
• Address Book class Addresseo:
@Entity
@Table (name= "Address")
public class Addresseo Implement serializable{
@Id
@GeneratedValue (Stragegy=generationtype=auto)
private int id;
*
all of the above examples and relationships have the following table structure:
TABLE customer (id int, name varcher,
address_id int, PRIMARY KEY ID)//One more address_id attribute in the customer
TABLE address (id int, address_name varcher, ZipCode varcher, City varcher, PRIMARY KEY ID)
• Primary Key Association (@PrimaryKeyJoinColumn)//I cannot form a primary key association. #
use @primarykeyjoincolumn annotations directly in the associated two entities;
# in the above table structure
need to remove the address_id field from the Customer table;
Example:
• Customer entity class Customereo:
@Entity
@Table (name= "Customer")
public class Customereo Implement serializable{
@Id
@GeneratedValue (Stragegy=generationtype=auto)
private int id;
@OneToOne (Casade={cascadetypetype.all}) @PrimaryKeyJoinColumn Private Addresseo address;
....
}
• Address Book class Addresseo:
@Entity
@Table (name= "Address")
public class Addresseo Implement serializable{
@Id
@GeneratedValue (Stragegy=generationtype=auto)
&
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.