In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article will explain in detail about the configuration of SpringDataJPA entity class relationship mapping. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
SpringDataJPA//FetchType.LAZY: lazy loading. Attributes that define lazy loading are not immediately loaded from the database when an entity is loaded. / / FetchType.EAGER: emergency loading, when an entity is loaded, attributes that define rapid loading are immediately loaded from the database. / / cascade = CascadeType.ALL indicates that the associated operation is performed in all cases That is, save-update and delete@JsonBackReference / / solve the circular reference problem @ JsonIgnoreProperties (value = "order") / / solve the circular reference problem, order content is large, do not load / / two-way mapping to json infinite recursion problem need to be annotated to deal with the recommendation to use the second annotation value value for the other table to map the variable value 1. One-way one-to-one mapping
One side
/ / teacher @ Id @ GeneratedValue (strategy=GenerationType.IDENTITY) private int id; private String name; private long salary; @ OneToOne @ JoinColumn (name= "PSPACE_ID") private ParkingSpace parkingSpace
One side
/ / parking lot @ Id @ GeneratedValue (strategy=GenerationType.IDENTITY) private int id; private int lot; private String location
Summary
When adding, you need to add the @ OneToOne first, and delete it as well.
two。 Bidirectional one-to-one mapping
One side
/ person @ Id @ GeneratedValue (strategy=GenerationType.IDENTITY) private long id; private String name; @ OneToOne @ JoinColumn (name= "DEPT_ID") @ JsonIgnoreProperties (value = "person") private Department department
One side
/ / Department @ Id @ GeneratedValue (strategy=GenerationType.IDENTITY) private long id; private String name; @ OneToOne (mappedBy= "department") @ JsonIgnoreProperties (value = "department") private Person person
Summary
@ JsonBackReference / / solve the problem of circular reference @ JsonIgnoreProperties (value = "order") / / solve the problem of circular reference. The content of order is large. If you do not load / / two-way mapping, the problem of infinite recursion to json needs to be annotated to deal with the variable value 3 that is recommended to use the value value of the second annotation to map the other table. One-way one-to-many mapping
One side
@ Id @ GeneratedValue private Long id; @ Column (length = 32) private String name; @ OneToMany (cascade = CascadeType.ALL, fetch = FetchType.LAZY) / / cascaded save, update, delete, refresh; delayed loading @ JoinColumn (name = "author_id") / / add a foreign key column to the book table to achieve an one-to-many one-way association private Set books = new HashSet ()
Multi-party
/ / in the case of one-way one-to-many, multiple parties do not need to add any associated identity @ Id @ GeneratedValue private Long id; @ Column (length=32) private String name
Summary
Because it is an one-way association, multiple parties do not need to add any association identity, only need to add in one party.
4. Bidirectional one-to-many mapping
One side
@ Id @ GeneratedValue (strategy = GenerationType.IDENTITY) private Integer id; / / key private String name; / / name / / description customer can have multiple orders @ OneToMany (mappedBy = "customer", cascade = CascadeType.ALL) @ JsonBackReference private Set order = new HashSet ()
Multi-party
@ Id @ GeneratedValue (strategy = GenerationType.IDENTITY) private Integer id; private Double money; private String receiverInfo; / / ship to address / / order is associated with the customer @ ManyToOne (cascade = CascadeType.ALL) @ JoinColumn (name = "c_id") / / specify the foreign key column @ JsonIgnoreProperties (value = "order") / / solve the circular reference problem. Order content is large and private Customer customer is not loaded. / / describe that the order belongs to a customer
Summary
@ JsonBackReference / / solve the problem of circular reference @ JsonIgnoreProperties (value = "order") / / solve the problem of circular reference. The content of order is large. If you do not load / / two-way mapping, the problem of infinite recursion to json needs to be annotated to deal with the variable value 5 that is recommended to use the value value of the second annotation to map the other table. Unidirectional many-to-one mapping
Multi-party
@ GeneratedValue @ Id private Integer id; @ Column (name= "ORDER_NAME") private String orderName; / / Mapping one-way nmur1 associations / / using @ ManyToOne to map many-to-one relationships / / using @ JoinColumn to map foreign keys / / you can use @ ManyToOne's fetch attribute to modify the default association attribute loading policy @ JoinColumn (name= "CUSTOMER_ID") / / in addition, the primary key of one party @ ManyToOne (fetch=FetchType.EAGER) private Customer customer
One side
/ / in the case of one-way many-to-one, one party does not need to add any associated logo @ GeneratedValue (strategy=GenerationType.AUTO) @ Id private Integer id; private String lastName; private String email; private int age; private Date createdTime; private Date birth
Summary
Save many-to-one, it is recommended to save one end of 1, and then save the end of n, so that there are no extra update statements.
6. Bidirectional many-to-one mapping
Multi-party
@ GeneratedValue @ Id private Integer id; @ Column (name= "ORDER_NAME") private String orderName / / use @ ManyToOne to map many-to-one associations / / use @ JoinColumn to map foreign keys / / you can use the fetch attribute of @ ManyToOne to modify the loading policy of default association attributes @ JoinColumn (name= "CUSTOMER_ID") @ ManyToOne (fetch=FetchType.EAGER) @ JsonIgnoreProperties (value = {"customer", "orders"}) private Customer customer.
One side
@ GeneratedValue (strategy=GenerationType.AUTO) @ Id private Integer id; @ Column (name= "LAST_NAME", length=50,nullable=false) private String lastName; private String email; private int age; / / defines the date format @ Temporal (TemporalType.TIMESTAMP) private Date createdTime; @ Temporal (TemporalType.DATE) private Date birth / / Mapping the association of one-way 1OneToMany / / using @ OneToMany to map the association of one-way 1murn / / using @ JoinColumn to map the name of the foreign key / / you can use the fetch attribute of @ OneToMany to modify the loading policy / / you can use the cascade attribute of @ OneToMany to modify the default deletion policy / / @ JoinColumn (name= "CUSTOMER_ID") / / one end of one Abandon and maintain the association @ OneToMany (fetch=FetchType.EAGER Cascade= {CascadeType.REMOVE}, mappedBy= "customer") @ JsonIgnoreProperties (value = "orders") private Set orders = new HashSet ()
Summary
1. The @ JsonIgnoreProperties annotation must be used to intercept infinite recursion when transferring to json.
two。 If it is a two-way association of 1MFN, when saving is performed:
(1) if you save one end of n and then one end of 1, by default, there will be 2n more UPDATE statements
(2) if one end of 1 is saved first, n more UPDATE statements will be added.
(3) therefore, in the two-way 1murn association relationship, it is recommended to use one side of n to maintain the association relationship, while the other party of 1 does not maintain the related relationship, which will effectively reduce the SQL statement (that is, the table at one end of N uses a foreign key to associate one end of 1, and the foreign key corresponds to the primary key of one end of 1).
Note: if you use the mappedBy attribute in @ OneToMany on one side of 1, the @ JoinColumn attribute can no longer be used on the @ OneToMany side.
7. Unidirectional many-to-many mapping
Multi-party
@ Id @ GeneratedValue private Long id; private String name / / indicates many-to-many @ ManyToMany (cascade = CascadeType.ALL) / / the table name of the intermediate table @ JoinTable (name = "t_user_role", joinColumns = {@ JoinColumn (name = "user_id")}, and / / the reverse side of the user table is role inverseJoinColumns = {@ JoinColumn (name = "role_id")} private Set roles = new HashSet ()
Multi-party
@ Id @ GeneratedValue private Long id; private String name
Summary
Use @ JoinTable to map intermediate tables
1. Name points to the name of the intermediate table
2. JoinColumns maps the foreign keys of the table in which the current class resides in the middle table
2.1 name specifies the column name of the foreign key column
2.2 referencedColumnName specifies which column of the current table is associated with the foreign key column
3. InverseJoinColumns maps the foreign key of the intermediate table where the associated class resides
8. Bidirectional many-to-many mapping
Multi-party
@ Id @ GeneratedValue private Long id; private String name; / / means many to many @ ManyToMany (fetch = FetchType.LAZY,cascade = CascadeType.REMOVE) @ JoinTable (name= "t_user_role", joinColumns= {@ JoinColumn (name= "user_id")}, inverseJoinColumns = {@ JoinColumn (name= "role_id")}) @ JsonIgnoreProperties (value = "users") private Set roles = new HashSet ()
Multi-party
Id @ GeneratedValue private Long id; private String name; @ ManyToMany (fetch = FetchType.LAZY,cascade = CascadeType.ALL) @ JoinTable (name= "t_user_role", joinColumns= {@ JoinColumn (name= "role_id")}, inverseJoinColumns = {@ JoinColumn (name= "user_id")}) @ JsonIgnoreProperties (value = "roles") private Set users = new HashSet ()
Summary
Use @ JoinTable to map intermediate tables
1. Name points to the name of the intermediate table
2. JoinColumns maps the foreign keys of the table in which the current class resides in the middle table
2.1 name specifies the column name of the foreign key column
2.2 referencedColumnName specifies which column of the current table is associated with the foreign key column
3. InverseJoinColumns maps the foreign key of the intermediate table where the associated class resides
@ JsonBackReference / / solve the circular reference problem @ JsonIgnoreProperties (value = "order") / / solve the circular reference problem. The order content is large and does not load.
The problem of infinitely recursive conversion to json in bidirectional mapping needs to be annotated to deal with the variable values that are recommended to be mapped using the second annotation value as the opposite table.
This is the end of this article on "what are the configuration methods of SpringDataJPA entity class mapping". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it out for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.