Mini version of JPA entity classes and relating them

huangapple go评论79阅读模式
英文:

Mini version of JPA entity classes and relating them

问题

I am looking for creating a smaller version of an entity class.

我正在寻找创建实体类的较小版本。

I have Patient table and Diabetic Patient table and medicines table.. And they are all huge master tables.

我有患者表、糖尿病患者表和药物表... 它们都是庞大的主表。

I would like to create mini versions and use them for listing.. like Patient Mini, Diabetic Patient mini and medicines mini.

我想创建迷你版本并在列表中使用它们... 像患者迷你、糖尿病患者迷你和药物迷你。

Is that possible in JPA with Hibernate. I can not do anything directly on Master entities since they are used by different other teams and i have a limitation of using the entire entities rather than individual fields in it.. So is there a way to create such mini version of the entity classes. Please advice.

在JPA和Hibernate中是否有可能实现这一点?由于主实体被其他不同的团队使用,我不能直接对主实体进行任何操作,而且我有一个只能使用整个实体而不是其中的单个字段的限制... 那么有没有办法创建这样的实体类的迷你版本。请提供建议。

英文:

I am looking for creating a smaller version of an entity class.

I have Patient table and Diabetic Patient table and medicines table.. And they are all huge master tables.

I would like to create mini versions and use them for listing.. like Patient Mini, Diabetic Patient mini and medicines mini.

Is that possible in JPA with Hibernate. I can not do anything directly on Master entities since they are used by different other teams and i have a limitation of using the entire entities rather than individual fields in it.. So is there a way to create such mini version of the entity classes. Please advice.

答案1

得分: 1

你可以创建一些DTO(数据传输对象),并使用 select new 查询并填充DTO,这是开箱即用的功能。DTO类必须提供一个构造函数,该构造函数接受查询结果集中所需的所有属性。

示例:

List<PatientDTO> PatientDTOs = entityManager.createQuery("select new your.package.structure.dto.PatientDTO(p.id, p.name, p.medicines) from Patient p ", PatientDTO.class).getResultList();

如果你想了解如何将投影查询映射到DTO(数据传输对象)使用JPA和Hibernate,可以查看以下链接:
https://vladmihalcea.com/the-best-way-to-map-a-projection-query-to-a-dto-with-jpa-and-hibernate/

英文:

You could create some DTOs and use select new to query and fill the DTOs out of the box. The DTOs classes must provide a constructor that takes all the attributes that you need to be fetched by the result set of the query.

example:

List&lt;PatientDTO&gt; PatientDTOs = entityManager.createQuery(&quot;select new your.package.structure.dto.PatientDTO(p.id, p.name, p.medicines) from Patient p &quot;, PatientDTO.class).getResultList();

if you want to know more about how map a projection query to a DTO (Data Transfer Object) with JPA and Hibernate :
https://vladmihalcea.com/the-best-way-to-map-a-projection-query-to-a-dto-with-jpa-and-hibernate/

huangapple
  • 本文由 发表于 2020年8月12日 17:18:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/63373511.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定