[英]How to set up a child entity referencing parent by only ID using a JoinTable in Spring JPA/Hibernate
Problem问题
I only care about the IDs for the associations.我只关心关联的 ID。 I do not want the DTO to have the respective associated class as an attribute.
我不希望 DTO 将相应的关联类作为属性。 I want
Parent
to have Set<String> childIds
and Child
to have String parentId
in the DTOs.我希望
Parent
在 DTO 中有Set<String> childIds
和Child
有String parentId
。 Because of this, I do not want to load the entire object in the persistence layer just to map away everything else.因此,我不想在持久层中加载整个对象只是为了映射其他所有内容。
Attempts尝试
EntityManager.getReference
but that throws an error if it's a new entity, so how can I save new ones?EntityManager.getReference
但如果它是一个新实体,则会引发错误,那么我该如何保存新实体呢? I'd have to do an existsById
but now we're making even more database calls, this is getting expensiveexistsById
但现在我们要进行更多的数据库调用,这变得越来越昂贵JoinColumn
, JoinColumns
, JoinTable
, etc.JoinColumn
、 JoinColumns
、 JoinTable
等的组合。 Java classes Java 类
Entities实体
@Entity
@(LombokGettersAndSetters)
public class ParentEntity {
@Id
private Long id;
@(???)
private Set<Long> childIds; || private Set<ChildEntity> children;
}
@Entity
@(LombokGettersAndSetters)
public class ChildEntity {
@Id
private Long id;
@(???)
private Long parentId; || private ParentEntity parent;
}
Mappers映射器
(Mapstruct mappers)
DTOs DTO
public class ParentDTO {
private Long id;
private Set<Long> childIds;
}
public class ChildDTO {
private Long id;
private Long parentId;
}
Tables表
Parent Child Parent_join_Child
------- ----- -----------------
id id parent_id
child_id
Thanks in advance!提前致谢!
Edit @ +1m : I should note, I removed some annotations in my examples such as the below编辑@ +1m :我应该注意,我在我的示例中删除了一些注释,如下所示
@Column(name = "parent_id", columnDefinition = CustomColumnDefinition.UNSIGNED_INT)
@GeneratedValue(strategy = GenerationType.IDENTITY)
You can use @JoinTable
to join your parent to child as shown below您可以使用
@JoinTable
将您的父母加入到孩子中,如下所示
@Entity
@(LombokGettersAndSetters)
public class ParentEntity {
@Id
private Long id;
@JoinTable(name = "Parent_join_Child", joinColumns = {
@JoinColumn(name = "parent_id", referencedColumnName = "id") }, inverseJoinColumns = {
@JoinColumn(name = "child_id", referencedColumnName = "id") })
private Set<ChildEntity> children;
}
Can you do the following:你能不能做到以下几点:
Create ParentChild entity:创建父子实体:
@Entity
public class ParentChild {
private long id;
private long parentId;
private long childId;
}
and work with it:并使用它:
parentChildRepository.findByWhatever() and group by parent and map to dto.
You'd have to write a custom query that selects only the parts that you care about, but then you won't be able to use Mapstruct.您必须编写一个仅选择您关心的部分的自定义查询,但是您将无法使用 Mapstruct。 I recommend you take a look at whatBlaze-Persistence Entity Views has to offer.
我建议您查看Blaze-Persistence Entity Views提供的功能。
I created the library to allow easy mapping between JPA models and custom interface or abstract class defined models, something like Spring Data Projections on steroids.我创建了该库以允许在 JPA 模型和自定义接口或抽象类定义的模型之间轻松映射,例如类固醇上的 Spring Data Projections。 The idea is that you define your target structure(domain model) the way you like and map attributes(getters) via JPQL expressions to the entity model.
这个想法是您按照自己喜欢的方式定义目标结构(域模型),并通过 JPQL 表达式将属性(getter)映射到实体模型。
Assuming an entity model like this:假设一个实体模型是这样的:
@Entity
public class ParentEntity {
@Id
private Long id;
@OneToMany(mappedBy = "parent")
private Set<ChildEntity> children;
}
@Entity
public class ChildEntity {
@Id
private Long id;
@ManyToOne
private ParentEntity parent;
}
A DTO model for your use case could look like the following with Blaze-Persistence Entity-Views:使用 Blaze-Persistence Entity-Views 的用例的 DTO 模型可能如下所示:
@EntityView(ParentEntity.class)
public interface ParentDTO {
@IdMapping
Long getId();
@Mapping("children.id")
Set<Long> getChildIds();
}
Querying is a matter of applying the entity view to a query, the simplest being just a query by id.查询是将实体视图应用于查询的问题,最简单的只是按 id 查询。
ParentDTO a = entityViewManager.find(entityManager, ParentDTO.class, id);
Behind the scenes this will create the query to fetch exactly what is needed, nothing more.在幕后,这将创建查询以准确获取所需内容,仅此而已。
The Spring Data integration allows you to use it almost like Spring Data Projections: https://persistence.blazebit.com/documentation/entity-view/manual/en_US/index.html#spring-data-features Spring Data 集成允许您几乎像 Spring Data Projections 一样使用它: https : //persistence.blazebit.com/documentation/entity-view/manual/en_US/index.html#spring-data-features
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.