我有這些 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)
);
物體:
@Entity
@NoArgsConstructor
@EqualsAndHashCode
@Getter
@Setter
@ToString
@Table(name = "deals_new")
public class DealTwap {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private long id;
@Column(name = "slip_id")
private String slipId;
...
}
@Entity
@NoArgsConstructor
@Getter
@Setter
@Table(name = "twap")
public class Twap implements Serializable {
@Id
@Column(name = "id")
private long id;
@Column(name = "deal_id")
private String dealId;
@Column(name = "employee_id")
private Long employeeId;
@OneToMany(fetch = FetchType.LAZY)
@JoinColumn(name = "slip_id", referencedColumnName = "deal_id")
private List<Deal> deals;
}
@Entity
@Table(name = "employees")
@NoArgsConstructor
@AllArgsConstructor
@EqualsAndHashCode
@Builder
@Getter
@Setter
@ToString
public class Employee {
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "last_name")
private String lastName;
@Column(name = "first_name")
private String firstName;
@ManyToOne(cascade = CascadeType.ALL)
@JoinColumn(name = "account_id")
private Account account;
}
!!筆記!!!Employee和之間沒有關系Twap
我創建了這個 JPA 存盤庫:
public interface DealsRepository extends JpaRepository<DealTwap, 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<ResultDTO> getHistoryAllPairsSearchParam(@Param("offset") int offset,
@Param("limit") int limit);
}
如您所見,我使用此介面得到了結果:
public interface ResultDTO {
String getFirstName();
String getLastName();
}
List<ResultDTO> list = dealsRepository.getHistoryAllPairsSearchParam(...);
for (ResultDTO item : list) {
System.out.println("!!!!!!!!!!!!!!! a " item.getFirstName());
}
當我運行代碼時,我得到:
!!!!!!!!!!!!!!! a null
!!!!!!!!!!!!!!! a null
!!!!!!!!!!!!!!! a null
..........
你知道有什么問題嗎?結果,我總是得到空值。當我在 SQL 編輯器中運行這個查詢時,我得到了正確的結果表。
uj5u.com熱心網友回復:
您正在嘗試將本機查詢結果映射到非物體類。如果我沒記錯的話,結果列名應該匹配方法名。您是否嘗試過為這樣的列設定別名?
SELECT e.first_name AS firstName, e.last_name AS lastName
您還閱讀過這個Spring Data JPA 將本機查詢結果映射到非物體 POJO嗎?它看起來與您的情況非常相似。
如果這沒有幫助,看起來谷歌有很多關于如何做到這一點的結果本機查詢結果在 dto。
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/409036.html
標籤:
上一篇:如何使用SpringBoot帶來大型物體并將其轉換為dto
下一篇:SpringBoot:使用WebSecurityConfigurerAdapter從2.5.7遷移到2.6.2后出現IllegalStateException
