Hibernate version: 3
Name and version of the database you are using:Sql server 2000
Struts version: 1.2.7
Hi,
I'm building a simple webapplication with struts and hibernate and I'm trying to achieve the following behaviour:
When an object is saved it is first validated by Struts. You have great design patterns for this. However since I've already specified the size and required (yes/no) of each property in hibernate, I want to use this knowledge.
What I can do is try to save the object with Hibernate and catch a java.sql.DataTruncation exception if it occurs.
However, when I do this I can only report to the user that one of the properties is too long, but I don't know which property and what should be the size.
Is there any design pattern for this?
This is my code so far. this is the save methode of my generic Dao for saving an object.
Code:
public boolean save(Object obj) throws DaoException {
boolean result=false;
try{
Session s = Persistence.getSession();
Transaction tx = s.beginTransaction();
s.save(obj);
tx.commit();
result=true;
}
catch(Exception e){
Throwable cause = e.getCause();
if ((cause!=null) && (cause.getClass().equals(java.sql.DataTruncation.class))){
throw new DaoFieldFormatException("data truncation, "+cause.getMessage());
}else{
throw new DaoException("Error saving object "+obj.toString()+"\n"+e.getMessage());
}
}
return result;
}
[/code]