Spring hibernate持久异常分离实体

时间:2015-05-13 15:48:55

标签: java spring hibernate jpa persistence

我有一个问题,当我执行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 

1 个答案:

答案 0 :(得分:1)

尝试构建Project对象而不设置其projectId,因为您使用@GeneratedValue(strategy=GenerationType.AUTO)

对其进行了注释