1

I have one problem:

I have 3 table in sql server (postgres)

  • 1 table is object with id and name columns
  • 2 table is object2 with id and name columns
  • 3 table is objects-objects2 with id, object.id from table1 and object2.id from table2 columns

and

I have 2 Classes

public class Obj {

  private int id;

  private String name;
}

public class Obj2 {

  private int id;

  private String name;

  private Set<Obj> objects;
}

What annotations do I need to use for Set objects so that I can receive data through the repository @ManyToMany or?

SSK
  • 3,444
  • 6
  • 32
  • 59
whiteo
  • 11
  • 1
  • Have a look here: [manytomany-relationship-between-three-tables](https://stackoverflow.com/questions/42488559/manytomany-relationship-between-three-tables) – Dirk Deyne Feb 27 '21 at 17:34
  • i thought to do `@ManyToMany @JoinTable(name = "objects-objects2", joinColumns = @JoinColumn(name = "object2.id", foreignKey = @ForeignKey(name = "object2_fk"))))` – whiteo Feb 27 '21 at 18:37
  • Take a look to https://docs.jboss.org/hibernate/orm/current/userguide/html_single/Hibernate_User_Guide.html#associations-many-to-many for more information about suitables ways to work with `ManyToMany` associations. On the other hand, regarding to work with a `Set`, next link could help you https://stackoverflow.com/questions/62526030/in-spring-boot-2-is-it-possible-to-auto-generate-a-jointable-with-a-unique-cons Only if you want to have FK to `objects-objects2` is required create it with specific `id`, usually is enough use columns to the other tables as PK: `object.id & object2.id` – doctore Feb 28 '21 at 08:41

0 Answers0