-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 
Author Message
 Post subject: generic validation mechanism
PostPosted: Thu Aug 04, 2005 11:13 am 
Beginner
Beginner

Joined: Tue Jan 18, 2005 9:37 am
Posts: 29
Location: The Netherlands
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]


Top
 Profile  
 
 Post subject: Validation
PostPosted: Fri Aug 05, 2005 11:33 am 
Expert
Expert

Joined: Fri Jul 22, 2005 2:42 pm
Posts: 670
Location: Seattle, WA
I do not think that accessing Hibernate configuration will do much good ( although it is possible somehow with ClassMetadata meta = session.getSessionFactory().getClassMetadata( PersistentClass.class) ).

For example lets look at the Date field:
We can get information from H that out field is Date:
meta.getPropertyType( “propName”) but it gives us no clue what format of data we want to use for the representation;

Same is about an string field: it that field is supposed to hold email, then we have to check for email format at the business level.


My point is this:
- even we could use some information from Hibernate configuration file, we still need to compliment it with some additional information;
- in theory Hibernate based persistence could work behind EJB or CORBA or SOAP interface and UI application might not have access to the H configuration at all.

Possible solution:
1. Use XDoclet based or Java5 based annotations on the actual business classes and derive necessary configuration files ( H mappings and Validation rules) from it.
Something like /* @validation min-length=”10” format-regexp=”[0-9]2/[0-9]2/[0-9]4”

2. Use XSLT to parse *.hbm.xml –s and write some parts of validation rules as you want them to be for your application;


Top
 Profile  
 
 Post subject:
PostPosted: Sun Aug 07, 2005 5:56 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
http://www.hibernate.org/hib_docs/annotations/reference/en/html_single/#validator

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 3 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.