Read the rules before posting!
http://www.hibernate.org/ForumMailingli ... AskForHelp
Hibernate version: HibernateExt module from CVS as of Apr 18
Mapping documents: see below
Code between sessionFactory.openSession() and session.close(): Not applicable
Full stack trace of any exception that occurs: Not applicable
Name and version of the database you are using: SQLServer 2000
The generated SQL (show_sql=true): Not applicable
Debug level Hibernate log excerpt: Not applicable
I'm trying to set up a project with ant generating all the POJOs to one package, and me "hand-coding" sub-classes in another package, where each hand-coded class inherits from a generated POJO. This way I can separate generated code from hand-written business logic.
I reverse engineered the following hbm.xml file from an existing database (using the contents of JBossIDE-1.5M1-Hibernate-Tools.zip), and added a generated-class meta (I changed the package name to hide my client's name):
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping>
<!--
Auto-generated mapping file from
the hibernate.org cfg2hbm engine
-->
<class name="com.junk.model.ActionPage" table="action_page" schema="dbo" >
<meta attribute="generated-class">com.junk.model.generated.ActionPage</meta>
<composite-id name="id" class="com.junk.model.generated.ActionPageId">
<key-many-to-one name="RoleAction" class="com.junk.model.RoleAction">
<column name="action_id" scale="10" precision="0" not-null="false" />
</key-many-to-one>
<key-property name="Status" type="java.lang.String">
<column name="status" scale="30" precision="0" not-null="true" sql-type="varchar" />
</key-property>
</composite-id>
<property name="Page" type="java.lang.String">
<column name="page" scale="100" precision="0" not-null="true" sql-type="varchar" />
</property>
</class>
</hibernate-mapping>
I'm using the following ant target to generate the code
Code:
<target name="hibernate-codegen">
<taskdef name="hibernatetool" classname="org.hibernate.tool.ant.HibernateToolTask"/>
<hibernatetool destdir="${basedir}/topdown">
<configuration>
<fileset dir="${src}" id="id">
<include name="**/ActionPage.hbm.xml"/>
<include name="**/RoleAction.hbm.xml"/>
</fileset>
</configuration>
<hbm2java/>
</hibernatetool>
</target>
I was expecting the generator to produce three files: one for ActionPage, one for ActionPageId, and one for RoleAction. However, it produced only two: one called ActionPage.java which contained the fields for ActionPageId (not ActionPage), and one for RoleAction.
Stepping through the debugger, I isolated it to the following method in org.hibernate.tool.hbm2x.Cfg2JavaTool
Code:
public String getQualifiedDeclarationName(Component component) {
String generatedName = getMetaAsString(component, "generated-class");
if(generatedName==null || generatedName.trim().length()==0) {
generatedName = component.getComponentClassName();
}
generatedName = generatedName.replace('$', '.');
String qualifier = StringHelper.qualifier(component.getComponentClassName());
if("".equals(qualifier)) {
return qualifier + "." + generatedName;
} else {
return generatedName;
}
}
It seems that when the generator is writing the code for the composite ID, it picks up the generated-class attribute from the enclosing class entity. The generator overwrites ActionPage.java with a file of the same name, containing the fields from ActionPageId.
I tried adding a generated-class meta to the composite-id, but the generator concatenated the two generated-class attributes and created a whole new set of sub-packages.
Is there some other way to achieve what I want, or is there a bug in the above method?
Thanks,
Jon.