-->
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.  [ 4 posts ] 
Author Message
 Post subject: Shared constant for hibernate property size and HTML maxlen
PostPosted: Wed Jul 20, 2005 8:31 pm 
Newbie

Joined: Wed Jul 20, 2005 7:46 pm
Posts: 4
Location: Vancouver Canada
I think this is pretty cool. But thi s is my first Hibernate/Struts project so feel free to critique away... or to suggest somewhere else this should be posted. Or maybe this is a well-known pattern already?

The Problem:
A typical project will have some text strings that are entered using HTML <input> form fields and then written to a database using Hibernate via a framework such as Struts. The maximum string length may be specified in the HTML (<input maxlength=...), in the DDL, in the xxx.hbm.xml, and possibly in a validation rule. Using XDoclet and hbm2ddl, and no Struts length validation rule, the size is in both the XDoclet comments and in the HTML.

It is easy to make the error of changing a constant in only one of the two places. This may not show up as a problem until a user enters a long string and sees an exception if the database field is smaller.

Wouldn't it be nice to specify each property length in just one place?

The Solution:
This assumes Struts, but may be adaptable for other purposes.

The length is in the XDoclet annotation only:
Code:
   /**
      * @hibernate.property
      * length="40"
      * non-null="true"
      */
    public String getCountry() { ...


This generates XML as usual (or you may handcode the XML, depending on your methodology):
Code:
<property
            name="country"
            type="java.lang.String"
            column="country"
            length="40" />


Now, use a simple stylesheet (size.xsl) to generate a resource properties file from the XML:

Code:
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text" encoding="utf-8"/>
<xsl:template match="class">
<xsl:for-each select="property">
size.<xsl:value-of select='@name'/>=<xsl:value-of select='@length'/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>


Ant has XSLT support built-in so it is easy to generate the .properties file as part of the build:

Code:
     <target name="xsl" depends="xdoclet">
      <xslt basedir="build/xdoclet/" destdir="build/"
                       includes="*.hbm.xml"     style="size.xsl">
            <mapper type="glob" from="*.hbm.xml" to="*.properties"/>
      </xslt>
   </target>


Then in Struts, add all the generated .properties files (aka bundles), one for each class, in struts-config.xml:

Code:
  <message-resources key="class1" parameter="Class1" null="false"/>
  <message-resources key="class2" parameter="Class2" null="false"/>


In the JSP page, set a variable to the property name (e.g. ${field.name}), then using that extract the size from the appropriate message resource bundle:
Code:
   <c:set var="s">
     <bean:message key="size.${field.name}" bundle="class1" />
   </c:set>
   <html:text property="${field.name}" size="${s}" maxlength="${s}" />


(Am I correct that JSTL's <fmt:message> won't accept an expression? That's why I used <bean:message>.)

Works for me! I hope someone finds this useful.

_________________
Software Engineering Consultant
www.skahasoftware.com


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 20, 2005 8:34 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
You should check out Hibernate Validator (part of the Hibernate Annotations package).

JDK5 only, however.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 20, 2005 8:50 pm 
Newbie

Joined: Wed Jul 20, 2005 7:46 pm
Posts: 4
Location: Vancouver Canada
Interesting (not to be confused as I was with Struts Validator)...
It is not obvious to me from the documentation whether I could use the Hibernate Annotations API to create custom JSP tags for extracting size, min, max, etc for use in the HTML. If so, that could eliminate the need for the XSL. Otherwise, it does seem Hibernate Validator provides more annotations (max, min) that would allow my approach to be taken further than just string lengths.

gavin wrote:
You should check out Hibernate Validator (part of the Hibernate Annotations package).
JDK5 only, however.

_________________
Software Engineering Consultant
www.skahasoftware.com


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jul 20, 2005 9:08 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 12:50 pm
Posts: 5130
Location: Melbourne, Australia
Um. Depends whether you are trying to generate values statically into the JSP text, or whether you just want to be able to access the value at runtime in your JSP.

Runtime access is easy via reflection API. Static might be more difficult.


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