Spring 休眠持久性异常分离实体
Spring hibernate persistance exception detached entity
我有一个问题,当我执行一个 entitymanager.persiste(object) 它抛出一个异常
这是我的文件:
实体:
@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 ...
}
第二个实体
@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 ...
}
我的 DAO
@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;
}
}
二道
@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;
}
}
我的服务
@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);
}
}
第二次服务
@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());}
}
这是我在控制器中写的用来持久化一个新项目的内容
Project projet = new Project(7, "facebook", "English", "Web App", "an awsome Work", "socialMedia;Blue;Awsome",
"12000$", "Sumsung", "face@book.com", "22/12/2014", "Getting Started", "22/04/2014", "22/11/2014");
projet.setUser(userService.find(2));
projectService.persist(projet);
我得到的异常:
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
尝试构造 Project
对象而不设置它的 projectId
,因为你用 @GeneratedValue(strategy=GenerationType.AUTO)
注释它
我有一个问题,当我执行一个 entitymanager.persiste(object) 它抛出一个异常
这是我的文件:
实体:
@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 ...
}
第二个实体
@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 ...
}
我的 DAO
@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;
}
}
二道
@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;
}
}
我的服务
@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);
}
}
第二次服务
@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());}
}
这是我在控制器中写的用来持久化一个新项目的内容
Project projet = new Project(7, "facebook", "English", "Web App", "an awsome Work", "socialMedia;Blue;Awsome",
"12000$", "Sumsung", "face@book.com", "22/12/2014", "Getting Started", "22/04/2014", "22/11/2014");
projet.setUser(userService.find(2));
projectService.persist(projet);
我得到的异常:
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
尝试构造 Project
对象而不设置它的 projectId
,因为你用 @GeneratedValue(strategy=GenerationType.AUTO)