JPA -向现有实体添加字段

shyt4zoc  于 6个月前  发布在  其他
关注(0)|答案(1)|浏览(117)

假设我有一个实体类MyTable

@Entity
@Table(name = "my_table")
public class MyTable {

@Id
String a1

String a2

Double a3
...

字符串
在一个查询中,我需要一个新的字段在我的实体类上,我用一个本地查询获得(它必须是一个本地查询)

@Query(value = "select my_table.*, 1 as newCol from my_table", nativeQuery=true)
List<???> findExtraField();


my_table有很多列,这使得Map每一个列变得很麻烦。
如何让查询返回类型为

class newObj {
  MyTable myTable;
  int newCol;
}


而不必MapMyTable的每个字段?

lsmepo6l

lsmepo6l1#

您可以尝试以下操作,但必须触发一个额外的查询。
我假设你把一个

@Transient
private int newCol;

字符串
你的MyTable Entity

public void fetchAllData() {
        //fetch all your MyTable here
        List<MyTable> myTableList = emSlave.createNativeQuery("select my_table.* as newCol from my_table", MyTable.class)
                .getResultList();

        //this query will fetch just the table id from your MyTable and the new column you desire to bring out
        String secondQuery = "select my_table.id as tableId, 1 as newCol from my_table";
        
        List<Object[]> secondQueryResult = emSlave.createNativeQuery(secondQuery)
                .getResultList();
        
        //Collecting the result into a dto
        List<TempDataHolder> tempDataHolderList = secondQueryResult
                .stream()
                .map(o -> new TempDataHolder((Number) o[0], (Number) o[1]))
                .collect(Collectors.toList());

        //Collecting the TempDataHoler into a map
        Map<Integer, Integer> mapOfTableIdAndNewCol = tempDataHolderList
                .stream()
                .collect(Collectors.toMap(TempDataHolder::getTableId, TempDataHolder::getNewCol));

        //using a loop, fetch the value from Map and set into the extra column
        myTableList.forEach((mt) -> {
            mt.setNewCol(mapOfTableIdAndNewCol.get(mt.getId()));
        });
    }

    @Getter
    @Setter
    public class TempDataHolder {

        private int tableId;
        private int newCol;

        public TempDataHolder(Number tableId, Number newCol) {
            this.tableId = tableId.intValue();
            this.newCol = newCol == null ? 0 : newCol.intValue();
        }

    }


我使用TempDataHolder只是为了使它更容易。你可以实现你的结果,而不使用任何新的DTO,只是把你的secondQueryResult直接转换成Map
如果你只想把它投在你提到的新职业上。

public void fetchAllData() {
        //fetch all your MyTable here
        List<MyTable> myTableList = emSlave.createNativeQuery("select my_table.* as newCol from my_table", MyTable.class)
                .getResultList();

        //this query will fetch just the table id from your MyTable and the new column you desire to bring out
        String secondQuery = "select my_table.id as tableId, 1 as newCol from my_table";
        
        List<Object[]> secondQueryResult = emSlave.createNativeQuery(secondQuery)
                .getResultList();
        
        //Collecting the result into a dto
        List<TempDataHolder> tempDataHolderList = secondQueryResult
                .stream()
                .map(o -> new TempDataHolder((Number) o[0], (Number) o[1]))
                .collect(Collectors.toList());

        //Collecting the TempDataHoler into a map
        Map<Integer, Integer> mapOfTableIdAndNewCol = tempDataHolderList
                .stream()
                .collect(Collectors.toMap(TempDataHolder::getTableId, TempDataHolder::getNewCol));

       List<newObj> newObjList=new ArrayList<>(myTableList.size());
        
        for(MyTable mt : myTableList){
            newObjList.add(new newObj(mt, mapOfTableIdAndNewCol.get(mt.getId())));
        }
    }

    @Getter
    @Setter
    public class TempDataHolder {

        private int tableId;
        private int newCol;

        public TempDataHolder(Number tableId, Number newCol) {
            this.tableId = tableId.intValue();
            this.newCol = newCol == null ? 0 : newCol.intValue();
        }

    }

    @Getter
    @Setter
    class newObj {

        MyTable myTable;
        int newCol;

        public newObj(MyTable myTable, int newCol) {
            this.myTable = myTable;
            this.newCol = newCol;
        }
        
        
    }


如果成功了就告诉我。祝你好运!

相关问题