During the internship, we were given the task of creating a web application with authorization, registration, adding to friends and displaying a list of friends. Authorization and registration made on video (Spring security: https://www.youtube.com/watch?v=iivY8B5A0Tk&t=143s ).

To display friends made a separate jsp page at / freinds. All data is in the PostgreSQL database. There are 2 tables in the database:

  1. users (id, login, password)

  2. friends (first_friend (this is the user id from the first table), second_friend (also the id from the first table but a different user) and status) (status is a number from 0 to 2, 0 application, 1 accepted, 2 refused. Like is logical.

But implementation pumped up, because of that it is not familiar with Spring'om. How to connect user tables with friends table?

The user and friends tables are bound to the corresponding classes via @Entity.

@Entity @Table(name = "users") public class User { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; @Column(name = "username") private String username; @Column(name = "password") private String password; 

and

 @Entity @Table(name="friends") public class Friends { @Column(name = "friend_one") String friend_one; @Column(name = "friend_two") String friend_two; @Column(name ="status") int status; 

How to connect friend_one cells to id and friend_two to another id (is it a foreign key?) I don’t know how to access the database via Sql, and how to make a statement. Because authorization and registration are done through model or bean.

And tell me how to display data from the database on the jsp village. My example:

 <table> <c:forEach items="${friends}" var="friend"> <tr> <td>${friend.username}</td> <td><input type="submit" name="action" value="delete"/></td> </tr> </c:forEach> </table> 
  • one
    dig in the direction of many to many hibernate (JPA), spring is nothing to do with. You’ll have to tie the USERS table to yourself - JVic

1 answer 1

The essence of Friends is not needed at all. User can and should refer to other User :

 @Entity public class User { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; ... @ManyToMany(fetch = FetchType.LAZY, cascade=CascadeType.ALL) private Set<User> friends; public void setFriends(Set<User> friends) { this.friends = friends; } public Set<User> getFriends() { return friends; } } 

And then in jsp

 <table> <c:forEach items="${someUser.friends}" var="friend"> <tr> <td>${friend.username}</td> <td><input type="submit" name="action" value="delete"/></td> </tr> </c:forEach> </table> 

And friendship applications with their statuses are best stored in a separate entity of type

 @Entity class Invite { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; @Column private User from; @Column private User to; @Enumerated(EnumType.ORDINAL) private InviteStatus status; } 
  • Can you explain what the code does and how it works? I would like to understand the logic - JusTTaIReX
  • You will not undergo an internship if you do not attempt to figure it out yourself. Here is the very first link in Google for JPA @ManyToMany. - Sergey Gornostaev