我有存储库方法,我试图从表中获取特定列。 当我调用 findAllNamesAndID 方法时,我得到对象数组作为返回,其中我期待自定义小部件列表。
我不确定下面的代码有什么问题。
我可以在查询注释中使用 Customwidgets 的构造函数,但我想避免这种情况。有什么解决方法吗?
我有以下域类:-
public class Customwidgets implements Serializable {
@Id
private UUID id;
@NotNull
@Column(name = "name", nullable = false)
private String name;
---
---
}
我的存储库类:-
@Repository
public interface CustomwidgetsRepository extends JpaRepository<Customwidgets, UUID>, JpaSpecificationExecutor<Customwidgets> {
@Query("select o.id, o.name from Customwidgets o")
List<Customwidgets> findAllNamesAndID();
}
最佳答案
您可以创建一个界面来定义您的投影。例如:
public interface CustomWidgetsProjection {
String getId();
String getName();
}
然后您可以返回您创建的这个界面,而不是 CustomWidgets
:
@Query("select o.id, o.name from Customwidgets o")
List<CustomWidgetsProjection> findAllNamesAndID();
关于java - 具有返回对象数组的特定列的 JPQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54283877/