JPA 2.0: Load a subset of fields for an entity

前端 未结 3 981
太阳男子
太阳男子 2020-12-16 23:01

I have an Entity named address. The address Entity has several fields, one of which is CITY. I\'ve created a query by calling entityManager.createQuery but my query only inc

3条回答
  •  借酒劲吻你
    2020-12-16 23:49

    There is a way to do this, but it involves some duplication of code. You'll have to create a new entity called "AddressWithCityOnly" containing just the ID and the CITY attributes and point it to the same table in the db (address table).

    Then you can use this new entity to do partial load of the Address entity (loading these entities will only load the CITY field) and partial update (will update only the CITY field) to the address table.

    I am facing a similar issue. I have a PERSON table with about 70 columns and about 100,000 rows, and I want to update just one column of all of the rows. Loading the original entity would retrieve all 70 columns which is too much overhead.

提交回复
热议问题