使用单向ManyToOne在Spring Data Query中进行内部联接

时间:2018-01-03 23:50:51

标签: spring spring-mvc jpa spring-data spring-data-jpa

我尝试create a query in a project with Spring Data使用内部联接,并且我得到了一个"无法解析符号"错误。

我的项目结构是这个

Project structure

我的实体是:

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

所以,我想要PlayerInstance之间的双向关系。

这是我的存储库:

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();

}

会产生以下错误:&#34;无法解析符号播放器&#34; (也是为了发生)。

我遇到了这个问题,which deals with a bidirectional mapping。评论的代码是尝试创建相同的查询,但没有起作用。我想知道,是否可以使用单向映射创建此类查询?

0 个答案:

没有答案