File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Struts and the fly likes Error in unique key Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Error in unique key " Watch "Error in unique key " New topic

Error in unique key

anand raj.m

Joined: Nov 03, 2009
Posts: 5
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

Joined: Aug 03, 2008
Posts: 9466

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...

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
I agree. Here's the link:
subject: Error in unique key
It's not a secret anymore!