Hibernate在保存实体时抛出TransientPropertyValueException [英] Hibernate throws TransientPropertyValueException when saving an entity

查看:1875
本文介绍了Hibernate在保存实体时抛出TransientPropertyValueException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个相关的实体,机构和注册机构,因此注册和机构之间存在多对一的关系。因此,我们应该能够为单个机构注册登记。



实体和关系映射:

 @Entity 
public class Registration {

@NotNull @ManyToOne(可选= false)
@JoinColumn(updatable = false,insertable = false )
私立机构;

}


@实体
公共类机构{
@OneToMany(cascade = {CascadeType.REMOVE,CascadeType.MERGE}, fetch = FetchType.EAGER,mappedBy =Institution,orphanRemoval = true)
private设置<注册>注册;
}

和错误追踪可用 here

  [INFO] 04:34:26,043 WARN [org.hibernate.action.internal.UnresolvedEntityInsertActions](http-localhost / 127.0.0.1:8888-4)HHH000437:尝试保存一个或多个与未保存的瞬态实体具有不可空对应关系的实体。未保存的瞬态实体必须在保存这些从属实体之前保存在操作中。 
[INFO]未保存的瞬态实体:( [com.bitrunk.apps.nussa.client.shared.Institution#< null>))
[INFO]从属实体:( [[com.bitrunk。 apps.nussa.client.shared.Registration#< null>]])
[INFO]不可为空的关联:([com.bitrunk.apps.nussa.client.shared.Registration.institution] )
[INFO] 04:34:26,046错误[stderr](http-localhost / 127.0.0.1:8888-4)java.lang.IllegalStateException:org.hibernate.TransientPropertyValueException:Not-null属性引用瞬态值 - 在当前操作之前必须保存临时实例:

错误是:由org.hibernate引起。 TransientPropertyValueException:非空属性引用临时值 - 临时实例必须在当前操作之前保存:Registration.institution - > Institution



这很奇怪,因为在注册期间,用户使用下拉小部件从已经在数据库中的条目中选择他/她的机构例如Registration.institution应该指向DB中的一个对象,这让我想知道这个错误是什么以及如何解决它。



请给我一个像昨天一样修复。感谢所有。

解决方案

您当前的映射不会设置注册/机构关联的所有者。


  1. 如果您希望注册控制关联(最常见),那么这是您的映射:

      @Entity 
    公共类注册{

    @NotNull
    @ManyToOne(可选= false)
    私立机构机构;

    }


    @实体
    公共类机构{
    @OneToMany(cascade = {CascadeType.REMOVE,CascadeType.MERGE}, fetch = FetchType.EAGER,mappedBy =Institution,orphanRemoval = true)
    private设置<注册>注册;


  2. 如果您希望Institution控制关联)然后你的映射变成:

      @Entity 
    public class Registration {

    @NotNull
    @ManyToOne(可选= false)
    @JoinColumn(updatable = false,可插入= false)
    私立机构;

    }


    @实体
    公共类机构{
    @OneToMany(cascade = {CascadeType.REMOVE,CascadeType.MERGE}, fetch = FetchType.EAGER,orphanRemoval = true)
    private Set< Registration>注册;

    $ / code $ / pre
    $ b $ p $您不能同时设置( updatable = false insertable = false )和 mappedBy 因为那么这个关联的结束都不能控制关联,告诉Hibernate在将对象状态与数据库同步时要调查哪一方。


    I have two related entities, Institution and Registration, such that there is a many-to-one relationship between Registration and Institution. Thus we should be able to have registration entries for a single institution.

    The entities and relationship mapping:

    @Entity
    public class Registration{
    
        @NotNull @ManyToOne(optional=false)
            @JoinColumn(updatable=false, insertable=false)
            private Institution institution;
    
    }
    
    
    @Entity
    public class Institution{
        @OneToMany(cascade={CascadeType.REMOVE, CascadeType.MERGE}, fetch=FetchType.EAGER, mappedBy="institution", orphanRemoval=true)
            private Set<Registration> registrations;
    }
    

    and error trace is available here:

    [INFO] 04:34:26,043 WARN  [org.hibernate.action.internal.UnresolvedEntityInsertActions] (http-localhost/127.0.0.1:8888-4) HHH000437: Attempting to save one or more entities that have a non-nullable association with an unsaved transient entity. The unsaved transient entity must be saved in an operation prior to saving these dependent entities.
    [INFO]  Unsaved transient entity: ([com.bitrunk.apps.nussa.client.shared.Institution#<null>])
    [INFO]  Dependent entities: ([[com.bitrunk.apps.nussa.client.shared.Registration#<null>]])
    [INFO]  Non-nullable association(s): ([com.bitrunk.apps.nussa.client.shared.Registration.institution])
    [INFO] 04:34:26,046 ERROR [stderr] (http-localhost/127.0.0.1:8888-4) java.lang.IllegalStateException: org.hibernate.TransientPropertyValueException: Not-null property references a transient value - transient instance must be saved before current operation: 
    

    The error is : Caused by: org.hibernate.TransientPropertyValueException: Not-null property references a transient value - transient instance must be saved before current operation: Registration.institution -> Institution

    This is quite strange because during registration, the user selects his / her institution from entries already in the DB using a drop-down widget, such that Registration.institution is supposed to be pointing to an object from the DB which makes me wonder what this error is about and how to fix it.

    Please I need a fix like yesterday. Thanks all.

    解决方案

    Your current mappings doesn't set an owner of the Registration/Institution association.

    1. If you want the Registration to control the association (most common) then this is your mapping:

      @Entity
      public class Registration{
      
          @NotNull 
          @ManyToOne(optional=false)      
          private Institution institution;
      
      }
      
      
      @Entity
      public class Institution{
          @OneToMany(cascade={CascadeType.REMOVE, CascadeType.MERGE}, fetch=FetchType.EAGER, mappedBy="institution", orphanRemoval=true)
          private Set<Registration> registrations;
      }
      

    2. If you want the Institution to control the association (not that common) then your mapping becomes:

      @Entity
      public class Registration{
      
          @NotNull 
          @ManyToOne(optional=false)
          @JoinColumn(updatable=false, insertable=false)          
          private Institution institution;
      
      }
      
      
      @Entity
      public class Institution{
          @OneToMany(cascade={CascadeType.REMOVE, CascadeType.MERGE}, fetch=FetchType.EAGER, orphanRemoval=true)
          private Set<Registration> registrations;
      }
      

    You cannot set both (updatable=false, insertable=false) and mappedBy because then neither of this association ends can control the association, telling Hibernate which side to investigate when synchronizing the object state with the database.

    这篇关于Hibernate在保存实体时抛出TransientPropertyValueException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