I need to have "FriendRequest" and "ListOfFriends" functionalities.Similar to facebook, which shows number of received friend requests and number of approved friends.
By "FriendRequest", I mean to have a list of friend requests that the user receives.
By "ListOfFriends", I mean to have a list of friends of the user.
To achieve that, I defined following class but when I try to retrieve a user using its username, a "Stackoverflow" exception message will be thrown. It seems it goes into an indefinite loop.
When I remove "FriendRequests" and "Friends" from my toString
method it stops throwing the exception.
This question is similar to what I am going to achieve but the difference is I do not what to have a separate Credential class.
Member
@Entity
public class Member implements Serializable {
@Id
@GeneratedValue
private long id;
@Column(name = "username", nullable = false, unique = true)
private String username;
@Column(nullable = false)
private String fname;
@Column(nullable = false)
private String lname;
@OneToMany(fetch = FetchType.EAGER, mappedBy = "requester")
private Set<Friendship> friendRequests = new HashSet<Friendship>();
@OneToMany(fetch = FetchType.EAGER, mappedBy = "friend")
private Set<Friendship> friends = new HashSet<Friendship>();
getters and setters
@Override
public String toString() {
return "Member [
.....
, fname=" + fname + ", lname=" + lname
// + friendRequests.size() + ", friends=" + friends.size() +
"]";
}
}
Friendship
@Entity
public class Friendship implements Serializable {
@Id
@ManyToOne
@JoinColumn(referencedColumnName = "username")
Member requester;
@Id
@ManyToOne
@JoinColumn(referencedColumnName = "username")
Member friend;
@Temporal(javax.persistence.TemporalType.DATE)
Date date;
@Column(nullable = false)
boolean active;
Many-to-Many Association This type of relationship is established in Hibernate with the help of @ManyToMany annotation. For example, the relationship between Book and Author; more than one author may write one or more books; alternatively, many books are written by one or more author.
friendship, a state of enduring affection, esteem, intimacy, and trust between two people. In all cultures, friendships are important relationships throughout a person's life span.
In order to map a many-to-many association, we use the @ManyToMany, @JoinTable and @JoinColumn annotations. Let's have a closer look at them. The @ManyToMany annotation is used in both classes to create the many-to-many relationship between the entities.
hibernate mappings are one of the key features of hibernate . they establish the relationship between two database tables as attributes in your model. that allows you to easily navigate the associations in your model and criteria queries.
I would like to suggest DB Design.
Right now, You are having a friendship table and friends table as per POJO classes. Instead of that, you can just only have friendship table with one more column boolean isAccepted;(variable in POJO class).
If boolean value is true, It means that that member(friend) is a friend. When you want to get all the friends, retrieve the rows of friendship with isAccepted set to true; If you want to get only friendrequests(Or pending requests to be Approved), get all the rows with isAccepted set to false.
With the present DB design or ER model, you are using. You need to delete the row from the friendship table (as It is no more a friend request), If the person accepts the friend request and store somewhere. Where do you store the friends . According to your present design, It stores in the friends table. But there is no column saying that the row indicates a friend of someone. It is just like adding a row(friend) to the friends table every time one member accepts the request of other member and just making redundant rows.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With