Spring Hibernate Errors and Solutions


Problem:
Hibernate Gives an Error for updating a column with a null value this column is a foreign key
   Entity X has a resource in it
    @JoinColumn(name = "resource_id", referencedColumnName = "id")
    @ManyToOne(optional = false)
    private Resource          resource;

  • not-null-property-references-a-null-or-transient-value
When the resource is null when we try to update in Hibernate 4.2.2 Final a error is given in Nullability.class

if ( checkability[i] && values[i]!= LazyPropertyInitializer.UNFETCHED_PROPERTY ) {
final Object value = values[i];
if ( !nullability[i] && value == null ) {

//check basic level one nullablilty
throw new PropertyValueException(
"not-null property references a null or transient value",
persister.getEntityName(),
persister.getPropertyNames()[i]
);

}
Solution: Set @ManyToOne(optional = true)
    @JoinColumn(name = "resource_id", referencedColumnName = "id")
    @ManyToOne(optional = true)
    private Resource          resource;


Problem: 
Hibernate does a select before updating entities and consumes memory for huge amount of data

Solution: Use @SelectBeforeUpdate(value = false)
+/**
+ * Should the entity's current state be selected from the database when determining whether to perform an update when
+ * re-attaching detached entities?
Here when update() called, the entity got attached again. This always issues an UPDATE statement. This happens irrespective of weather the object has changed after detaching or not. One way to avoid this UPDATE statement while reattaching is setting the select-before-update= “true”. If this is set, hibernates tries to determine if the UPDATE needed by executing the SELECT statement by setting it to "true"



Comments

Popular posts from this blog

API design best practices

DB Connection Issues

Reading Excel Sheet on client side and processing it