简体   繁体   English

如何包含冗余列以显示在 SELECT 从第二个表而不是从 Spring 存储库中的主表查询?

[英]How to include redundant column to show in SELECT Query from second table instead from the main table in Spring Repository?

I have a query like this: Query 1:我有这样的查询:查询 1:

select *
from items item

This is bind to an entity in SpringJPA.这绑定到 SpringJPA 中的实体。 Now I have to select one more column from another table like below.现在我必须 select 从另一张表中再增加一列,如下所示。

Query 2:查询 2:

select item.*,is.id
from items item
inner join item_state itm_s where item.id=itm_s.id

Now,there is a column "code" which exists in both item and item_state.现在,item 和 item_state 中都存在一个“代码”列。 I want to select it from item_state and not from item due to some data issues.由于某些数据问题,我想从 item_state 而非 item 中获取 select。 I know the straight forward way is to write all columns in select statement,excluding code column from item and including code column from item_state.我知道直接的方法是在 select 语句中编写所有列,排除项目中的代码列并包括项目状态中的代码列。 But the thing is item table has around 100 columns.但问题是项目表有大约 100 列。 Is there a good way to solve this issue without changing the entity class?在不改变实体class的情况下,有什么好的方法可以解决这个问题吗?

Look at Spring Data JPA projections and perform the following query:查看Spring 数据 JPA 投影并执行以下查询:

@Query("select item.*, itm_s.code from items item join item.item_state itm_s where item.id = itm_s.id")

You will have to look at the resulting query to write your projection class.您将不得不查看生成的查询以编写您的投影 class。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM