[英]object references an unsaved transient instance - save the transient instance before flushing
I have two instances. 我有两个实例。 One is Project, the other is Module. 一个是项目,另一个是模块。 One to Many
relationship. One to Many
关系。
@Entity
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Table(name = "project", uniqueConstraints = { @UniqueConstraint(columnNames = { "name", "version" }) })
public class Project implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name = "name")
private String name;
@Column(name = "version")
private String projectVersion;
@Column(name = "description")
private String description;
@Column(name = "is_cartridge")
private boolean cartridge;
@Column(name = "manifest")
private String manifest;
@OneToMany(mappedBy = "project", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "project_id")
private Set<Module> modules = new HashSet<Module>();
@Entity
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
@Table(name = "module", uniqueConstraints = { @UniqueConstraint(columnNames = { "project_id", "name", "module_id" }) })
public class Module implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@Column(name = "name")
private String name;
@Column(name = "description")
private String description;
@Column(name = "module_id")
private String moduleId;
@Column(name = "version")
private String version;
@Column(name = "type")
private String type;
@Column(name = "resource")
private String resource;
@OneToOne
@JoinColumn(name = "default_language_id")
private Locality defaultLocality = new Locality();
@OneToOne(fetch = FetchType.EAGER)
@JoinColumn(name = "project_id")
private Project project = new Project();
@OneToMany(mappedBy = "module", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "module_id")
private Set<KeyValue> keyValues = new HashSet<KeyValue>();
I will set the modules into the project. 我将把模块设置到项目中。 and then use session.save(project)
. 然后使用session.save(project)
。 First time, because there is no record in the database, so the project and modules can be saved into the database. 第一次,因为数据库中没有记录,所以可以将项目和模块保存到数据库中。 However, the second time, because I don't need to create new project, I only create new modules and then set the modules back to the project. 但是,第二次,因为我不需要创建新项目,所以只创建新模块,然后将模块设置回项目。
An exception occurs. 发生异常 。
object references an unsaved transient instance - save the transient instance before flushing. 对象引用未保存的瞬态实例-在刷新之前保存瞬态实例。
@Transactional
public Project save(Project project) {
Session session = sessionFactory.openSession();
try {
Criteria criteria = session.createCriteria(Project.class).add(
Restrictions.eq("name", project.getName()));
List<Project> list = criteria.list();
if (list.size() == 0) {
Integer id = (Integer) session.save(project);
project = get(id);
return (project);
} else {
Project persistedModule = list.get(0).merge(project);
session.update(persistedModule);
session.flush(); **// Problem occurs in this line.**
return (persistedModule);
}
} finally {
session.close();
}
}
Is there any way to solve this problem? 有什么办法解决这个问题? Thank you. 谢谢。
Your mapping is wrong: 您的映射错误:
JoinColumn
annotation on the OneToMany. 因此,您必须在OneToMany上没有@ JoinColumn
批注。 You have repeated these errors for other associations, BTW. 您已对其他关联BTW重复了这些错误。
You're also calling update()
on a project (called persistedModule
, which makes things quite confusing) that is attached, since it's the result of a previous merge()
call. 您还要在附加的项目(称为persistedModule
,使事情变得很混乱update()
上调用update()
,因为它是先前merge()
调用的结果。 Updating an attached object is useless. 更新附加对象是没有用的。 It's already attached, so there's ,no reason to attach it again. 它已经被附加了,所以没有理由再次附加它。
I'm not sure if that will fix your problem, since it's hard to know what the actual problem is without the code and the stack trace, but I would start by fixing these issues 我不确定这是否可以解决您的问题,因为如果没有代码和堆栈跟踪信息就很难知道实际的问题是什么,但是我将从解决这些问题开始
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.