Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use annotations to implement one-to-many relationships in Spring

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces how to use annotations to realize one-to-many relationships in Spring. The introduction in this article is very detailed and has certain reference value. Interested friends must read it!

One-to-many relationships in Spring can be implemented with the @OnetoMany annotation

Then in practice, what if you want to sort dependent objects conditionally? It can be implemented with the annotation @OrderBy.

Let's look at my example.

A Product object, which has a OnetoMany relationship corresponding to multiple pictures, and then my picture in the background to support sorting, so I added an ordernum int type field in the Picture class to sort marks

@OneToMany(cascade = CascadeType.ALL,mappedBy = "product",fetch = FetchType.EAGER)private List pictures;

And how do I sort the images the way I want them when I'm taking values?

Use @OrderBy

@Target({METHOD, FIELD})@Retention(RUNTIME)public @interface OrderBy { /** * An orderby_list. Specified as follows: * * * orderby_list::= orderby_item [,orderby_item]* * orderby_item::= [property_or_field_name] [ASC | DESC] * * *

If ASC or DESC is not specified, * ASC (ascending order) is assumed. * *

If the ordering element is not specified, ordering by * the primary key of the associated entity is assumed. */ String value() default "";}

From the definition of this annotation, you can see that its parameter is a String

For example, in my project, the ascending order of ordernum of pictures is @OrderBy("ordernum ASC");ordernum is the sorting method corresponding to the field name ASC, separated by spaces

Of course, it also supports multiple conditions for sorting, for example, I want to sort by ordernum and id, that is @OrderBy("ordernum ASC,id ASC")

Last my code @OneToMany(cascade = CascadeType.ALL, mapedBy = "product",fetch = FetchType.EAGER)@OrderBy("ordernum ASC")private List pictures;Entity One-to-Many sort settings/** * Comment entity class, corresponding to TCOMMENT table. * @author William */@Entity@Table(name = "TCOMMENT")public class Comment { /** * The ID field is the primary key, and the primary key generation policy is Automatic. */ @Id @GeneratedValue private Long id; private String nickname; private String content; private Integer note; /** * Publication date, corresponding to the posted_date field, field type is TIMESTAMP. */ @Column(name = "posted_date") @Temporal(TemporalType.TIMESTAMP) private Date postedDate; // Constructors, setters, getters}/** * News entity class, corresponding to News table. * @author William */@Entity@NamedQuery(name = "findAllNews", query = "SELECT n FROM News n")public class News { /** * The ID field is the primary key, and the primary key generation policy is Automatic. */ @Id @GeneratedValue private Long id; /** * content News content, this field is required. */ @Column(nullable = false) private String content; /** * Comments, make one-to-many associations with them. * FetchType.EAGER Get news immediately relevant comments * CascadeType.ALL supports all cascading operations. * OrderBy When getting a list of comments, sort them in descending order of postedDate. */ @OneToMany (fetch = FetchType.EAGER, cascade = {CascadeType.ALL}) @JoinTable (name = "NEWS_COMMENT", joinColumns = @JoinColumn(name = "NEWS_ID"), inverseJoinColumns = @JoinColumn (name = "COMMENT_ID")) @OrderBy ("postedDate DESC") private List comments;// Constructors, setters, getters}CREATE TABLE TCOMMENT ( ID BIGINT, NICKNAME VARCHAR(50), CONTENT VARCHAR(500), NOTE INT, POSTED_DATE TIMESTAMP, PRIMARY KEY (ID)); CREATE TABLE NEWS ( ID BIGINT, CONTENT VARCHAR(500), PRIMARY KEY (ID)); CREATE TABLE NEWS_COMMENT ( NEWS_ID BIGINT, COMMENT_ID BIGINT, PRIMARY KEY (NEWS_ID, COMMENT_ID), FOREIGN KEY (NEWS_ID) REFERENCES NEWS(ID), FOREIGN KEY (COMMENT_ID) REFERENCES TCOMMENT(ID)); The above is "How to use annotations to implement one-to-many relationships in Spring". Thank you for reading all the contents of this article! Hope to share the content to help everyone, more relevant knowledge, welcome to pay attention to the industry information channel!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report