java Hibernate 在保存实体时抛出 TransientPropertyValueException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25072653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 07:17:08  来源:igfitidea点击:

Hibernate throws TransientPropertyValueException when saving an entity

javahibernateormentityhibernate-mapping

提问by Odili Charles Opute

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

错误是:引起:org.hibernate.TransientPropertyValueException:非空属性引用了一个瞬态值 - 在当前操作之前必须保存瞬态实例: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.

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

Please I need a fix like yesterday. Thanks all.

请我需要像昨天一样修复。谢谢大家。

回答by Vlad Mihalcea

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;
    }
    
  1. 如果您希望注册控制关联(最常见),那么这是您的映射:

    @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. 如果您希望机构控制关联(不那么常见),那么您的映射将变为:

    @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 mappedBybecause then neither of this association ends can control the association, telling Hibernate which side to investigate when synchronizing the object state with the database.

你不能同时设置 ( updatable=false, insertable=false) ,mappedBy因为这样关联的两端都不能控制关联,告诉 Hibernate 在将对象状态与数据库同步时要调查哪一边。

回答by Thiago Kronig

Double-check the Institutioninstances inside the drop-down widget: they might be copies of the fetched instances.

仔细检查Institution下拉小部件内的实例:它们可能是获取的实例的副本。