'How to access many to many relationship auto generated table in Java Spring?
I have two table named recipe and tags, joined with many to many relationship
This is recipe table:
@Data
@NoArgsConstructor
@Entity()
@Table(name = "recipes",
    uniqueConstraints = {
        @UniqueConstraint(name = "recipe_title_unique", columnNames = "title")
})
public class RecipeEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", updatable = false)
    private Long id;
    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(
        joinColumns = @JoinColumn(name = "recipe_id", referencedColumnName = "id"),
        inverseJoinColumns = @JoinColumn(name = "tag_id", referencedColumnName = "id")
    )
    private Set<TagEntity> tags;
And this is tags table:
@Data
@NoArgsConstructor
@Entity()
@Table(name = "tags", uniqueConstraints = {
        @UniqueConstraint(name = "tag_unique", columnNames = "name")
})
public class TagEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", updatable = false)
    private int id;
    @Column(name = "name", nullable = false)
    private String name;
ERD Diagram for many to many relationship
My question is, how I access recipes_tags table with JPQL?
For example I want to know how many recipe that use tag with ID 1.
@Query(value = "SELECT " +
        "COUNT t.tag_id " +
        "FROM recipes_tags t +
        "WHERE t.tag_id = :tagId")
Int getCount(@Param("tagId") int tagId);
							
						Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source | 
|---|
