gpt4 book ai didi

Spring数据投影和错误: "No aliases found in result tuple! Make sure your query defines aliases!"

转载 作者:行者123 更新时间:2023-12-04 22:28:07 33 4
gpt4 key购买 nike

以下从 JPA 查询中获取 Spring 数据投影的方法对我不起作用:

https://stackoverflow.com/a/45443776/1005607

我的 table :

查找_T

id   description    display_order_num
------------------------------------
1 Category #1 1
2 Category #2 2

ACTIVITIES_T(activity_category_id 映射到 LOOKUP_T.id)
id  activity_category_id  activity_title
---------------------------------------
1 2 Sleeping
2 2 Eating
3 2 Travel

Spring Data DAO 接口(interface)从这个连接中获取某些字段:
@Repository
public interface ActivitiesDAO extends JpaRepository<ActivitiesT, Integer> {

@Query("select a.activityTitle, l.description as category, " +
"l.displayOrderNum as categoryDisplayOrderNum " +
"from ActivitiesT a, LookupT l " +
"where a.lookupT.id = l.id order by l.displayOrderNum asc ")
public List<MySpringDataProjection> findCustom();

}

Spring数据投影模型接口(interface):
public interface MySpringDataProjection {

public String getActivityTitle();

public String getCategory();

public Integer getCategoryDisplayOrderNum();

}

一切都与接受的答案相同。但收到此错误:
org.springframework.dao.InvalidDataAccessApiUsageException: No aliases found in result tuple! Make sure your query defines aliases!; nested exception is java.lang.IllegalStateException: No aliases found in result tuple! Make sure your query defines aliases!
org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:381)
org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:489)
org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59)
org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)

我不想使用 select new Obj(..)在查询中,它很脏并且依赖于我们抽象到 JPA 的 Hibernate。

我想让这种投影方法发挥作用。

我引用的(非工作)答案的相关问题,
Spring data JPA: getting No aliases found in result tuple! error when executing custom query

最佳答案

我遇到同样的问题。在尝试了几次更改后,我发现我们只需要在 NativeQuery 中为每列添加“as”(即使列名没有更改)。在这里为您更改您的 sql,如下所示:

    @Query("select a.activityTitle **as activityTitle**, l.description as category, " + 
"l.displayOrderNum as categoryDisplayOrderNum " +
"from ActivitiesT a, LookupT l " +
"where a.lookupT.id = l.id order by l.displayOrderNum asc ")

关于Spring数据投影和错误: "No aliases found in result tuple! Make sure your query defines aliases!",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47420600/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com