Reputation: 77
i have a problem that when i execute a entitymanager.persiste(object) it throws an exception
Here are my files :
Entities :
@Entity
@Table(name = "user")
public class User extends BaseEntity implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@Column(name = "iduser")
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer userId;
@OneToMany(mappedBy="user",fetch = FetchType.LAZY)
private List<Project> projects;
@ManyToMany(mappedBy="senderReciever")
private Set<Mail> mails;
... attributes and getters and setters and constructors ...
}
second entity
@Entity
@Table(name = "project")
public class Project {
@Id
@Column(name = "idSM")
@GeneratedValue(strategy=GenerationType.AUTO)
private Integer projectId;
@OneToMany
private List<Sprint> sprints;
@OneToMany
private List<Backlog> backlogs;
@ManyToOne
@JoinColumn(name = "iduser")
private User user;
... attributes and getters and setters and constructors ...
}
My DAOs
@Repository
public class UserDaoImpl extends GenericDaoImpl<User,Integer> implements UserDao {
@PersistenceContext(unitName="easyScrumPU")
EntityManager em;
UserDaoImpl(){
super(User.class);
}
@Override
protected EntityManager getEntityManager() {
return em;
}
}
second dao
@Repository
public class DAOprojectImpl extends GenericDaoImpl<Project,Integer> implements DAOproject {
@PersistenceContext(unitName="easyScrumPU")
EntityManager em;
DAOprojectImpl(){
super(Project.class);
}
@Override
protected EntityManager getEntityManager() {
return em;
}
}
my services
@Service
public class ProjectServiceImpl extends GenericServiceImpl<Project,Integer> implements ProjectService {
@Autowired
DAOproject projectdao;
@Autowired
private BackLogService backLogService;
@Override
protected GenericDao<Project, Integer> getDao() {
return projectdao;
}
@Override
public void save(Project prj) {
for(Backlog i : (prj.getBacklogs()))
backLogService.save(i);
projectdao.create(prj);
}
}
second service
@Service
public class UserServiceImpl extends GenericServiceImpl<User, Integer> implements UserService{
@Autowired
public UserDao userDao;
@Override
protected GenericDao<User, Integer> getDao() {
return userDao;
}
@Override
public List<User> findUserByLogin(String login) {
return userDao.findWithNamedQuery("findByLogin", by("login",login).parameters());}
}
this is what i wrote in the controller to persist a new project
Project projet = new Project(7, "facebook", "English", "Web App", "an awsome Work", "socialMedia;Blue;Awsome",
"12000$", "Sumsung", "[email protected]", "22/12/2014", "Getting Started", "22/04/2014", "22/11/2014");
projet.setUser(userService.find(2));
projectService.persist(projet);
the exception i get :
mai 13, 2015 4:27:55 PM org.apache.catalina.core.StandardWrapperValve invoke
GRAVE: Servlet.service() for servlet [dispatcher] in context with path [/easyscrumweb] threw exception [Request processing failed; nested exception is javax.persistence.PersistenceException: org.hibernate.PersistentObjectException: detached entity passed to persist: com.sqli.easyscrum.entity.Project] with root cause
org.hibernate.PersistentObjectException: detached entity passed to persist: com.sqli.easyscrum.entity.Project
... a lot of lines
Upvotes: 1
Views: 98
Reputation: 1571
Try to construct Project
object without setting its projectId
, as you annotated it with @GeneratedValue(strategy=GenerationType.AUTO)
Upvotes: 1