Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Error in unique key  RSS feed

anand raj.m
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,
I have set few Fields in my DB tables as unique Keys am using Struts2,Hibernate and MySql. I want Data in few tables as Unique For Eg, Country i have a Table called Country and i dont want the Name of the Country to be Repeated for that i made the CountryName Field as Unique.

- I didnt made any changes in my hibernate.
- This is the Code i have used in the Struts.xml file
<action name="insertCountry" method="insertCountry" class="CountryAction">
<exception-mapping exception="java.lang.Exception" result="input"/>
<result name="success" type="chain">/jsp/countryjsp</result>
<result name="input">/jsp/countryForm.jsp</result>

- This is the code in the Action class
public insert Country{
insert function code goes here........

}catch (RuntimeException e) {
ErrorsAction errAction = new ErrorsAction();
String sError = errAction.getError(e);
throw e;
} return SUCCESS;

- This ia the ErrorAction class
public class ErrorsAction extends ActionSupport{
public String getError(RuntimeException e){
String sError = e.getCause().getLocalizedMessage();
return sError;

- Now the Error wont show in front end and the data wont insert also ,but i want to show some user Understandable msg in front end.
- I have to Show some User Understandable Msg Or is there any other way to catch this exception

- Or is there any other way to use Unique key in Hibernate and catch the Exception

- Please help

Thanks in Advanced
Ankit Garg
Posts: 9610
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I personally would not let an exception to come. I would first find if there's already a country with the name in the database, and if there is, then I would return the input mapping. I think there's no message shown on the JSP page because you are thrown an exception after setting the action error which might remove the action error (not sure). You can add the action error and then return an input mapping instead of throwing an exception, that must solve your problem.

Also please Use Code Tags when you post a source code. You can edit your message using button and then add code tags to it...
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!