Здравствуйте..помогите пжлста... Есть главный класс (Entity) .В котором есть ссылки(колекции) на другие сущности.
@Entity
public class Projects {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String ame;
@ManyToMany
@JoinTable(name="employee")
private List<Employee> employee;
@ManyToMany
@JoinTable(name="projectLeader")
private List<ProjectLeader> projectLeader;
@ManyToMany
@JoinTable(name="projectPerformers")
private List<ProjectPerformers> projectPerformers;
//get/set
}
Есть еще три остальных класса Entity.
@Entity
public class Employee {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Long id;
private String name;
@ManyToMany(mappedBy = "employee")
private List<Projects> projects;
//get/set...
}
@Entity
public class ProjectLeader {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Long id;
private String name;
@ManyToMany(mappedBy = "projectLeader")
private List<Projects> projects;
//get/set...
}
@Entity
public class ProjectPerformers {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
Long id;
private String name;
@ManyToMany(mappedBy = "projectPerformers")
private List<Projects> projects;
//get/set...
}
А сохраняю вот так:
@Repository
@Transactional
public class DataService {
@Autowired
SessionFactory sessionFactoryDistant;
Success s = new Success();
private List<Employee> em = new ArrayList<Employee>();
private List<ProjectLeader> pLeader = new ArrayList<ProjectLeader>();
private List<ProjectPerformers> pPerformers= new ArrayList<ProjectPerformers>();
public Object add(){
Projects p = new Projects();
Employee employee = new Employee();
ProjectLeader projectLeader = new ProjectLeader();
ProjectPerformers projectPerformers = new ProjectPerformers();
p.setName("lalalProject");
employee.setName("vinniPuh");
projectLeader.setName("michael");
projectPerformers.setName("progers");
em.add(employee);
pLeader.add(projectLeader);
pPerformers.add(projectPerformers);
p.setEmployee(em);
p.setProjectLeader(pLeader);
p.setProjectPerformers(pPerformers);
sessionFactoryDistant.getCurrentSession().save(p);
s.setResult("сохранено");
return s;
}
}
Вот такая ошибка :
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.dao.InvalidDataAccessApiUsageException: object references an unsaved transient instance - save the transient instance before flushing: com.springapp.mvc.models.Employee; nested exception is org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.springapp.mvc.models.Employeeorg.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:978)org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:868)javax.servlet.http.HttpServlet.service(HttpServlet.java:641)org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
root cause
org.springframework.dao.InvalidDataAccessApiUsageException: object references an unsaved transient instance - save the transient instance before flushing: com.springapp.mvc.models.Employee; nested exception is org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.springapp.mvc.models.Employeeorg.springframework.orm.hibernate4.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:190)org.springframework.orm.hibernate4.HibernateTransactionManager.convertHibernateAccessException(HibernateTransactionManager.java:684)org.springframework.orm.hibernate4.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:566)org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:757)org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:726)org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:496)org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:276)org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653)com.springapp.mvc.service.DataService$$EnhancerBySpringCGLIB$$35c7728e.add(<generated>)com.springapp.mvc.m_controllers.MainController.addProject(MainController.java:21)sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)java.lang.reflect.Method.invoke(Method.java:606)org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:781)org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:721)org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:868)javax.servlet.http.HttpServlet.service(HttpServlet.java:641)org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
root cause
org.hibernate.TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing: com.springapp.mvc.models.Employeeorg.hibernate.engine.internal.ForeignKeys.getEntityIdentifierIfNotUnsaved(ForeignKeys.java:294)org.hibernate.type.EntityType.getIdentifier(EntityType.java:537)org.hibernate.type.ManyToOneType.nullSafeSet(ManyToOneType.java:165)org.hibernate.persister.collection.AbstractCollectionPersister.writeElement(AbstractCollectionPersister.java:899)org.hibernate.persister.collection.AbstractCollectionPersister.recreate(AbstractCollectionPersister.java:1308)org.hibernate.action.internal.CollectionRecreateAction.execute(CollectionRecreateAction.java:67)org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:463)org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:349)org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:350)org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:56)org.hibernate.internal.SessionImpl.flush(SessionImpl.java:1222)org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:425)org.hibernate.engine.transaction.internal.jdbc.JdbcTransaction.beforeTransactionCommit(JdbcTransaction.java:101)org.hibernate.engine.transaction.spi.AbstractTransactionImpl.commit(AbstractTransactionImpl.java:177)org.springframework.orm.hibernate4.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:558)org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:757)org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:726)org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:496)org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:276)org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:653)com.springapp.mvc.service.DataService$$EnhancerBySpringCGLIB$$35c7728e.add(<generated>)com.springapp.mvc.m_controllers.MainController.addProject(MainController.java:21)sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)java.lang.reflect.Method.invoke(Method.java:606)org.springframework.web.method.support.InvocableHandlerMethod.invoke(InvocableHandlerMethod.java:215)org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:132)org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:104)org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:781)org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:721)org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:83)org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:943)org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:877)org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:966)org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:868)javax.servlet.http.HttpServlet.service(HttpServlet.java:641)org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:842)javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.14 logs.