Sunday, October 23, 2005

 

org.hibernate.StaleStateException: Batch update returned unexpected row count from update: 0 actual row count: 0 expected: 1

20:48:20,299 ERROR AbstractBatcher:61 - Exception executing batch:
org.hibernate.StaleStateException: Batch update returned unexpected row count from update: 0 actual row count: 0 expected: 1
at org.hibernate.jdbc.BatchingBatcher.checkRowCount(BatchingBatcher.java:93)
at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:79)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:193)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:230)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:296)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:877)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:344)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
20:48:20,314 ERROR AbstractFlushingEventListener:299 - Could not synchronize database state with session
org.hibernate.StaleStateException: Batch update returned unexpected row count from update: 0 actual row count: 0 expected: 1
at org.hibernate.jdbc.BatchingBatcher.checkRowCount(BatchingBatcher.java:93)
at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:79)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:193)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:230)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:296)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:877)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:344)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
20:48:20,320 ERROR GenericCarAgent:425 - Exception of unknown type caught at top level
org.hibernate.StaleStateException: Batch update returned unexpected row count from update: 0 actual row count: 0 expected: 1
at org.hibernate.jdbc.BatchingBatcher.checkRowCount(BatchingBatcher.java:93)
at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:79)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:193)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:230)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:296)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:877)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:344)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)
Exception in thread "main" org.hibernate.StaleStateException: Batch update returned unexpected row count from update: 0 actual row count: 0 expected: 1
at org.hibernate.jdbc.BatchingBatcher.checkRowCount(BatchingBatcher.java:93)
at org.hibernate.jdbc.BatchingBatcher.checkRowCounts(BatchingBatcher.java:79)
at org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:58)
at org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:193)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:230)
at org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:141)
at org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:296)
at org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
at org.hibernate.impl.SessionImpl.flush(SessionImpl.java:877)
at org.hibernate.impl.SessionImpl.managedFlush(SessionImpl.java:344)
at org.hibernate.transaction.JDBCTransaction.commit(JDBCTransaction.java:106)

Monday, September 26, 2005

 

Upgrading from Hibernate2 -> Hibernate3

I decided to upgrade to hibernate3 because of many reasons. One of the reasons was that Hibernate3 offers a cleaner API with less redundencies. Hibernate3 also supports delete and update support via Query interface.

1) Change packages in your source code as follows:
net.sf.hibernate -> org.hibernate
net.sf.hibernate.expression -> org.hibernate.criterion

2) If you have any log4j configuration file (typically this file is called log4j.properties and resides in the classes directory) has entries for the package net.sf.hibernate, change it to org.hibernate.

Monday, September 19, 2005

 

Postgresql transaction error

I am getting this server side error:

Caused by: org.postgresql.util.PSQLException: ERROR: current transaction is aborted, commands ignored until end of transaction block

at org.postgresql.util.PSQLException.parseServerError(PSQLException.java:139)
at org.postgresql.core.QueryExecutor.executeV3(QueryExecutor.java:156)
at org.postgresql.core.QueryExecutor.execute(QueryExecutor.java:101)
at org.postgresql.core.QueryExecutor.execute(QueryExecutor.java:43)
at org.postgresql.jdbc1.AbstractJdbc1Statement.execute(AbstractJdbc1Statement.java:517)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:50)
at org.postgresql.jdbc1.AbstractJdbc1Statement.executeQuery(AbstractJdbc1Statement.java:233)
at net.sf.hibernate.impl.BatcherImpl.getResultSet(BatcherImpl.java:87)
at net.sf.hibernate.loader.Loader.getResultSet(Loader.java:875)
at net.sf.hibernate.loader.Loader.doQuery(Loader.java:269)
at net.sf.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:133)
at net.sf.hibernate.loader.Loader.loadEntity(Loader.java:911)
at net.sf.hibernate.loader.Loader.loadEntity(Loader.java:931)
at net.sf.hibernate.loader.EntityLoader.load(EntityLoader.java:59)
at net.sf.hibernate.loader.EntityLoader.load(EntityLoader.java:51)
at net.sf.hibernate.persister.EntityPersister.load(EntityPersister.java:419)
... 47 more

Monday, June 27, 2005

 

Searching and indexing with Hibernate

The simple way to search with a small data set is to use the "where" clause in HQL or to use Query or Criteria objects.

However, as the dataset becomes larger and larger we want the text fields searchable by indexing them.

One way of making your hibernate data searchable is using Apache's Lucene framework.

http://lucene.apache.org/java/docs/index.html

Wednesday, June 08, 2005

 

XDoclets for Hibernate

Coding in hibernate involves writing Java bean-like code and them creating a schema to persist that object. The tedious part here is to keep the schema in sync with the bean definition.

There are two common approaches to solve this problem.

In the first approach, one writes the bean definition in hibernate xml and then uses to tool to auto generate the Java bean code.

The second approach is to write a java bean and then generate the hibernate xml from the bean itself.

I personally like the second approach because of several reasons.

1) You write the code in java, and not a proprietry xml. You can provide custom tweaks to the Java code that is not possible when you code in xml.

2) You can convert your Java code to multiple formats if needed. E.g. you could generate a hibernate xml and also any other proprietary conversion that you may want to.

XDoclets for Hibernate is an intuitive way to implement the second approach. All you need to do is put some special tags in you class and method javadoc comments (similar to @param and @returns). The conversion tool reads these comments and generates the hibernate xml.

Here's a sample code that illustrates this:

/** A class representing a car
*
* @hibernate.class table="cars"
**/
public class Car {
int id;

/** The getter method for this Customer's identifier.
*
* @hibernate.id generator-class="assigned"
**/
public int getId() {
return id;
}

public void setId(int id) {
this.id = id;
}

/**
* Gets the value of color
*
* @hibernate.property
* @return the value of color
*/
public String getColor() {
return this.color;
}

/**
* Sets the value of color
*
* @param argColor Value to assign to this.color
*/
public void setColor(String argColor) {
this.color = argColor;
}
}


The above can be used to generate a hibernate xml definition using the ant task 'generate.hibernate'. The generated xml can then be converted to a database schema using the ant task 'schemaexport'. The schema generated for the above class looks like this:

drop table cars;
create table cars (
id int4 not null,
color varchar(255),
);

Friday, June 03, 2005

 

Switching from JDBC to Hibernate

I started working with Hibernate 6 months ago. Soon I realized that with Hibernate I save 70% of the development time (mostly database code) and now I can't imagine going back to direct database access anytime soon.

This page is powered by Blogger. Isn't yours?