spring 如何使用JPA实现“轻量级实体”版本?

92dk7w1h  于 2023-06-21  发布在  Spring
关注(0)|答案(1)|浏览(86)

有一个“完整的实体”类:

@Entity(name = "vacancy_dec_to_words")
public class VacancyDescriptionToWords {

    @Id
    @Column(name = "id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;

    @JoinColumn(name = "vacancy_description_id")
    @ManyToOne(cascade = CascadeType.ALL)
    private VacancyDescription vacancyDescription;

    @JoinColumn(name = "words_id")
    @ManyToOne
    private Words words;

    @Column(name = "qty")
    private int qty;

    @Column(name = "create_date")
    private Date date;

    //...getters and setters

在一些方法中,我只需要使用这个数据库中的2列:word_idqty
我尝试以下方法:

预测https:docs.spring.io/spring-data/jpa/docs/2.1.2.RELEASE/reference/html/#projections

public interface QtyWords {
    Long getWords();
    Integer getQty();
}

JpaReposytory:

  • 查询,我用测试和它可行,我用他在JpaRepository
@Repository
public interface SmallVDTWRepository extends JpaRepository<VacancyDescriptionToWords, Long> {

@Query(nativeQuery = true,
        value = "SELECT sum(qty), words_id FROM vacancy_desc_to_words WHERE vacancy_description_id IN (" +
                "SELECT id FROM vacancy_description WHERE vacancy_id IN (" +
                "SELECT id FROM vacancy WHERE explorer_id = :exp))" +
                "GROUP BY words_id")
List<QtyWords> getDistinctWordsByExplorer(@Param("exp") long exp);
}

但是当我得到实体列表时,我得到了一些有趣的结果:

List<QtyWords> list = vdtwService.getByExplorerId(72);

我没有得到任何例外,但我有一个未知对象的列表。这个对象包含了我需要的数据(qtywords_id),但是我不能从他那里得到它们。

我可以使用这个方法(Projection)来实现这个任务吗?一般来说,在这种情况下如何正确地实现“Light Entity”?

7z5jn7bk

7z5jn7bk1#

Spring提供了两种机制,可用于限制要获取的数据。

预测

投影可以帮助您减少从数据库检索的数据,具体方法是设置要获取的属性。

示例:

@Entity
class Person {
    @Id UUID id;
    String firstname, lastname;
    @OneToOne
    Address address;
}

@Entity
static class Address {
    @Id UUID id;
    String zipCode, city, street;
}

interface NamesOnly {
    String getFirstname();
    String getLastname();
}

@Repository
interface PersonRepository extends Repository<Person, UUID> {
    Collection<NamesOnly> findByLastname(String lastname);
}

实体图

注解EntityGraph可以帮助您减少对数据库的查询量,通过设置您需要获取的确切相关实体。

示例:

@Entity
@NamedEntityGraph(name = "GroupInfo.detail", attributeNodes = @NamedAttributeNode("members"))
public class GroupInfo {
    @Id UUID id;
    @ManyToMany //default fetch mode is lazy.
    List<GroupMember> members = new ArrayList<GroupMember>();
}

@Repository
public interface GroupRepository extends CrudRepository<GroupInfo, String> {

    @EntityGraph(value = "GroupInfo.detail", type = EntityGraphType.LOAD)
    GroupInfo getByGroupName(String name); //Despite of GroupInfo.members has FetchType = LAZY, it will be fetched because of using EntityGraph
}

有两种类型的EntityGraph

  1. EntityGraphType.LOAD-用于指定实体图,由实体图的属性节点指定的属性被视为FetchType.EAGER,并且未指定的属性根据其指定的或默认的FetchType来处理。
  2. EntityGraphType.FETCH-用于指定实体图,由实体图的属性节点指定的属性被视为FetchType.EAGER,并且未指定的属性被视为FetchType.LAZY

**PS:**还记得可以设置lazy fetch type:@ManyToOne(fetch = FetchType.LAZY)和JPA不会在提取父实体时提取子实体。

相关问题