Hibernate Exceptions Errors and Causes

The following section will list the general exception that we get while dealing with hibernate.



org.hibernate.ObjectDeletedException:deleted object would be re-saved by cascade
when we try to delete the child object and don't remove the association, for instance we loaded a parent object with list of child and then we deleted one of the

child object but did not removed the child from the parent.

Example,

Box gotBox =(Box)dao.load(Box.class, box.getBoxId());

List boxitem = new ArrayList(gotBox.getBoxItems());

BoxItem deleteItem = boxitem.get(0);

dao.deletItem(deleteItem.getItemName());

Solution is, remove the transient instance from the parent object(Box) like.
 

gotBox.getBoxItems().remove(deleteItem);

OR use
 

@Cascade(value=org.hibernate.annotations.CascadeType.DELETE_ORPHAN)

on the child object.

-------------------------------------------------------------------------------------------------


No Dialect mapping for JDBC type: -1


We get this exception when trying to execute SQLQuery in hibernate and the query returns columns value that Hibernate not able to identify(map to its own data type, for example MySQL Data type Text.

CREATE TABLE `country`
(`countryId` INT  NOT NULL,`countryName` TEXT  NOT NULL) 
 
If we execute the following 
SQLQuery query = session.createSQLQuery("select * from country");    
countries = query.list(); 
We will get the exception.
The solution is to add scalar to query object.
like,
query.addScalar("countryId", Hibernate.LONG);
query.addScalar("countryName", Hibernate.STRING);   
Without adding Scalars, hibernate uses ResultSetmetaData to resolve the data types.
 
 
 

Comments

  1. Hi dude,

    In case of any database exception e.g. if some record doesn't exists in db ,does hibernate delivers that error till application level or there is any default way of handling that ?

    Thanks
    Javin
    FIX Protocol tutorial

    ReplyDelete

Post a Comment

Popular posts from this blog

Difference between volatile and synchronized

Converting Java Map to String

Masking Credit Card number in Java