JPA @ManyToMany how to bring related objects

2

I'm mapping two objects: Professor and Vehicles, it's a n to n relationship so I'm using a relationship table called "rel_prof_vehicle". The problem comes when it comes to obtaining any object Professor or Vehicles, it brings me the whole object but in the set of vehicles or professors that have respectively I get the following error:

com.sun.jdi.InvocationException occurred invoking method

I've looked through stackoverflow and people say it's by the toString method, but it's self-generated:

@Override
    public String toString() {
        return "Professor [professorId=" + professorId + ", firstName=" + firstName + ", lastName=" + lastName
                + ", dni=" + dni + ", email=" + email + ", birthdate=" + birthdate + ", phone=" + phone + ", home="
                + home + ", vehicles=" + vehicles + "]";
    }

I also leave the mappings too. Professor:

@ManyToMany(fetch = FetchType.LAZY,
        cascade = {
                CascadeType.PERSIST,
                CascadeType.MERGE
        })
    @JoinTable(name = "rel_prof_vehicle",
        joinColumns = { @JoinColumn(name = "professor_id") },
        inverseJoinColumns = { @JoinColumn(name = "vehicle_id") })
    public Set<Vehicle> getVehicles() {
        return vehicles;
    }

Vehicle:

@ManyToMany(fetch = FetchType.LAZY,
                cascade = {
                    CascadeType.PERSIST,
                    CascadeType.MERGE
                },
                mappedBy = "vehicles")
    public Set<Professor> getProfessors() {
        return professors;
    }

I have been browsing and viewing mappings of objects n to n in jpa, but I have not gotten them working, if they knew of some place or another way of mapping them (same with ManyToOne) I would appreciate it. Before you ask, in the database I have the foreign keys of the table related to professor and vehicle.

    
asked by Daniel Velasco Talavera 27.03.2018 в 22:08
source

1 answer

0

Fixed: it turns out that in the methods toString (), hashCode () and equals () was calling the opposite method of the bidirection creating an infinite loop. I have removed calls to these methods in each model and it works correctly.

    
answered by 31.03.2018 в 12:34