org.hibernate.AssertionFailure: null id in entry (don’t flush the Session after an exception occurs)

I’m  inserting multiple records to MySQL with Hibernate 5.

After a constraint failure, all the records are failed to get inserted with the error ‘org.hibernate.AssertionFailure: null id in entry (don’t flush the Session after an exception occurs)’. To get rid of this problem, I cleared the hibernate session when the exception occurs with session.clear().

Data is getting pumped without any problem now.

Cd1bUvDUUAEo8M4.jpg large

Advertisements

Ticketing management / support desk management with struts, spring and hibernate

Here are the screenshots I promised in my earlier post.

image

Report of the tickets-more filters to be added

image

Details of a ticket and the provision to add the follow-ups if any.

image

Historic report of the followups for the selected ticket.

image

Screen to add a ticket

Delivery 1 is completed yesterday. Lets wait for next release for more features.. Stat tuned!

ஜெய்ஹிந்த்
Posted from WordPress for Android

Ticketing management on SDLC with struts, spring and hibernate

Here are tge screenshots I promised in my earlier post.

image

Report of the tickets-more filters to be added

image

Details of a ticket and the provision to add the follow-ups if any.

image

Historic report of the followups for the selected ticket.

image

Screen to add a ticket

Delivery 1 is completes yesterday. Lets wait for next release for more features.. Stat tuned!

ஜெய்ஹிந்த்
Posted from WordPress for Android

Illegal attempt to associate a collection with two open sessions

Illegal attempt to associate a collection with two open sessions

When we are working with collections with spring and hibernate, you may end up with this exception while saving an object. Googling says this is a feature/bug of hibernate – hibernate is not versioning the collections as it does for other objects. Don’t know what it is in detail, but I should solve this right now. The suggested solution is to have  single session. The following the code that saves my object.

public class BaseDAO extends HibernateDaoSupport implements IBaseDAO {
.....
/* (non-Javadoc)
	 * @see com.eginnovations.license.dao.IBaseDAO#save(java.lang.Object)
	 */
	@Override
	@Transactional(propagation = Propagation.REQUIRED, readOnly=false)
	public Object save(Object entity){
		Session session = super.getSession();
		Serializable save = session.save(entity);
		logger.debug("save:"+save.getClass()+" "+save+" "+entity);
		return save;
	}

..............
}

New session are getting opened in super.getsession() and session.save().

The problem is I have not implemented OpenSessionInViewFilter till now, so I don’t have the option to have the single session. So here is the entries added to the deployment descriptor web.xml.

	<filter>
		<filter-name>openSessionInViewFilter</filter-name>
		<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
		<init-param>
			<param-name>singleSession</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>

And this is the filter mapping.

