当我尝试使用特定 ID(非自动生成)保存一些相关实体时,我遇到了 crudRepository(Spring boot-hibernate-postgres)和级联问题。我花了一些时间谷歌搜索没有成功。
这是方法:
private String importSourceInfo() {
SourceCompany sourceCompany= new SourceCompany();
sourceCompany.setName("companyName");
sourceCompany.setId(UUID.fromString("2bf05cbc-d530-11eb-b8bc-0242ac130003"));
Set<SourceUser> sourceUsers = new HashSet<>();
SourceUser sourceUser= new SourceUser();
sourceUser.setName("dev-team");
sourceUser.setId(UUID.fromString("4bede7a0-d530-11eb-b8bc-0242ac130003"));
sourceUser.setCompany(sourceCompany);
sourceUsers.add(sourceUser);
sourceCompany.setUsers(sourceUsers);
SourceInfo sourceInfo= new SourceInfo();
sourceInfo.setSourceUser(sourceUser);
sourceInfo.setSourceCompany(sourceCompany);
sourceInfo.setId(UUID.fromString("74a52aa0-d530-11eb-b8bc-0242ac130003"));
sourceInfo.setVersion("v1");
sourceInfo.setDescription("company v1");
sourceInfoRepository.save(sourceInfo);
return "SourceInfo has been imported!";
}
这些是模型:
来源信息:
@Data
@Entity
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
@Table(name = "source_info")
public class SourceInfo {
@Id
@EqualsAndHashCode.Include
private UUID id;
@ManyToOne(cascade=CascadeType.PERSIST)
@JoinColumn(name = "fk_source_user", referencedColumnName = "id")
private SourceUser sourceUser;
@ManyToOne(cascade=CascadeType.PERSIST)
@JoinColumn(name = "fk_source_company", referencedColumnName = "id")
private SourceCompany sourceCompany;
@Column(columnDefinition = "text")
private String description;
private String version;
}
来源公司:
@Data
@Entity
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
@Table(name = "source_company")
public class SourceCompany {
@Id
@EqualsAndHashCode.Include
private UUID id;
private String name;
@OneToMany(cascade = CascadeType.PERSIST, mappedBy = "company")
private Set<SourceUser> users;
}
来源用户:
@Data
@Entity
@EqualsAndHashCode(onlyExplicitlyIncluded = true)
@Table(name = "source_user")
public class SourceUser {
@Id
@EqualsAndHashCode.Include
private UUID id;
private String name;
//ref
@ManyToOne
@JoinColumn(name = "fk_source_company")
private SourceCompany company;
}
这是错误:
javax.persistence.EntityNotFoundException: Unable to find xxx.sp_model.sustainability.model.utils.SourceCompany with id 2bf05cbc-d530-11eb-b8bc-0242ac130003
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$JpaEntityNotFoundDelegate.handleEntityNotFound(EntityManagerFactoryBuilderImpl.java:163) ~[hibernate-core-5.4.27.Final.jar!/:5.4.27.Final]
at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:216) ~[hibernate-core-5.4.27.Final.jar!/:5.4.27.Final]
at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:332) ~[hibernate-core-5.4.27.Final.jar!/:5.4.27.Final]
at org.hibernate.event.internal.DefaultLoadEventListener.doOnLoad(DefaultLoadEventListener.java:108) ~[hibernate-core-5.4.27.Final.jar!/:5.4.27.Final]
at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:74) ~[hibernate-core-5.4.27.Final.jar!/:5.4.27.Final]
at org.hibernate.event.service.internal.EventListenerGroupImpl.fireEventOnEachListener(EventListenerGroupImpl.java:113) ~[hibernate-core-5.4.27.Final.jar!/:5.4.27.Final]
at org.hibernate.internal.SessionImpl.fireLoadNoChecks(SessionImpl.java:1186) ~[hibernate-core-5.4.27.Final.jar!/:5.4.27.Final]
at org.hibernate.internal.SessionImpl.internalLoad(SessionImpl.java:1051) ~[hibernate-core-5.4.27.Final.jar!/:5.4.27.Final]
at org.hibernate.type.EntityType.resolveIdentifier(EntityType.java:697) ~[hibernate-core-5.4.27.Final.jar!/:5.4.27.Final]
我认为休眠想要更新子 sourceCompany 而不是保存它(如果是新的)。但我不知道如何解决它。(我还需要以更复杂的结构扩展最终解决方案,所以我需要一个通用解决方案,因为原则上应用程序的 id 是自动生成的并且一切正常,现在需要在后端设置这些 ID 以便保存特定的 ID。)