简体   繁体   中英

Spring Data JPA multiple one to many relations from primary key as a foreign key

I'm working on tinder-like Spring Boot and Angular web app, but stuck at making relationship in JPA (I've done relations in postgreSQL database in pgAdmin) I tried @OneToMany, @JoinColumns and other methods, but can't figure out how to make it and if making relationship like this is even possible, because I hadn't found example like this on any site (including Stackoverflow of course)

When one person swipe right another person application will insert into Swipes

  • id: id

  • decision: yes/no

  • swipeFrom: id of profile who sent like (profiles.id)

  • swipeTo: id of profile who gets like (profiles.id)

  • timestamp: timestamp

AND IN EVERY OTHER TABLES IT WILL WORK LIKE ABOVE

Is relations like this possible? If not, what should I do? Maybe just left it like it's and just delete Matches, Swipes etc. manually in method when parent is deleted? 在此处输入图片说明

Although I don't know what you've tried, but it is possible. If you want bidirectional mapping, you would have:

@Entity
@Table(name = "profiles")
public class Profile {

    // other fields

    @OneToMany(mappedBy = "sender", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<Message> sentMessages = new ArrayList<>();

    @OneToMany(mappedBy = "receiver", cascade = CascadeType.ALL, orphanRemoval = true)
    private List<Message> receivedMessages = new ArrayList<>();

    // other collections for swipes, matches and timers
}

@Entity
@Table(name = "messages")
public class Message {

    // other fields

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "message_from")
    private Profile sender;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "message_to")
    private Profile receiver;
}

The same applies for other tables (swipes, matches, timers). You use @JoinColumn to specify which foreign key you want to map to which field.

If you want unidirectional mapping or something else, I encourage you to check Vlad Mihalcea 's article The best way to map a @OneToMany relationship with JPA and Hibernate .


Note : You'll need to use Criteria API (or some other approach) if you want to fetch Profile with sent and received Messages. If you would try something like:

@Query("from Profile p join fetch p.sentMessages join fetch p.receivedMessages where p.id = :id")
Optional<Profile> findProfileByIdFetchSendAndReceivedMessages(int id);

or

@Override
@EntityGraph(attributePaths = { "sentMessages", "receivedMessages" })
Optional<Profile> findById(int id);

you would get MultipleBagFetchException .

Since there are many great articles on this topic, I will not go into details now. For example, you can check another Vlad Mihalcea 's article The best way to fix the Hibernate MultipleBagFetchException if you ran into this issue.

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