0

I am a JPA newbie and trying to understand @JoinTable annotation for Bidirectional OneToMany relationship b/w Project and Task Entities where Project can have multiple tasks.

I can use @JoinTable with Entity having @ManyToOne annotation, but when I am placing @JoinColumn on the other Entity having @OneToMany, I am not getting an option to specify "mappedBy" attribute on @ManyToOne annotation.

I would like to know why ?

I have tried placing @JoinTable annotation on both the entities but then Hibernate is itrying to insert two records in Join table

Project Entity :-

@Entity
@Data
public class Project {
    @Id
    @Column(name = "project_pk")
    @GeneratedValue
    private Long id;

    @Column(name = "project_name")
    private String name;

    @OneToMany(mappedBy = "project", cascade = CascadeType.ALL)
    List<Task> tasks;
}

Tasks Entity :-

@Entity
@Data
public class Task {

    @Id
    @GeneratedValue
    @Column(name = "task_pk")
    private Long id;

    public Task() {
    }

    private String name;

    @ManyToOne(cascade = CascadeType.ALL)
    @JoinTable(name = "project_related_tasks",
            inverseJoinColumns = @JoinColumn(name = "project_id", referencedColumnName = "project_pk"),
            joinColumns = @JoinColumn(name = "task_id", referencedColumnName = "task_pk")
    )
    private Project project;

    public Task(String name) {
        this.name = name;
    }
}
Niteesh Bhargava
  • 157
  • 1
  • 10

1 Answers1

1

There are two ways to implement one-to-many relations:

  1. Using a join table
  2. Using a foreign key on the many-to-one side

mappedBy is used for the second way (using a foreign key). You don't have to specify mappedBy, if you want to use a join table.

Using a join table is not very good idea because you can't control that join table using Hibernate. For example you can't just add a record to a join table directly.

what is @JoinColumn and how it is used in Hibernate

v.ladynev
  • 19,275
  • 8
  • 46
  • 67
  • `mappedBy` also exists in ManyToMany relationships where the `joinTable` is the default implementation mechanism. It is there to hint what side of the relationship will be responsible for the other side. – Panagiotis Bougioukos Mar 28 '21 at 13:04
  • @Boug Responsibility is usually controlled by cascade. – v.ladynev Mar 28 '21 at 13:23
  • 1
    'You don't have to specify mappedBy, if you want to use a join table' is a bit misleading. You *have to* specify `mappedBy` in a bidirectional association. It doesn't matter if you use a join table or a FK – crizzis Mar 28 '21 at 16:19
  • @v.ladynev I m sorry to interrupt you but while I was playing around with these annotations I found a way where I didn't use mappedBy attribute and still there was no JoinTable was created. I have a stack overflow post regarding this as well https://stackoverflow.com/q/75754258. Kindly look into this post as it would really help me. – humbleCodes Mar 18 '23 at 20:37
  • @crizzis can you also take a look at a problem stated in above comment, that I have shared though a stack overflow post with the link https://stackoverflow.com/q/75754258 – humbleCodes Mar 18 '23 at 20:39