简体   繁体   中英

Like Button Implementation in Spring Boot

In my application users can post articles. And other users can like these articles.

Article class :

@Entity
public class Article {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "article_id")
    private int id;

    @Column(name = "article_title")
    private String articleTitle;

    @OneToMany(mappedBy = "event")
    private List<PeopleWhoLiked> peopleWhoLiked;
}

@Entity
public class PeopleWhoLiked {
    @EmbeddedId
    private PeopleWhoLiked id;
    @ManyToOne @MapsId("articleId")
    private Article article;
    @ManyToOne @MapsId("userId")
    private User user;
}

And there is category entity.Every article has one category.

public class Category {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "category_id")
    private int id;

    @NotEmpty
    @Column(name = "categoryName")
    private String categoryName;

    @OneToMany(mappedBy = "article")
    private List<Article> articleList;
}

My Like Table

Article_id User_id
x          x

These are both foreign keys to related tables.

With category.getArticleList(); function i can show articles to users.They can like articles.But the thing is the system doesn't know that if the article was liked by user already. So always like button is active.

Querying (select statement for every article on Like table) is looks like has huge time complexity and overload to the system.) Even if i do how can i post this into thymeleaf th:each statement with only Article object.

I think querying 10 article's like per time with one select statement sounds good .But again how can i pass this to thymeleaf with Article object.

Your problem with performance is caused by additional request for every row.

For 1 select returning 100 rows you make additionals 100 select to database.

If you need display complicated result build view and than map result of view to your @Entity class, which will used only for presentation purpose.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM