简体   繁体   English

对象引用未保存的瞬态实例-在刷新之前保存瞬态实例

[英]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: 您的映射错误:

  • you have a OneToMany on one side, and a OneToOne on the other side. 您的一侧是OneToMany,另一侧是OneToOne。 It should be OneToMany and ManyToOne. 应该是OneToMany和ManyToOne。
  • you're saying the the OneToMany is mapped by the project field in module, but you also specify a join column. 您说的是OneToMany由模块中的项目字段映射,但是您还指定了一个连接列。 If it's mapped by product, then product defines how the association is mapped. 如果按产品映射,则product定义如何映射关联。 So you must not have a @ 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.

相关问题 对象引用一个未保存的瞬态实例,在刷新之前保存瞬态实例: - Object references an unsaved transient instance save the transient instance before flushing: 使用 Hibernate 保存 object object 引用未保存的瞬态实例 在刷新之前保存瞬态实例 - save the object using Hibernate object references an unsaved transient instance save the transient instance before flushing 对象引用了一个未保存的瞬态实例-在刷新之前保存瞬态实例:Spring Data JPA - object references an unsaved transient instance - save the transient instance before flushing : Spring Data JPA 如何解决对象引用未保存的瞬态实例的错误-在刷新之前保存瞬态实例? - How do I solve this error of object references an unsaved transient instance - save the transient instance before flushing? 对象引用未保存的瞬态实例 - 在刷新之前保存瞬态实例:com.entity.Role - object references an unsaved transient instance - save the transient instance before flushing: com.entity.Role TransientObjectException:对象引用了一个未保存的临时实例-在执行合并时在刷新之前保存该临时实例 - TransientObjectException: object references an unsaved transient instance - save the transient instance before flushing when I am doing merge TransientObjectException - 对象引用未保存的瞬态实例 - 在刷新之前保存瞬态实例 - TransientObjectException - object references an unsaved transient instance - save the transient instance before flushing 对象引用未保存的瞬态实例-在刷新之前保存瞬态实例(而Fetch Query JPA) - object references an unsaved transient instance - save the transient instance before flushing( while Fetch Query JPA) 对象引用了一个未保存的临时实例-使用休眠空间在刷新之前保存该临时实例 - object references an unsaved transient instance - save the transient instance before flushing using hibernate spatial 域表出现错误“对象引用了未保存的瞬态实例-在刷新之前保存瞬态实例” - Domain table getting error “object references an unsaved transient instance - save the transient instance before flushing”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM