-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Any better model for the FAN and FOLLOW with Person?
PostPosted: Wed Apr 05, 2017 10:28 am 
Newbie

Joined: Wed Jul 13, 2016 10:40 pm
Posts: 17
I am confusing how to design a OOP model for FAN and FOLLOW, just like the FRIEND and FOLLOW IN facebook.

My design:
Code:
@Entity
public class Person{
   @Id
   private String personId;

   @OneToMany(mappedBy="person")
   private Set<Follow> followSet;
        .....
}

Code:
@Entity
public class Follow {
   @Id
   @GeneratedValue(strategy = GenerationType.IDENTITY)
   private long followId;
   
   @ManyToOne(optional=false)
   private Person person;
   
   @OneToOne(optional=false)
   private Person following;
        .....
}


The disadvantage is that I cannot get the SET of FAN easily.
But Is it really necessary for the relationship to design two sort of SET? (fanSet followSet)


Top
 Profile  
 
 Post subject: Re: Any better model for the FAN and FOLLOW with Person?
PostPosted: Wed Apr 05, 2017 11:02 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
You should have a single table person, and then for friends and follows you have two separate tables in the database.

The friends table has a composite PK build out of 2 FK: from_person_id and to_person_id. Both these two are FK to the person id PK.

The follows table is the same: the PK is also composite PK out of 2 FK: from_person_id and to_person_id. Both these two are FK to the person id PK.

Check out this article about how you can map a composite identifier with JPA and Hibernate. From the FK relationships, you can build the @ManyToOne sides.

You don't necessarily need the collections since you can map the Follow and Friend associations and just use queries for those. For more info, check out this article about @OneToMany association as well.


Top
 Profile  
 
 Post subject: Re: Any better model for the FAN and FOLLOW with Person?
PostPosted: Thu Apr 06, 2017 1:05 am 
Newbie

Joined: Wed Jul 13, 2016 10:40 pm
Posts: 17
vlad wrote:
You should have a single table person, and then for friends and follows you have two separate tables in the database.

The friends table has a composite PK build out of 2 FK: from_person_id and to_person_id. Both these two are FK to the person id PK.

The follows table is the same: the PK is also composite PK out of 2 FK: from_person_id and to_person_id. Both these two are FK to the person id PK.

Check out this article about how you can map a composite identifier with JPA and Hibernate. From the FK relationships, you can build the @ManyToOne sides.

You don't necessarily need the collections since you can map the Follow and Friend associations and just use queries for those. For more info, check out this article about @OneToMany association as well.


Instead of having two tables, I think that why not just add one more attribute to the composite identifier Object to identify this record whether follow or fan.

from_person_id to_person_id tag
1 3 follow
3 1 friend


Top
 Profile  
 
 Post subject: Re: Any better model for the FAN and FOLLOW with Person?
PostPosted: Thu Apr 06, 2017 1:23 am 
Hibernate Team
Hibernate Team

Joined: Thu Sep 11, 2014 2:50 am
Posts: 1628
Location: Romania
That will work as well.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.