【发布时间】:2021-06-30 03:36:32
【问题描述】:
尝试在我们的 Web 应用程序上创建课程实体实例时出现以下错误。这是一个 JHipster 应用程序完整的错误日志如下,但主要错误是
'org.hibernate.PersistentObjectException: 分离的实体传递给持久化:'
有问题的代码似乎在这个类中:CourseResoure.java
完整代码在:
https://github.com/smitht06/aseproj
org.springframework.dao.InvalidDataAccessApiUsageException: detached entity passed to persist: edu.uwf.cen6030.domain.User; nested exception is org.hibernate.PersistentObjectException: detached entity passed to persist: edu.uwf.cen6030.domain.User
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:319)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:255)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:528)
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61)
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:242)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:153)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodInterceptor.invoke(CrudMethodMetadataPostProcessor.java:178)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:95)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
at com.sun.proxy.$Proxy183.save(Unknown Source)
at edu.uwf.cen6030.web.rest.CourseResource.createCourse(CourseResource.java:68)
更新代码:
@PostMapping("/courses")
@Transactional()
public ResponseEntity<Course> createCourse(@Valid @RequestBody Course course) throws URISyntaxException {
log.debug("REST request to save Course : {}", course);
if (course.getId() != null) {
throw new BadRequestAlertException("A new course cannot already have an ID", ENTITY_NAME, "idexists");
}
if (Objects.isNull(course.getTeacher())) {
throw new BadRequestAlertException("Invalid association value provided", ENTITY_NAME, "null");
}
Long userId = course.getTeacher().getId();
userRepository.findById(userId).ifPresent(course::user);
Course result = courseRepository.save(course);
return ResponseEntity.created(new URI("/api/courses/" + result.getId()))
.headers(HeaderUtil.createEntityCreationAlert(applicationName, false, ENTITY_NAME, result.getId().toString()))
.body(result);
}
【问题讨论】:
-
您对 jhipster 的看法可能是正确的。这是相当复杂的。我添加了您建议的符号,但仍然出现错误 Resolved [org.springframework.dao.InvalidDataAccessApiUsageException: detached entity passing to persist: edu.uwf.cen6030.domain.User;嵌套异常是 org.hibernate.PersistentObjectException:分离的实体传递给持久化:edu.uwf.cen6030.domain.User]
-
不需要方法级别的注释,因为它已经存在于类级别。
标签: hibernate spring-data-jpa jhipster