简体   繁体   English

通过两个唯一字段 spring boot 搜索唯一性

[英]Search by two unique fields spring boot for uniqueness

I created a child entity:我创建了一个子实体:

public class ChildEntity  {
   private Long bookNo;
   private String bookType;
}

Those two fields are together unique.这两个领域一起是独一无二的。 Some kind of data:某种数据:

| No | Type      |
|----|-----------|
| 1  | Classical |
| 2  | Classical |
| 2  | Scifi     |
| 3  | Scifi     |
| 1  | Classical | (Error for uniqueness

I created this entity to search inside the parent entity.我创建了这个实体来搜索父实体。

public class ParentEntity{
    @ManyToOne
    private ChildEntity child;    
    //and other fields here
}

Also, there are 2 different parent entities.此外,还有 2 个不同的父实体。 They have to be different entity, they have a few different fields.他们必须是不同的实体,他们有几个不同的领域。 But this child entity is common.但是这个子实体很常见。 So, I will use those child entities to compare / take difference between data tables because only unique areas are in that child entity ones.因此,我将使用这些子实体来比较/获取数据表之间的差异,因为该子实体中只有唯一区域。 I can take difference of data tables via that child entity fields.我可以通过该子实体字段获取数据表的差异。

I made ManyToOne because many different parents can have the same child.我制作了ManyToOne ,因为许多不同的父母可以拥有同一个孩子。

I create parent entities in a service like this:我在这样的服务中创建父实体:

private createParentEntity(Dto dto){
    
    ParentEntity parent = new ParentEntity();
    //set other fields from dto
    parent.setAge(dto.getAge);
    
    ChildEntity child = new ChildEntity();
    child.setBookNo(dto.getBookNo());
    child.setBookType(dto.getBookType());
    
    parent.setChild(child);//here is problem , please look down
}

What my problem is, the line I put a comment.我的问题是,我发表评论的那一行。

Same child entities can come, so when I save parents, it will try to save the child again and can get a unique constrain error?相同的子实体可以来,所以当我保存父母时,它会再次尝试保存孩子并且会得到一个唯一的约束错误? What can I do to do this?我能做些什么来做到这一点?

Because in the end, I will use spring data to search like this inside parent repository:因为最后,我将使用 spring 数据在父存储库中像这样搜索:

parentEntityRepository.findByChild(ChildEntity child);
@UniqueConstraints = {
    @UniqueConstraint(
         name = "uq_general_checklist_ordinal",
         columnNames = {"book_no", "book_type"}
    )
}
public class ChildEntity {
   @Id
   private Long bookNo;
    
   @Id
   private String bookType;
}

Is that a good solution to put both Ids?这是放置两个 ID 的好方法吗? Or here, I should first save the child and check if it exists or not?或者在这里,我应该先救孩子,然后检查它是否存在?

ChildEntity child =childRepository.findByBookNoAndBookType(
     dto.getBookNo(), 
     dto.getBookType()
);//if exists, set to parent. if not, create new and set it?

I'd do something like this: "Or here, i should first save child and check if exists or not?"我会做这样的事情:“或者在这里,我应该先保存孩子并检查是否存在?”

However, instead of saving the child first, just search for a child with the new IDs.但是,不是先保存孩子,而是搜索具有新 ID 的孩子。 This way, you avoid SQL Exceptions.这样,您可以避免 SQL 异常。 If it doesn't exist, still dont save the child.如果它不存在,仍然不要救孩子。 Add it to the parent and save the parent.将其添加到父级并保存父级。 It will consequently save the child.因此,它将拯救孩子。 If it exists, add it to the new parent and save the parent.如果存在,将其添加到新父级并保存父级。

Unless when I want to make heavy use of Hibernate's first level caching and such, my goto approach for tight One-to-Many scenarios has become to make this a unidirectional relationship but except for the loading part take all responsibility out of Hibernate's hands and just do it myself.除非我想大量使用 Hibernate 的第一级缓存等,否则我在紧密的一对多场景中的 goto 方法已经变成了一种单向关系,但是除了加载部分之外,所有的责任都从 Hibernate 手中拿走,只是自己做。

That makes it predictable and saves a lot of time in fiddling around with Hibernate and resulting problems with other conventions, peoples' assumptions and other frameworks (eg recursive toString,equals,hashcode methods when you have them autogenerated / not carefully crafted).这使得它可以预测并节省大量时间来摆弄 Hibernate 以及其他约定、人们的假设和其他框架(例如递归 toString、equals、hashcode 方法,当您自动生成/未精心制作时)。

For this I use:为此,我使用:

@OneToMany
@JoinColumn(insertable = false, updatable = false)

at the parent and just the plain id column at the child.在父级和子级的普通 id 列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM