0
votes

In my Spring Boot application, I have two models. Actually trying to implement ManyToMany relationship.

A:

@Entity
public class A {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private Set<B> b=new HashSet<>();
    public A() {

    }
}

B:

@Entity
public class B {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    private Set<A> a=new HashSet<>();
    public B() {

    }

}

When I try to compile this this gives me error, saying : Invocation of init method failed; nested exception is javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; nested exception is org.hibernate.MappingException: Could not determine type for: java.util.Set, at table: a, for columns: [org.hibernate.mapping.Column(b)]

Full log : here

1
Did you define your relations ManyToOne or other? Also id does not make sense to B having Set of A and vice versa. If you mean ManyToMany relationship, it does not work like that - Houssem Badri
I getting error only for two days, can you post an answer. Yes I'm trying to create a ManyToMany relationship @HoussemBadri - Maifee Ul Asad
if you want to implement @ManyToMany relationship, where have you implement it? - Patrick
If you want to add ManyToMany relation you need to add an intermediate table, that relationship would be change to OneToMnay and ManyToOne. Have a look at this link: baeldung.com/hibernate-many-to-many - Houssem Badri
@HoussemBadri I tried @JoinTable(name ="...",joinColumns = @JoinColumn(name = "..."), inverseJoinColumns = @JoinColumn(name = "...")) ... And this works, and creates table. But the problem is I don't know how to retrieve data from this table? And I'm not sure if they are updating or not .. - Maifee Ul Asad

1 Answers

0
votes

A:

@Entity
public class A {
    ...
    @ManyToMany(cascade = CascadeType.ALL)//don't use fetch here
    private List<B> b=new ArrayList<>();
}

B:

@Entity
public class B {
    ...
    @ManyToMany(cascade = CascadeType.ALL)//don't use fetch here
    private List<A> a=new ArrayList<>();
}

Don't use fetch on OneToMany or ManyToMany meaning *ToMany