We are getting the following error when trying to create a course entity instance on our web application. This is a JHipster app Full error log is below but the main error is
'org.hibernate.PersistentObjectException: detached entity passed to persist:'
The offending code seems to be in this class: CourseResoure.java
The full code is at:
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)
updated code:
@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);
}