Spring JPA @ManyToMany z dodatkową kolumną.

0

Cześć - mam relację many to many między Userem a Eventem. Póki co wszystko działało ok do momentu kiedy stwierdziłem, że potrzebuję dodatkową kolumnę w tabeli wiążącej ( informacja o reakcji użytkownika na wydarzenie). Zrobiłem, więc tak:

@Entity
public class User {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private long id;

    @OneToMany(mappedBy="user")
    private Set<EventUser> eventUsers = new HashSet<>();

//

}


@Entity
public class Event {

    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    private long id;

    @OneToMany(mappedBy="event")
    private Set<EventUser> eventUsers = new HashSet<>();

//

}

@Entity
@Table(name = "Event_User")
public class EventUser  {

    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    private long id;

    private String reaction;

    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "user_id")
    private User user;

    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "event_id")
    private Event event;

//

}

I tak .. Spring tworzy tą dodatkową tabelę w bazie, wszystko niby ok, ale teraz nie wiem jak np wyszukać wszystkie wydarzenia po mailu usera ...
Wcześniej w interfejscie rozszerzającym JpaRepository<Event,Long> miałem metodę:

Collection<Event> findByUsersEmail(String email);

A teraz tak nie zrobię. Próbowałem utworzyć taką metodę:

Collection<Event> findByEventUsersUserEmail(String email);

ale wywala mi nieskończoną rekurencję ...

Rady ?

0

Może po prosty stworzyć normalne zapytanie JPQL?

0

@scibi92:

@Query("SELECT eu.event FROM EventUser eu WHERE eu.user.email = :email")
    Collection<Event> findByUserEmail(@Param("email") String email);

Napisałem tak i znów nieskończona rekursja ...

EDIT:

Ok naprawiłem - @JsonIgnore zaradził ;) I nie muszę stosować JPQL - stare zapytanie działa.

0

Ale gdzie dałeś to @JsonIgnore i czemu to pomogło?

0

Potrzebujesz zupełnie innej konstrukcji, która będzie miała klucz złożony:

@Entity
public class UserEvent{

	@EmbeddedId
	private UserEventId userEventId;

	ManyToOne(fetch = FetchType.LAZY)
	@MapsId("userId")
    	private User post;
 
    	@ManyToOne(fetch = FetchType.LAZY)
    	@MapsId("eventId")
    	private Event tag;
 
    	@Column(name = "reaction")
	private String reaction;

	@Embeddable
	public static class UserEventId implements Serializable{
		@Column(name = "post_id")
    		private Long postId;
 
		@Column(name = "tag_id")
		private Long tagId;
	}
	// gettery settery, poprawnie zrobiony hashCode itd.
}

I teraz twój User wygląda tak:

@Entity
public class User{
	@Id
    	@GeneratedValue
    	private Long id;
 
    @OneToMany(
        mappedBy = "user",
        cascade = CascadeType.ALL,
        orphanRemoval = true
    )
    private List<UserEvent> posts = new ArrayList<>();
   /// reszta 
}

a Event tak:

@Entity
public class Event{
	@Id
    	@GeneratedValue
    	private Long id;
 
    @OneToMany(
        mappedBy = "event",
        cascade = CascadeType.ALL,
        orphanRemoval = true
    )
    private List<UserEvent> posts = new ArrayList<>();
   /// reszta 
}

I to by było na tyle. Choć pewno walnąłem gdzieś literówkę.

1 użytkowników online, w tym zalogowanych: 0, gości: 1