<filter-mapping>
		<filter-name>openSessionInViewFilter</filter-name>
		<url-pattern>/*</url-pattern>
		<dispatcher>REQUEST</dispatcher>
		<dispatcher>FORWARD</dispatcher>
		<dispatcher>ERROR</dispatcher>
	</filter-mapping>

Go ahead!

org.hibernate.LazyInitializationException: could not initialize proxy – no Session

I have an object X which has a many-to-one relationship with Object Y. I could see it is working fine in the beginning, but it ended up with problems after some days in production.

org.hibernate.LazyInitializationException: could not initialize proxy - no Session

  • I collect these X objects in a list in my model.
  • I iterate it with Struts tag in view, where I could see this error in log file.

The session I used in model got closed before I iterate the list in UI. The solution was given here. http://stackoverflow.com/questions/345705/hibernate-lazyinitializationexception-could-not-initialize-proxy

Changing the relationship as lazy=false in HBM mapping solves this issue. Make sure it is getting inserted and deleted  properly.

a different object with the same identifier value was already associated with the session

This is purely a Hibernate problem. I have one to many relationship between tables. Mapping table c contains the relations between table a and table b. When I try to save a new object in a, which has a set of b objects (All those b objects are already existing), I got an Hibernate exception saying

a different object with the same identifier value was already  associated with the session:
[org.grassfield.common.entity.User#4];  nested exception is org.hibernate.NonUniqueObjectException:
a different  object with the same identifier value was already associated with the  session:
[org.grassfield.common.entity.User#4]

Seems to be quiet common issue for one to many or many to many relationship in hibernate. Need to look at it tomorrow.

Finally I found the table I have created to store the many to many relation is goofy. I removed the id column (autoincrement and PK). It resolved the issue. finally I have only two columns to store the relations.

Done!

—-

Added on 13 Aug 2010

Making cascade=none solved this in another situation.

<many-to-one name=”oldLicense” column=”OLD_LICENSE_ID” cascade=”none” lazy=”false” not-null=”false”/>

JDBCExceptionReporter:77 – SQL Error: 0, SQLState: 08S01

I have a scheduled job running with spring beans. It is scheduled such that it will run once in a day. Whenever I restart the webapp, it is running for the first day. but on the second day onwards, it is not running – quits with the below exception 😦 Seems to be an issue with DB connectivity. Need to look at it.

00:54:28,090  WARN JDBCExceptionReporter:77 - SQL Error: 0, SQLState: 08S01
00:54:28,090 ERROR JDBCExceptionReporter:78 - Communications link failure due to underlying exception:

** BEGIN NESTED EXCEPTION **

java.net.SocketException
MESSAGE: Software caused connection abort: socket write error

STACKTRACE:

java.net.SocketException: Software caused connection abort: socket write error
 at java.net.SocketOutputStream.socketWrite0(Native Method)
 at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
 at java.net.SocketOutputStream.write(SocketOutputStream.java:136)
 at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:65)
 at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:123)
 at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:2744)
 at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1612)
 at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)
 at com.mysql.jdbc.Connection.execSQL(Connection.java:3283)
 at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1332)
 at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1467)
 at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:93)
 at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:186)
 at org.hibernate.loader.Loader.getResultSet(Loader.java:1787)
 at org.hibernate.loader.Loader.doQuery(Loader.java:674)
 at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
 at org.hibernate.loader.Loader.doList(Loader.java:2220)
 at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)
 at org.hibernate.loader.Loader.list(Loader.java:2099)
 at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:94)
 at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1569)
 at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)
 at com.eginnovations.license.dao.LicenseDAO.listExpiry(LicenseDAO.java:106)
 at com.eginnovations.license.service.LicenseService.listExpiry(LicenseService.java:291)
 at com.eginnovations.license.jobs.ExpiryTimer.run(ExpiryTimer.java:85)
 at java.util.TimerThread.mainLoop(Timer.java:512)
 at java.util.TimerThread.run(Timer.java:462)

** END NESTED EXCEPTION **

Last packet sent to the server was 15 ms ago.
00:54:28,090 ERROR ExpiryTimer:95 - Error in expiry job
org.hibernate.exception.JDBCConnectionException: could not execute query
 at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:74)
 at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
 at org.hibernate.loader.Loader.doList(Loader.java:2223)
 at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)
 at org.hibernate.loader.Loader.list(Loader.java:2099)
 at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:94)
 at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1569)
 at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)
 at com.eginnovations.license.dao.LicenseDAO.listExpiry(LicenseDAO.java:106)
 at com.eginnovations.license.service.LicenseService.listExpiry(LicenseService.java:291)
 at com.eginnovations.license.jobs.ExpiryTimer.run(ExpiryTimer.java:85)
 at java.util.TimerThread.mainLoop(Timer.java:512)
 at java.util.TimerThread.run(Timer.java:462)
Caused by: com.mysql.jdbc.CommunicationsException: Communications link failure due to underlying exception:

** BEGIN NESTED EXCEPTION **

java.net.SocketException
MESSAGE: Software caused connection abort: socket write error

STACKTRACE:

java.net.SocketException: Software caused connection abort: socket write error
 at java.net.SocketOutputStream.socketWrite0(Native Method)
 at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
 at java.net.SocketOutputStream.write(SocketOutputStream.java:136)
 at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:65)
 at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:123)
 at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:2744)
 at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1612)
 at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)
 at com.mysql.jdbc.Connection.execSQL(Connection.java:3283)
 at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1332)
 at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1467)
 at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:93)
 at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:186)
 at org.hibernate.loader.Loader.getResultSet(Loader.java:1787)
 at org.hibernate.loader.Loader.doQuery(Loader.java:674)
 at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
 at org.hibernate.loader.Loader.doList(Loader.java:2220)
 at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)
 at org.hibernate.loader.Loader.list(Loader.java:2099)
 at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:94)
 at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1569)
 at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:283)
 at com.eginnovations.license.dao.LicenseDAO.listExpiry(LicenseDAO.java:106)
 at com.eginnovations.license.service.LicenseService.listExpiry(LicenseService.java:291)
 at com.eginnovations.license.jobs.ExpiryTimer.run(ExpiryTimer.java:85)
 at java.util.TimerThread.mainLoop(Timer.java:512)
 at java.util.TimerThread.run(Timer.java:462)

** END NESTED EXCEPTION **

Last packet sent to the server was 15 ms ago.
 at com.mysql.jdbc.MysqlIO.send(MysqlIO.java:2759)
 at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1612)
 at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1723)
 at com.mysql.jdbc.Connection.execSQL(Connection.java:3283)
 at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1332)
 at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1467)
 at org.apache.commons.dbcp.DelegatingPreparedStatement.executeQuery(DelegatingPreparedStatement.java:93)
 at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:186)
 at org.hibernate.loader.Loader.getResultSet(Loader.java:1787)
 at org.hibernate.loader.Loader.doQuery(Loader.java:674)
 at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
 at org.hibernate.loader.Loader.doList(Loader.java:2220)
 ... 10 more