From: Francois J. (JIRA) <no...@at...> - 2006-07-31 21:26:13
|
[ http://opensource.atlassian.com/projects/hibernate/browse/HBX-716?pag= e=3Dcomments#action_23809 ]=20 Francois Jean commented on HBX-716: ----------------------------------- Just to be sure to do the right thing. Presently the pojo generation will generate the class OR the interface but = not both at the same time. So the patch would generate the constant string in an Interface while gener= ating the pojo class. It will also add the constant String in the interface= if we specify <meta attribute=3D"interface">true</meta> in the xxx.hbm.xml= file. Is it what you want? > Trick to help maintenance of HQL query. > --------------------------------------- > > Key: HBX-716 > URL: http://opensource.atlassian.com/projects/hibernate/browse/H= BX-716 > Project: Hibernate Tools > Type: Improvement > Components: hbm2java > Versions: 3.2beta6 > Reporter: Francois Jean > Priority: Minor > Attachments: patch.txt > > > I'm just sending a modification I made to a FreeMarket template in Hibern= ate Tools (hbm2java) to help us maintaining our HQL. > We are using a large database with more the 300 tables and we often do a = reverse-engineering of the database to keep our Java entities in synch and = sometimes we have to deal with column names change. After the reverse-engin= eering process, the compiler will show errors for all the getter/setter tha= t have change name. But it will not show any errors in HQL using "old" attr= ibute names. > For example if we have a table "Application" with a column "Version_num",= we would get a Class "Application" with an attribute "versionNum" and its = own getter and setter. If the column "Version_num" change its name for "App= _version_num", the new attribute name will be "appVersionNum" with its new = getter/setter. And the compiler will show errors for old code using the get= ter "getVersionNum". But will not show any errors for HQL like: "from " +Ap= plication.class.getName() + " where versionNum =3D 3" > Solution: > We modify the generation of the entity to include a constant string for e= ach attribute name, by using these constants the compiler can flag error in= HQL. 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 t= rigger the constants generation by using a parameter in the ant task when a= sking Hibernate Tools to generate Java entities. > Template that was modified: PojoFields.ftl > // Fields name constant > =20 > <#foreach field in pojo.getAllPropertiesIterator()><#if pojo.getMetaAttri= bAsBool(field, "gen-property", true)> > public static final String ${field.name?upper_case} =3D "${field.nam= e}"; > </#if> > </#foreach> > =20 > // Fields =20 > <#foreach field in pojo.getAllPropertiesIterator()><#if pojo.getMetaAttri= bAsBool(field, "gen-property", true)> <#if pojo.hasMetaAttribute(field, "fi= eld-description")> /** > ${pojo.getFieldJavaDoc(field, 0)} > */ > </#if> ${pojo.getFieldModifiers(field)} ${pojo.getJavaTypeName(field,= jdk5)} ${field.name}<#if pojo.hasFieldInitializor(field, jdk5)> =3D ${pojo= .getFieldInitialization(field, jdk5)}</#if>; > </#if> > </#foreach> > PS: This solution is not perfect, a better approach would be something th= at would also validate at compilation time the type of association. For exa= mple, the association "person.address.city" present in a HQL statement will= only 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 |