简体   繁体   English

实体映射:-分离的实体传递给持久化

[英]Entity Mapping :- detached entity passed to persist

I am trying to save an object but I get the following error 我正在尝试保存对象,但出现以下错误

org.hibernate.PersistentObjectException: detached entity passed to persist: com.tets.ditacedentity.entity.Department

actully by using CascadeType.MERGE I got the solution but I want to use CascadeType.ALL to the question I want to save the entity by using an CascadeType.ALL . 通过使用CascadeType.MERGE我得到了解决方案,但我想对要使用CascadeType.ALL保存实体的问题使用CascadeType.ALL Please any one tell me the solution for that.Thanks in advance. 请任何人告诉我解决方案。谢谢。

User Entity Class:---- 用户实体类:

@Entity
@Table(name = User.TABLE_NAME, indexes = { @Index(columnList = User.COLUMN_USER_ID, unique = true) })
public class User {
    public static final String TABLE_NAME = "User";
    public static final String COLUMN_USER_ID = "pk_user_id";
    public static final String COLUMN_USER_FNAME = "user_fname";
    public static final String COLUMN_USER_EMAIL = "user_email";

    public static final String DEPARTMENT_FOREIGN_KEY = "fk_department";

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = COLUMN_USER_ID)
    private int userId;

    @Column(name = COLUMN_USER_FNAME, columnDefinition = "VARCHAR(255)")
    private String userName;

    @Column(name = COLUMN_USER_EMAIL, columnDefinition = "VARCHAR(255)")
    private String userEmail;

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = DEPARTMENT_FOREIGN_KEY, foreignKey = 
    @ForeignKey(name = DEPARTMENT_FOREIGN_KEY))
    private Department dept;

    //Getter Setter

}

Department Entity Class:----- 部门实体类别:-----

@Entity
@Table(name = Department.TABLE_NAME, indexes = { @Index(columnList = Department.COLUMN_DEPARTMENT_ID, unique = true)})
public class Department {
    public static final String TABLE_NAME = "Deepartment";
    public static final String COLUMN_DEPARTMENT_ID = "dept_id";
    public static final String COLUMN_DEPARTMENT_NAME = "user_fname";


    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = COLUMN_DEPARTMENT_ID)
    private Integer deptId;

    @Column(name = COLUMN_DEPARTMENT_NAME, columnDefinition = "VARCHAR(255)")
    private String deptName;

//Getter setter
}

Service Impl Class(Buisness logic) 服务Impl类(业务逻辑)

public void save(User user) {

    if(user.getDept()!=null)
    {
        Department dept = deptRepo.findOne(user.getDept().getDeptId());
        if(dept==null)
        {
            dept = deptRepo.save(user.getDept());
        }
        user.setDept(dept);
    }
    UserRepository.save(user);  

    }

The documentation says that CascadeType.ALL is equivalent to 该文档说CascadeType.ALL等效于

cascade={PERSIST, MERGE, REMOVE, REFRESH, DETACH}.

You could choose only the types you realy want to use, like this: 您可以只选择您真正想要使用的类型,如下所示:

@Cascade(cascade={CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE})

Following the documentation: https://docs.oracle.com/javaee/6/api/javax/persistence/CascadeType.html 遵循文档: https : //docs.oracle.com/javaee/6/api/javax/persistence/CascadeType.html

You are most likely passing an entity that has been fetched within an already closed transaction: 您最有可能传递已在已关闭的事务中获取的实体:

public void save(User user)

Try to merge the passed user first and then work on the result: 尝试先合并传递的用户,然后处理结果:

mergedUser = UserRepository.merge(user) // calls session.merge(user) inside

if(mergedUser .getDept()!=null)
{
    Department dept = deptRepo.findOne(mergedUser .getDept().getDeptId());
    if(dept==null)
    ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM