File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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 Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Error in unique key " Watch "Error in unique key " New topic
Author

Error in unique key

anand raj.m
Greenhorn

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>
</action>

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

}catch (RuntimeException e) {
ErrorsAction errAction = new ErrorsAction();
String sError = errAction.getError(e);
addActionError(sError);
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
Sheriff

Joined: Aug 03, 2008
Posts: 9291
    
  17

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: http://aspose.com/file-tools
 
subject: Error in unique key
 
Similar Threads
Struts & Hibernate Integration unable to delete values from MYSQL database
Struts2 Action Not forwarding to a result
Validation error mesage is not display in browser
struts 2 validation error issue[instantiat object before returning input result]
Struts2.0 interceptor Problem