|
From: Francois J. (JIRA) <no...@at...> - 2006-07-29 14:33:13
|
Trick to help maintenance of HQL query.
---------------------------------------
Key: HBX-716
URL: http://opensource.atlassian.com/projects/hibernate/browse/HBX=
-716
Project: Hibernate Tools
Type: Improvement
Components: hbm2java =20
Versions: 3.2beta6 =20
Reporter: Francois Jean
Priority: Minor
Attachments: patch.txt
I'm just sending a modification I made to a FreeMarket template in Hibernat=
e Tools (hbm2java) to help us maintaining our HQL.
We are using a large database with more the 300 tables and we often do a re=
verse-engineering of the database to keep our Java entities in synch and so=
metimes we have to deal with column names change. After the reverse-enginee=
ring process, the compiler will show errors for all the getter/setter that =
have change name. But it will not show any errors in HQL using "old" attrib=
ute names.
For example if we have a table "Application" with a column "Version_num", w=
e would get a Class "Application" with an attribute "versionNum" and its ow=
n getter and setter. If the column "Version_num" change its name for "App_v=
ersion_num", the new attribute name will be "appVersionNum" with its new ge=
tter/setter. And the compiler will show errors for old code using the gette=
r "getVersionNum". But will not show any errors for HQL like: "from " +Appl=
ication.class.getName() + " where versionNum =3D 3"
Solution:
We modify the generation of the entity to include a constant string for eac=
h attribute name, by using these constants the compiler can flag error in H=
QL. The new HQL would be:
"from " +Application.class.getName() + " where "+ Application.VERSIONNUM + =
" =3D 3"
For now, we always generate these constants but it could be possible to tri=
gger the constants generation by using a parameter in the ant task when ask=
ing Hibernate Tools to generate Java entities.
Template that was modified: PojoFields.ftl
// Fields name constant
=20
<#foreach field in pojo.getAllPropertiesIterator()><#if pojo.getMetaAttribA=
sBool(field, "gen-property", true)>
public static final String ${field.name?upper_case} =3D "${field.name}=
";
</#if>
</#foreach>
=20
// Fields =20
<#foreach field in pojo.getAllPropertiesIterator()><#if pojo.getMetaAttribA=
sBool(field, "gen-property", true)> <#if pojo.hasMetaAttribute(field, "fiel=
d-description")> /**
${pojo.getFieldJavaDoc(field, 0)}
*/
</#if> ${pojo.getFieldModifiers(field)} ${pojo.getJavaTypeName(field, j=
dk5)} ${field.name}<#if pojo.hasFieldInitializor(field, jdk5)> =3D ${pojo.g=
etFieldInitialization(field, jdk5)}</#if>;
</#if>
</#foreach>
PS: This solution is not perfect, a better approach would be something that=
would also validate at compilation time the type of association. For examp=
le, the association "person.address.city" present in a HQL statement will o=
nly be validated when this HQL will be executed.
Fran=C3=A7ois J.
--=20
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators:
http://opensource.atlassian.com/projects/hibernate/secure/Administrators=
.jspa
-
For more information on JIRA, see:
http://www.atlassian.com/software/jira
|