英文:
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<PatientDTO> PatientDTOs = entityManager.createQuery("select new your.package.structure.dto.PatientDTO(p.id, p.name, p.medicines) from Patient p ", 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/
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论