JPA @EntityGraph提取模型

时间:2019-11-20 05:48:31

标签: jpa spring-data-jpa spring-data entitygraph

我在存储库方法中添加了类型为FETCH的@EntityGraph。而且我已经将@NamedEntityGraph添加到我的实体,但是属性petEntities尚未由属性节点指定。 根据官方文档,属性petEntities将被视为FetchType.LAZY。但是实际将其视为FetchType.EAGER。

EntityGraphType文档:

        /**
         * When the javax.persistence.fetchgraph property is used to specify an entity graph, attributes that are specified
         * by attribute nodes of the entity graph are treated as FetchType.EAGER and attributes that are not specified are
         * treated as FetchType.LAZY
         *
         * @see <a href="https://download.oracle.com/otn-pub/jcp/persistence-2_1-fr-eval-spec/JavaPersistence.pdf">JPA 2.1
         *      Specification: 3.7.4.1 Fetch Graph Semantics</a>
         */
        FETCH("javax.persistence.fetchgraph");

我的测试日志:

Hibernate: select userentity0_.id as id1_3_0_, accountent1_.id as id1_0_1_, userentity0_.age as age2_3_0_, userentity0_.name as name3_3_0_, accountent1_.account_name as account_2_0_1_, accountent1_.available_curl as availabl3_0_1_, accountent1_.user_id as user_id4_0_1_, accountent1_.user_id as user_id4_0_0__, accountent1_.id as id1_0_0__ from user userentity0_ left outer join account accountent1_ on userentity0_.id=accountent1_.user_id
Hibernate: select petentity0_.id as id1_2_2_, petentity0_.name as name2_2_2_, petentity0_.user_id as user_id3_2_2_, userentity1_.id as id1_3_0_, userentity1_.age as age2_3_0_, userentity1_.name as name3_3_0_, accountent2_.user_id as user_id4_0_4_, accountent2_.id as id1_0_4_, accountent2_.id as id1_0_1_, accountent2_.account_name as account_2_0_1_, accountent2_.available_curl as availabl3_0_1_, accountent2_.user_id as user_id4_0_1_ from pets petentity0_ left outer join user userentity1_ on petentity0_.user_id=userentity1_.id left outer join account accountent2_ on userentity1_.id=accountent2_.user_id where petentity0_.user_id=?
Hibernate: select petentity0_.id as id1_2_2_, petentity0_.name as name2_2_2_, petentity0_.user_id as user_id3_2_2_, userentity1_.id as id1_3_0_, userentity1_.age as age2_3_0_, userentity1_.name as name3_3_0_, accountent2_.user_id as user_id4_0_4_, accountent2_.id as id1_0_4_, accountent2_.id as id1_0_1_, accountent2_.account_name as account_2_0_1_, accountent2_.available_curl as availabl3_0_1_, accountent2_.user_id as user_id4_0_1_ from pets petentity0_ left outer join user userentity1_ on petentity0_.user_id=userentity1_.id left outer join account accountent2_ on userentity1_.id=accountent2_.user_id where petentity0_.user_id=?
=============
UserEntity(id=1, name=牛小粒, age=12)
宠物null
=============
UserEntity(id=2, name=王小红, age=12)
宠物null

存储库:

public interface UserRepository extends JpaRepository<UserEntity, Long> {

   /* @Override
    @EntityGraph(value = "user.accountEntities", type = EntityGraph.EntityGraphType.FETCH)
    List<UserEntity> findAll();*/

    @Override
    @EntityGraph(value = "user.accountEntities2", type = EntityGraph.EntityGraphType.FETCH)
    List<UserEntity> findAll();
}

实体:

@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
@Table(name = "user")
@NamedEntityGraphs(
  value =
    {@NamedEntityGraph(name = "user.accountEntities", attributeNodes = {
      @NamedAttributeNode(value = "accountEntities", subgraph = "accountEntities-subgraph")
    }, subgraphs = {
      @NamedSubgraph(name = "accountEntities-subgraph",
        attributeNodes = {
          @NamedAttributeNode("accountDetailEntities")
        })
    }
    ),
      @NamedEntityGraph(name = "user.accountEntities2", attributeNodes = {
        @NamedAttributeNode(value = "accountEntities")
      }
      )}
)
public class UserEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private int age;
    @ToString.Exclude
    @EqualsAndHashCode.Exclude
    @OneToMany(mappedBy = "userEntity", fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    private Set<AccountEntity> accountEntities;

    @ToString.Exclude
    @EqualsAndHashCode.Exclude
    @OneToOne(mappedBy = "userEntity", cascade = CascadeType.ALL)
    private PetEntity petEntities;

您可以在此处查看完整的代码:https://github.com/winston9527/self-practice/tree/master/spring-data-jpa

0 个答案:

没有答案