Hibernate throws TransientPropertyValueException when saving an entity

拥有回忆 提交于 2019-12-11 00:27:37

问题


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.


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/25072653/hibernate-throws-transientpropertyvalueexception-when-saving-an-entity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!