[英]Spring Data JPA unable to find object with id
我正在使用 JPA + Hibernate + Spring 數據 Z9CE3D1BD8890F16A0C44809359508 用於我的項目數據庫(MySQL)。 最近我有一個錯誤說:
org.springframework.orm.jpa.JpaObjectRetrievalFailureException: Unable to find com.kanda.mindwire.domain.results.CognitiveResult with id 1700; nested exception is javax.persistence.EntityNotFoundException: Unable to find com.kanda.mindwire.domain.results.CognitiveResult with id 1700
at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:379) ~[spring-orm-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:257) ~[spring-orm-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:528) ~[spring-orm-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61) ~[spring-tx-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:242) ~[spring-tx-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:153) ~[spring-tx-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:149) ~[spring-data-jpa-2.2.3.RELEASE.jar:2.2.3.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93) ~[spring-aop-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212) ~[spring-aop-5.2.2.RELEASE.jar:5.2.2.RELEASE]
at com.sun.proxy.$Proxy157.findByCompany(Unknown Source) ~[na:na]
at com.kanda.mindwire.service.employee.EmployeeService.findWithFilter(EmployeeService.java:210) ~[classes/:1.0-SNAPSHOT]
該代碼試圖獲取與CognitiveResult具有OneToOne關系的Employee實體列表。 我在想可能是有Employee行, cognitive_id=1700但是當我查詢我的數據庫時,我發現沒有這樣的行。
問題是 hibernate 從哪里獲取這個id以及如何修復它?
這是我的代碼: Employee.java
@Table(uniqueConstraints = @UniqueConstraint(columnNames = {Employee_.EMAIL}))
@Entity
@Data
public class Employee extends BaseEmployee {
@Column
private String email;
....
@ManyToOne
@JoinColumn(name = "company_id")
private Company company;
@OneToOne(cascade = CascadeType.ALL, orphanRemoval = true)
@JoinColumn(name = "cognitive_id", referencedColumnName = "id")
private CognitiveResult cognitiveResult;
...
}
認知結果.java
@Entity
@Table(name = "cognitive_results")
@Data
public class CognitiveResult extends BaseAssesment {
...
@OneToOne(mappedBy = "cognitiveResult")
private Employee employee;
@PreRemove
public void removeCognitiveForEmployee() {
if (employee != null) {
employee.setCognitiveResult(null);
}
}
...
}
EmployeeRepository.java
public interface EmployeeRepository extends BaseRepository<Employee> {
...
List<Employee> findByCompany(Company company);
...
}
公司.java
@Entity
@Data
public class Company extends BaseSettingsEntity {
...
@OneToMany(mappedBy = "company", cascade = CascadeType.ALL)
private Set<Employee> employees = new HashSet<>();
/**
* Logic for syncing employees
* @param employee
*/
public void removeEmployee(Employee employee) {
if (!employees.contains(employee)) {
return;
}
employees.remove(employee);
employee.setCompany(null);
}
public void addEmployee(Employee employee) {
if (employees.contains(employee)) {
return;
}
employees.add(employee);
employee.setCompany(this);
}
...
}
我所有的域模型都繼承自BaseEntity.java
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@Entity
@Data
public abstract class BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.TABLE)
@Column(updatable = false, nullable = false)
protected Long id;
...
}
調用 findByCompany 時發生異常:
@Slf4j
public class EmployeeService extends BaseEntityService<Employee> {
private final EmployeeRepository repo;
public Map<String, Map<EmployeeJob, List<Employee>>> findWithFilter(EmployeeFilterWrapper filter, String username) {
List<Employee> employeeList;
User user = userService.findByUsername(username);
Company company = user.getCompany();
if (company == null)
throw new InvalidCompanyException("Company not assigned for a user!");
if (filter != null && filter.getDivisionsFilter() != null && CollectionUtils.isNotEmpty(filter.getDivisionsFilter().getDivisions())) {
employeeList = repo.findEmployeeByDivisionNamesAndCompany(filter.getDivisionsFilter().getDivisions(), company.getName());
} else {
employeeList = repo.findByCompany(company);
}
...
}
發現一個問題。 那里有一個鏈接到另一個表中缺少的cognitive_id
並且它丟失了,因為我最近直接在db 中清除了這些實體,但忘記了它們引用了另一個表。 這是一個從Employee
繼承的Supervisors
表
@Entity
@Table(name = "supervisors")
@Data
public class Supervisor extends Employee {
...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.