0

I have two tables which have Many-to-Many relations which have a JoinTable USER_SERVICES as below.

@Entity
public class User implements Serializable {

    @NotNull
    @Column(unique=true)
    private String username;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Long id;

    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(
            name = "USER_SERVICES",
            joinColumns = {@JoinColumn(name = "id", referencedColumnName = "id")},
            inverseJoinColumns = {@JoinColumn(name = "", referencedColumnName = "name")})
    private Set<Services> services;

    // Getters and Setters
}   


@Entity
public class Services implements Serializable {

    @NotNull
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Id
    private Long serviceId;

    @NotNull
    @Column(unique=true)
    private String name;

    //Getters and Setters

}

The above code creates a table USER_SERVICES, but I also want to have a Many-to-Many relation on the table USER_SERVICES with another table RATINGS which would result in another table USER_SERVICES_RATINGS. how can I define this relation with Hibernate/JPA annotations?

Himalay Majumdar
  • 3,883
  • 14
  • 65
  • 94
  • 1
    you may find [my tutorial](http://stackoverflow.com/questions/24257449/how-do-i-use-annotations-to-define-x-relationship-in-hibernate-4-and-spring) on Hibernate mappings with annotations helpful. – JamesENL Jul 02 '14 at 04:06
  • Thank you @JamesMassey, following your tutorial I was able to solve my problem. It would be great if you can also add some more code on CascadeType for manytomany. If you can just copy paste your code from your tutorial here, I will accept your answer. – Himalay Majumdar Jul 02 '14 at 18:10

2 Answers2

3

Bi-Directional Many to Many using user managed join table object (Relatively common)

Commonly used when you want to store extra information on the join object such as the date the relationship was created.

public class Foo{
    private UUID fooId;

    @OneToMany(mappedBy = "bar", cascade=CascadeType.ALL)
    private List<FooBar> bars;
}

public class Bar{
    private UUID barId;

    @OneToMany(mappedBy = "foo", cascade=CascadeType.ALL)
    private List<FooBar> foos;
}

@Entity
@Table(name="FOO_BAR")
public class FooBar{
    private UUID fooBarId;

    @ManyToOne
    @JoinColumn(name = "fooId")
    private Foo foo;

    @ManyToOne
    @JoinColumn(name = "barId")
    private Bar bar;

    //You can store other objects/fields on this table here.
}
JamesENL
  • 6,400
  • 6
  • 39
  • 64
  • All good but how could I fetch all `Bar` objects from a `Foo` object directly and not lose my custom `FooBar` table? – Stefan Falk May 13 '18 at 09:30
1

You need to create an explicit UserServices entity and setup the relationship to the Ratings entity per your needs.

Remember that in hibernate you model relationships between entities (i.e. your java objects), not db tables.

Taylor
  • 3,942
  • 2
  • 20
  • 33
  • That is right Taylor, I was able to solve the problem thanks to you and James. I am waiting for James to post snippet from his tutorial here, else I will accept your answer. – Himalay Majumdar Jul 02 '14 at 18:11