如何使用hibernate选择连接实体的所有字段?



我有两个表:Limit_utilisation_history with fields:

  • bigint limit_utilisation_history_id (PK)
  • 数字(20,2)utilisation_amount
  • bigint limit_id (FK to limit_utilization表)

Limit_utilisation with fields:

  • bigint limit_id (PK)
  • varchar customer_id

所以两个表是相关的。我需要通过rest调用公开以下查询的结果:

select limit_utilisation_history_id, utilisation_amount, limit_id, customer_id
where customer_id in (some list of values)

我用下面的方法做了这件事:

@Entity
@Data
public class LimitUtilisation{
 @Id
 private Long limitIdl
 private String customerId;
}
@Entity
@Data
@NamedQuery{
 name = "LimitUtilisationHistory.getByCustomer",
 query = "FROM LimitUtilisationHistory luh FETCH ALL PROPERTIES " +
         "INNER JOIN luh.limitId al " +
         "WHERE al.customerId in :values"
}
public class LimitUtilisationHistory{
 @Id
 private Long limitUtilisationHistoryId;
 @OneToOne(fetch = FetchType.EAGER)
 @JoinColumn(name = "limit_id", referencedColumnName = "limitId")
 private LimitUtilisation limitId;
}
public interface LimitUtilisationHistoryRepository extends PagingAndSortingRepository<LimitUtilisationHistory, Long> {
 @RestResource(path = "byCustomerId")
 @Query
 List<LimitUtilisationHistory> getByCustomer (@Param("values") List<String> customer);
}

它工作得很好,但是当我调用我的休息端点时,我只有utilisation_amount值,其他(主要是PK, FK,客户id丢失)。有人知道怎么正确地做吗?

select limit_utilisation_history_id, utilisation_amount, limit_id, customer_id
where customer_id in (some list of values)

备注:我不能更新数据库结构。我的意图是只从现有的数据库结构

读取

当您使用FetchType时。我看不出在查询中使用FETCH ALL的任何理由,您可以尝试下面的查询:

select luh.limit_utilisation_history_id, luh.utilisation_amount, luh.limit_id, lu.customer_id 
from LimitUtilisationHistory luh , LimitUtilisation lu
where luh.limit_id = lu.limit_id 
and lu.customerId in :values

最新更新