-->
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.  [ 13 posts ] 
Author Message
 Post subject: ***Data Validation in the mapping XML file***
PostPosted: Thu Jan 13, 2005 6:40 pm 
Newbie

Joined: Thu Jan 13, 2005 6:30 pm
Posts: 12
Is there a way i can define data basic validation rules in the mapping file.

for example:

<property name="attribute" validation="regular expression"/>

if there was a way to do it this way, i could indicate the required number of characters, first character should be alphabet etc. and let my validate() method work from here.

i know i can write rules in the validate() method or anywhere in the persistent class. but it will be nice to know if there is a better way of doing it.

i would greatly appreciate your suggestions/advise at the earliest. i am stuck.

i am fairly new to hibernate (using 2.1).

thanks in advance.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 13, 2005 6:46 pm 
Hibernate Team
Hibernate Team

Joined: Tue Sep 09, 2003 2:10 pm
Posts: 3246
Location: Passau, Germany
No there is no such thing.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 13, 2005 7:04 pm 
Newbie

Joined: Thu Jan 13, 2005 6:30 pm
Posts: 12
so how do you suggest i do data validation before save() ?

is there a hibernate or otherwise related best practice?

thanks very much.


Top
 Profile  
 
 Post subject: Suggestion...
PostPosted: Fri Jan 14, 2005 11:43 am 
Newbie

Joined: Thu Jan 13, 2005 6:30 pm
Posts: 12
this is for the developers of hibernate:

would you consider enhancing the property tag to add an attribute for data validation (something like shown below)???

<property name="attribute" validation="regular expression"/>


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 14, 2005 12:00 pm 
Hibernate Team
Hibernate Team

Joined: Mon Aug 25, 2003 9:11 pm
Posts: 4592
Location: Switzerland
Hibernate supports CHECK constraints on two levels in the mapping metadata. You can read these constraints with the Configuration API and initialize your presentation tier validation framework (or do something similar).


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 14, 2005 12:23 pm 
Newbie

Joined: Thu Jan 13, 2005 6:30 pm
Posts: 12
appreciate the quick reply.

wondering if you are referring to the [color=red]Database level constraints???

could you please elaborate. i would greatly appreciate it.[/color]


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 14, 2005 6:51 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
if you want something custom that your code can take and use whereever it wants then look into using <meta> tags to "annotate" your hbm.xml's.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Jan 14, 2005 10:01 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 7:19 pm
Posts: 2364
Location: Brisbane, Australia
As Max has indicated, I have used meta tags to determine validation rules and the code looks these up through the Configuration class and applies the validation processing appropriately.


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 17, 2005 8:00 pm 
Newbie

Joined: Thu Jan 13, 2005 6:30 pm
Posts: 12
thanks for your suggestion. i could use the meta tags.

i attempted to create a meta tag like this:

<property name="firstName">
<meta attribute="field-description">validation rule</meta>
</property>

is this what you suggest???

i am not having luck to read the value of this attribute in my code (validation rule).


please let me know how i can extract the field-description attribute (or if there is any other attribute i can use for this), in the CODE...


i got only this far... cant find a method to read the meta attribute values...

SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();

ClassMetadata meta = sessionFactory.getClassMetadata(StakeHolder.class) ;

String[] metaP = meta.getPropertyNames() ;


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 18, 2005 3:39 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
get it via Configuration - it is not available on the sessionfactory.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 18, 2005 12:44 pm 
Newbie

Joined: Thu Jan 13, 2005 6:30 pm
Posts: 12
thanks again.

i am still lost on how to read my hbm.xml file. i tried this:

new Configuration().addResource("StakeHolder.hbm.xml")

i would like to access the field-description in the code (in this case "first name column") :

<property name="firstName">
<meta attribute="field-description">first name column </meta>
</property>

OR is there another meta tag i can use, if so how?

i would greatly appreciate if you could give me a code snippet or atleast the classes and methods i can use to get this.

thanks a MILLION!!!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 18, 2005 2:20 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
look at Configuration API - look for getting a PersistentClass and on that you will have access to the properties and these will have getMetaAttribute

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Jan 18, 2005 5:44 pm 
Newbie

Joined: Thu Jan 13, 2005 6:30 pm
Posts: 12
AWESOME!!!


Thanks a lot for your help! i greatly appreciate it.

i used:
new Configuration().configure()
.getClassMapping(StakeHolder.class)
.getProperty("firstName")
.getMetaAttribute("field-description")
.getValue()

cheers!!!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 13 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.