Spring Data JPA自定义查询中的QuerySyntaxException

时间:2018-09-16 17:12:47

标签: spring spring-boot spring-data-jpa spring-data

我有一个实体:

@Entity
@Table(name = "story", schema = "")
@Data
public class Story implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "sID", unique = true, nullable = false)
    private Long sID;

    @Column(name = "vnName", nullable = false)
    private String vnName;

    @Temporal(TemporalType.TIMESTAMP)
    @DateTimeFormat(pattern = "dd-MM-yyyy HH:mm:ss")
    @Column(name = "sUpdate", length = 19)
    private Date sUpdate;
}

并且:

@Entity
@Table(name = "chapter", schema = "")
@Data
public class Chapter implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "chID", unique = true, nullable = false)
    private Long chID;

    @Column(name = "chName", nullable = false)
    private String chName;

    @JoinColumn(name = "sID", referencedColumnName = "sID")
    @ManyToOne(fetch = FetchType.LAZY)
    private Story story;
}

我创建了自定义pojo来获取带有最新章节的最新更新故事:

@Data
public class NewStory{
    private Story story;
    private Chapter chapter;
}

但是当我得到列表时:

@Repository
public interface StoryRepository extends CrudRepository<Story, Long> {
    @Query(value="SELECT NEW com.apt.truyenmvc.entity.NewStory(s as newstory, c as newchapter)"
                   + " FROM story s LEFT JOIN (SELECT * FROM Chapter c INNER JOIN "
                   + " (SELECT MAX(c.chID) AS chapterID FROM Story s LEFT JOIN Chapter c ON s.sID = c.sID GROUP BY s.sID) d"
                   + " ON c.chID = d.chapterID) c ON s.sID = c.sID order by s.sUpdate desc")
    public List<NewStory> getTopView();
}

错误:

Warning error: org.hibernate.hql.internal.ast.QuerySyntaxException: story is not mapped.

谁可以帮助我解决此问题?还是可以通过其他方式完成?

1 个答案:

答案 0 :(得分:0)

错误是自我解释。它只是您查询中的 typo 。您正在使用story。显然那还没有映射为实体。

将其修复到Story