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
. 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)
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
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
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)
...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.