【发布时间】:2022-01-11 15:03:42
【问题描述】:
我有这些 Postgres 表:
create table deals_new
(
id bigserial primary key,
slip_id text,
deal_type integer,
timestamp timestamp,
employee_id bigint
constraint employee_id_fk
references common.employees
);
create table twap
(
id bigserial primary key,
deal_id varchar not null,
employee_id bigint
constraint fk_twap__employee_id
references common.employees,
status integer
);
create table common.employees
(
id bigint primary key,
first_name varchar(150),
last_name varchar(150)
);
我创建了这个 JPA 存储库:
public interface DealsRepository extends JpaRepository<Employee, Long> {
@Query (value =
"SELECT e.first_name, e.last_name " +
"FROM common.deals_new d " +
"JOIN common.employees e ON e.id = d.employee_id " +
"LEFT OUTER JOIN common.twap t on " +
" t.deal_id = d.slip_id AND " +
" d.timestamp between '11-11-2010' AND '11-11-2011' AND " +
" d.deal_type in (1, 2) " +
"OFFSET :offset " +
"LIMIT :limit ",
nativeQuery = true)
List<IdsOnly> getHistoryAllPairsSearchParam(@Param("offset") int offset,
@Param("limit") int limit);
}
如你所见,我使用这个接口得到了结果:
public interface IdsOnly {
String getFirstName();
String getLastName();
}
private DealsList getResponseParams(List<IdsOnly> deals) {
return new DealsList( // get here the values from the interface and convert them);
}
我不清楚如何从界面获取值并进行一些处理。您能给我一些建议,例如如何打印它们吗?
【问题讨论】:
-
idsOnlyObject.getFirstName()工作吗? -
我没有在上面的代码中看到
idsOnlyObject?你从哪里弄来的? -
这称为投影,Spring JPA 创建一个实现 IdsOnly 接口的代理,调用 getFirstName() 将返回在数据库中找到的值。在此处阅读更多信息baeldung.com/spring-data-jpa-projections
-
由于某种原因我得到空值。知道如何解决吗?
-
你的 common.employees 类对象叫什么?将接口“IdsOnly”的名称更改为“
View”,看看是否可行。
标签: java spring-data-jpa spring-data