• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

unique key hibernate exception

 
Greenhorn
Posts: 9
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi professionals

I am using hibernate ,spring when i insert unique value at the time i got this exception
can any one give suggest how to handle this exception.


unique key hibernate exception

[size=18]



type Exception report

message

description The server encountered an inte rnal error () that prevented it from fulfilling this request.

exception

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: Could not execute JDBC batch update; nested exception is org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:488)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:431)
javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

root cause

org.springframework.dao.DataIntegrityViolationException: Could not execute JDBC batch update; nested exception is org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update
org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:622)
org.springframework.orm.hibernate3.HibernateAccessor.convertHibernateAccessException(HibernateAccessor.java:412)
org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:378)
org.springframework.orm.hibernate3.HibernateTemplate.save(HibernateTemplate.java:639)
com.percipient.comp.trending.store.SpringHibernateTrendingStore.save(SpringHibernateTrendingStore.java:440)
com.percipient.comp.trending.store.SpringHibernateTrendingStore.addInterval(SpringHibernateTrendingStore.java:69)
com.percipient.comp.trending.impl.TrendingImpl.createInterval(TrendingImpl.java:228)
com.percipient.sample.bo.BOTrend.createTrendInterval(BOTrend.java:213)
com.percipient.sample.controller.TrendController.createTrendInterval(TrendController.java:295)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
org.springframework.web.servlet.mvc.multiaction.MultiActionController.invokeNamedMethod(MultiActionController.java:473)
org.springframework.web.servlet.mvc.multiaction.MultiActionController.handleRequestInternal(MultiActionController.java:410)
org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:153)
org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:875)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:809)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:476)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:431)
javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)

root cause

java.sql.BatchUpdateException: Duplicate entry '9months' for key 2
com.mysql.jdbc.ServerPreparedStatement.executeBatch(ServerPreparedStatement.java:657)
org.hibernate.jdbc.BatchingBatcher.doExecuteBatch(BatchingBatcher.java:57)
org.hibernate.jdbc.AbstractBatcher.executeBatch(AbstractBatcher.java:174)
org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:226)
org.hibernate.engine.ActionQueue.executeActions(ActionQueue.java:136)
org.hibernate.event.def.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:274)
org.hibernate.event.def.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:27)
org.hibernate.impl.SessionImpl.flush(SessionImpl.java:730)
org.springframework.orm.hibernate3.HibernateAccessor.flushIfNecessary(HibernateAccessor.java:390)
org.springframework.orm.hibernate3.HibernateTemplate.execute(HibernateTemplate.java:374)
org.springframework.orm.hibernate3.HibernateTemplate.save(HibernateTemplate.java:639)
com.percipient.comp.trending.store.SpringHibernateTrendingStore.save(SpringHibernateTrendingStore.java:440)
com.percipient.comp.trending.store.SpringHibernateTrendingStore.addInterval(SpringHibernateTrendingStore.java:69)
com.percipient.comp.trending.impl.TrendingImpl.createInterval(TrendingImpl.java:228)
com.percipient.sample.bo.BOTrend.createTrendInterval(BOTrend.java:213)
com.percipient.sample.controller.TrendController.createTrendInterval(TrendController.java:295)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
java.lang.reflect.Method.invoke(Unknown Source)
org.springframework.web.servlet.mvc.multiaction.MultiActionController.invokeNamedMethod(MultiActionController.java:473)
org.springframework.web.servlet.mvc.multiaction.MultiActionController.handleRequestInternal(MultiActionController.java:410)
org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:153)
org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:875)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:809)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:476)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:431)
javax.servlet.http.HttpServlet.service(HttpServlet.java:689)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
[/size]
 
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sankar,
I think there is some mistake in the uniqueness, as the error clearly states: "Duplicate entry '9months' for key 2"
Try making sure that the 'key 2' is unique.
If you still have issues, try posting the mappings and the code that saves to DB.
 
sankar ramalingam
Greenhorn
Posts: 9
Eclipse IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Reehan

Thank you for your kind reply. what i am asking .If i enter the unique value in my jsp at the time i want display alert like "already exist". The above exception came from hibernate i want handle exception in hibernate,so how to handle help me.
 
Reehan Lalkhanwar
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Go to the method: com.percipient.comp.trending.store.SpringHibernateTrendingStore.save()
Wrap line 440 in try - catch block.
Catch the exception: org.hibernate.exception.ConstraintViolationException
If this is caught it means that a duplicate entry was being inserted and so the error was thrown.

Hope that helps.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Reehan Lalkhan wrote:Go to the method: com.percipient.comp.trending.store.SpringHibernateTrendingStore.save()
Wrap line 440 in try - catch block.
Catch the exception: org.hibernate.exception.ConstraintViolationException
If this is caught it means that a duplicate entry was being inserted and so the error was thrown.

Hope that helps.



Helped, indeed!
 
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes catching ConstraintViolationException helps but is there a way to display the key name instead of key2. So for example instead of saying
"Duplicate entry '9months' for key 2" can it display or is there a way to display "Duplicate entry '9months' for duration" assuming duration is the unique
key.

The bottom line is if there are more than one columns which uses constraint and say for that matter unique constraint is there a way to display the key name/column name of which the
constraint was violated?

I tried getConstraintName() on ConstraintViolationException but it returned null.
 
You firghten me terribly. I would like to go home now. Here, take this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic