0

I'm trying to create a query in a project with Spring Data that uses an inner join, and I'm getting a "Can't resolve symbol" error.

My project structure is this

Project structure

My entities are:

import javax.persistence.*;

@Entity
@Table(name="players")
public class Player {

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

    private String name;

    @ManyToOne(fetch= FetchType.LAZY)
    @JoinColumn(name="team_id")
    private Team team;

// Getters, setters, etc


import javax.persistence.*;

@Entity
@Table(name="incidences")
public class Incidence {

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

    @ManyToOne
    @JoinColumn(name = "player_id")
    private Player player;

    private IncidenceType type;

    private IncidenceStatus status;

    public enum IncidenceStatus {
        ACTIVE, COMPLETED
    }

// Getters, setters, etc

So, i don't want a bidirectional relationship between Player and Instance.

And this is my repository:

import com.github.juanmougan.worldcup.api.model.Player;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

import java.util.List;

public interface PlayerRepository extends CrudRepository<Player, Long> {

    //@Query("SELECT p.id, p.name, p.team_id FROM Player p inner join p.incidences i WHERE i.status = 'ACTIVE'")
    @Query("SELECT p.id, p.name, p.team_id FROM Player p inner join Incidence i WHERE i.status = 'ACTIVE'")
    List<Player> findPlayersWithActiveIncidences();

}

Which yields the following error: "Can't resolve symbol Player" (also for the Incidence).

I came across this question, which deals with a bidirectional mapping. The commented code was an attempt to create the same query, that didn't work. I'm wondering, is it possible to create such query with a unidirectional mapping?

jmm
  • 1,044
  • 2
  • 12
  • 38
  • 1
    I think the from condition should look like: `FROM Incidence i JOIN i.player p` you need a path expression with a join. – Jens Schauder Jan 04 '18 at 06:45
  • @JensSchauder thanks, I'll give it a try. Anyway, it sounds a little odd that a Player repository queries the Incidence table – jmm Jan 04 '18 at 13:48
  • What is weird about it? You already have the JOIN in the statement, you are just using a non-existing syntax. – Jens Schauder Jan 09 '18 at 06:32
  • Possible duplicate of [How To Define a JPA Repository Query with a Join](https://stackoverflow.com/questions/13154818/how-to-define-a-jpa-repository-query-with-a-join) – nitinsridar Aug 20 '19 at 10:37

0 Answers0