Home > Software engineering >  After Hibernate upgrade, the PropertyValueException is thrown
After Hibernate upgrade, the PropertyValueException is thrown

Time:08-22

after Hibernate upgrade 4.3.11.Final to 5.0.12.Final my test has stared failing with:

org.hibernate.PropertyValueException: not-null property references a null or transient value : com.mypackage.universal.model.MySetting.myOperation

That is thrown after attempt of saving MySetting without myOperation field. However,the myOperationField has Optional and Nullable set to true.

See the code below:

@Entity
@Table(name = "my_Settings")
public class MySetting {
 @Id
 private Long id;

 @ManyToOne(optional = true, fetch = FetchType.LAZY)
 @JoinColumn(name = "my_Operation_id", nullable = true)
 private MyOperation myOperation;

 @Column
 private String key;

 @Column
 private String value;
}

The piece that want to save MySetting:

    MySetting setting = new MySetting((Long) 1L, (String) myKey, (String) myValue);
    session.save(setting);

MySetting constructor:

    public MySetting(Long id, String key, String value) {
    this.id = id;
    setKey(key);
    setValue(value);
}

I am aware that I can disable null checking with:

<prop key="hibernate.check_nullability">false</prop>

But that is against the clean-code and will make handling the Entities harder. I've checked that this bug, also exist's on 5.1.17.Final version of Hibernate.

Do you have any ideas what is going on here? On 4.3.11.Final version there was no issue at all with this..

CodePudding user response:

It turned out that the root cause was in the implementation of MyOperation Enity for some reason. I don't fully understand why, but after changing wrongly used @ElementCollection to @OneToMany it started working. If someone knows why exactly the error had been risen, please let me know.

Before:

@ElementCollection(fetch = FetchType.LAZY)
@CollectionTable(name = "my_settings", joinColumns = @JoinColumn(name = "my_operation_id"))
@MapKeyColumn(name = "key")
@Column(name = "value")
private List<MySetting> mySettings;

After:

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "myOperation")
private List<MySetting> mySettings;
  • Related