Odili Charles Opute
Odili Charles Opute

Reputation: 331

Hibernate throws TransientPropertyValueException when saving an entity

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.

Upvotes: 2

Views: 15454

Answers (2)

Vlad Mihalcea
Vlad Mihalcea

Reputation: 154190

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.

Upvotes: 1

Thiago Kronig
Thiago Kronig

Reputation: 176

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

Upvotes: 0

Related Questions