1

I've been learning JPA 2 and have learned about OneToMany and ManyToOne relationships. Here is my criteria.

  1. There are two entities, Employee and Feedback.
  2. An Employee can receive multiple feedbacks. i.e. there is ManyToOne relationship between a feedback and Employee. This I have achieved.
  3. But an Employee can also write multiple feedbacks for multiple employees. This is where I'm getting stuck.

I've build Employee to Feedback relationship as follows.

In Feedback class

    @ManyToOne
@JoinColumn(name="idEmployee")
private Employee employee;

and in Employee class,

@OneToMany(mappedBy = "employee", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private List<Feedback> feedbacks = new ArrayList<Feedback>();

now I can retrieve all feedbacks received for a employee. But an Employee can also give feedbacks to other employees. How can I achieve this relationship?

Do I need to use Map relationship? If yes, then why?

Prasad Kharkar
  • 13,410
  • 5
  • 37
  • 56

3 Answers3

3

You need another OneToMany relationship: ensure to have the two columns in the db table representing Feedback: author and receivee: they will point to the primary key of the employee table, therefore they must have the same type as that. Add the foreign key constraints: both them should point to the primary key of the employee table. Then, map the relationships in Java as explained in zbigniew's answer:

Feedback class:

@ManyToOne
@JoinColumn(name="author")
private Employee author;

@ManyToOne
@JoinColumn(name="receivee")
private Employee receivee;

Employee class:

@OneToMany(mappedBy = "author", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private List<Feedback> givenFeedbacks;
@OneToMany(mappedBy = "receivee", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
private List<Feedback> receivedFeedbacks;
perissf
  • 15,979
  • 14
  • 80
  • 117
2

As I understood correctly, there is a many-to-many relationship between the two tables. You should either annotate with @ManyToMany or have a bridge table so that bridge table has a many-to-one relationship to FeedBack and many-to-one relationship to Employee. I rather second approach, because that respects NF3 of database design.

http://en.wikibooks.org/wiki/Java_Persistence/ManyToMany

http://en.wikipedia.org/wiki/Third_normal_form

pms
  • 944
  • 12
  • 28
1

Start analyzing from the fact that Feedback is connected to two types of Employees - one is an author of the feedback and the second one receives the feedback. After that all build up altogether like this.

@Entity
class Feedback {
    // id etc ...

    @ManyToOne
    Employee author;

    @ManyToOne
    Employee receivee;
}

@Entity
class Employee {
    // id etc ...

    @OneToMany(mappedBy="author")
    List<Feedback> feedbackGiven;

    @OneToMany(mappedBy="receivee")
    List<Feedback> feedbackReceived; 
}
zbig
  • 3,830
  • 2
  • 29
  • 37
  • Thank you for the idea sir. I had already tried this and called fetched both by using EAGER relationship. It encountered exceptions saying multiple eager fetches cannot be done. – Prasad Kharkar Jul 21 '14 at 14:58
  • Also, wouldn't it be that Feedback table will have two columns those are foreign keys to the same column in Employee class? – Prasad Kharkar Jul 21 '14 at 15:00
  • Yes, Feedback should have two FKs to Employee. And Eager Joining the should not be a problem. You have only FK to Employee in Feedback and cannot change it? If that is the case, than you are a bit stuck. With Map you would have to have a join table between Employee and Feedback. Are you considering join table? The most important thing is that you need to store an information about relationship type (FeedbackeeType={GIVEN,RECEIVED}) somewhere. Where it can be, that depends on your constraints in design. I just posted IMHO best design for your case. – zbig Jul 21 '14 at 15:11
  • design is not a constraint as I am simply learning things for my own. If the information is to be put in another table, then how do we access it? I mean I understand the relationship but if I'm going to implement your design, how do I do it? Could you please explain a little bit more? – Prasad Kharkar Jul 22 '14 at 05:54
  • Please consult another answer of mines: http://stackoverflow.com/questions/24861892/jpa-annotations-lists-opposed-to-separately-included-instances/24862474#24862474 – zbig Jul 22 '14 at 07:27
  • Thank you, I got my doubts cleared and example is working now :) – Prasad Kharkar Jul 22 '14 at 10:00