-->
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.  [ 6 posts ] 
Author Message
 Post subject: Table-per-subclass code-generation
PostPosted: Thu Sep 09, 2004 1:57 pm 
Newbie

Joined: Wed Mar 24, 2004 7:40 pm
Posts: 18
Hibernate version:
2.1.2, tools 2.0.1 and 2.1.6, tools 2.1.2

Name and version of the database you are using:
Oracle 9i

I am trying to create a mapping that has a single base class, a subclass, and a sub-subclass, using the table-per-subclass mapping strategy. (I know this strategy is not recommended, but this is legacy data, so I have no other choice.)

Below is a quote from gavin in another post which says that I should be able to accomplish this:
Quote:
There is, of course, no limit to the depth of inheritance in Hibernate.


However, I encounter the problem that there is no way to map the sub-subclass without getting a MappingException at code-generation time. Say C inherits from B inherits from from A. When code generating C, I get the following error even if B generates successfully

Exception:
net.sf.hibernate.MappingException: Cannot extend unmapped class com.twcaustin.intranet.myforms.model.B

Mapping documents:
These mappings represent a simplest-case for the problem I have experienced in my project.

A.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping package="com.twcaustin.intranet.myforms.model">
    <class name="com.twcaustin.intranet.myforms.model.A" table="A">
        <id
            name="id"
            type="java.lang.Long"
            column="ID"
            unsaved-value="null"
            access="property">
            <generator class="sequence">
                <param name="sequence">seq_ID</param>
            </generator>
        </id>
        <version name="name" type="string" column="name" unsaved-value="null" />
    </class>
</hibernate-mapping>


B.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping package="com.twcaustin.intranet.myforms.model">
   <joined-subclass name="com.twcaustin.intranet.myforms.model.B"
       extends="com.twcaustin.intranet.myforms.model.A"
       table="B" >
       <key column="ID" foreign-key="id" />
       <property name="title" column="TITLE" type="string" />
   </joined-subclass>
</hibernate-mapping>


C.hbm.xml
Code:
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
   "-//Hibernate/Hibernate Mapping DTD 2.0//EN"
   "http://hibernate.sourceforge.net/hibernate-mapping-2.0.dtd">
<hibernate-mapping package="com.twcaustin.intranet.myforms.model">
   <joined-subclass name="com.twcaustin.intranet.myforms.model.C"
       extends="com.twcaustin.intranet.myforms.model.B"
       table="C" >
       <key column="ID" foreign-key="id" />
       <property name="ln" column="LN" type="string" />
   </joined-subclass>
</hibernate-mapping>



I am using an ANT task that looks like this, which I have been using successfully to generate other Hibernate classes for some time:
Code:
<java classname="net.sf.hibernate.tool.hbm2java.CodeGenerator" fork="true">
  <classpath refid="hibernate.classpath" />
  <classpath location="${hbmdata.jar}" />
  <arg value="--output=${gensrc}" />
  <arg line="${hbm.files}" />
</java>



Here is the complete stack trace for the error given by the generator:

Code:
[java] net.sf.hibernate.MappingException: Cannot extend unmapped class com.twcaustin.intranet.myforms.model.B
[java]     at net.sf.hibernate.tool.hbm2java.CodeGenerator.handleClass(CodeGenerator.java:137)
[java]     at net.sf.hibernate.tool.hbm2java.CodeGenerator.main(CodeGenerator.java:109)


I have found this error in the JIRA and it says it was fixed 22/Jul/04 05:12 PM.

http://opensource.atlassian.com/projects/hibernate/browse/HB-1097

Once I found that, I upgraded from hibernate 2.1.2/tools 2.0.1 to hibernate 2.1.6/tools 2.1.2. This should have included the fix, but I still get the error.

Additionally, the JIRA bug says that the posted patch.txt does not actually fix the problem.

Additionally, I do not seem to be able to put the base class in one JAR file, and then import that JAR in my ant task to generate a the subclasses. I have not been able to find a JIRA bug listing for this problem.

Is there any known workaround for these issues, or is there a build that is known to have these fixes?

I am sorry if this is the wrong place to post this, but I feel like I have done due dilligence through all other sources. I am getting desperate, because I don't know how to finish my project without this capability and don't know where else to turn.

Thank you in advance for any assistance you can provide.
DW


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 09, 2004 4:38 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
I guess your problem is that you need to list the hbm.xml files in the proper order! A before B ;)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 10, 2004 2:59 pm 
Newbie

Joined: Wed Mar 24, 2004 7:40 pm
Posts: 18
Quote:
I guess your problem is that you need to list the hbm.xml files in the proper order! A before B ;)


I am afraid I dont understand what that means, or which order you are talking about. Do you mean the order in the hibernate.cfg.xml file? Here is my sample of that:

Code:
<mapping resource="A.hbm.xml"/>
<mapping resource="B.hbm.xml"/>
<mapping resource="C.hbm.xml"/>


Do you mean in the build file? I feel that would be strange since the generator program seems to be able map classes in order. If, for instance, you have a bi-directional one-to-one relationship, it does not get confused how to generate both classes, even though each needs the definition for the other.

So if it is just an order, that would be great, but what order?

Thanks,
Dann Webster


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 10, 2004 4:21 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
it's the sequence of hbm.xml files passed on to the hbm2java in the build file.

hbm2java can easy generate references to mappings it doesn't know yet as it only needs the classname - but for resovling the extends it needs to know the superclass before - so just add them in order (as per the docs ;)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject: Not seeing how to use correct order...can't find docs
PostPosted: Wed Sep 22, 2004 8:07 pm 
Newbie

Joined: Wed Mar 24, 2004 7:40 pm
Posts: 18
I have finally gotten back to this issue after using a workaround for several weeks.

I cannot find any documentation that mentions how to use joined-subclass mapping files with hbm2java.

Almost the only thing that comes up under google for "hbm2java joined-subclass" is the aforementioned Jira bug, and the only thing that comes up under the site search is this, which does not provide any useful info:

http://www.hibernate.org/70.html

I have attempted to make the ANT file put the files in the correct order using the following two build tasks:

Code:
    <target name="gen1" depends="hbmbase" description="Generate .java from .hbm files.">
        <hbm2java config="${src}/hibernate.cfg.xml" output="${gensrc}">
            <fileset dir="${src}">
                <include name="**/A.hbm.xml"/>
                <include name="**/B.hbm.xml"/>
                <include name="**/C.hbm.xml"/>
            </fileset>
        </hbm2java>
    </target>


Code:
    <target name="gen2" depends="hbmbase" description="Generate .java from .hbm files.">
        <hbm2java config="${src}/hibernate.cfg.xml" output="${gensrc}">
            <fileset dir="${src}">
                <include name="**/A.hbm.xml"/>
            </fileset>
        </hbm2java>
        <hbm2java config="${src}/hibernate.cfg.xml" output="${gensrc}">
            <fileset dir="${src}">
                <include name="**/B.hbm.xml"/>
            </fileset>
        </hbm2java>
        <hbm2java config="${src}/hibernate.cfg.xml" output="${gensrc}">
            <fileset dir="${src}">
                <include name="**/C.hbm.xml"/>
            </fileset>
        </hbm2java>
    </target>


Both of these give the same error as before using both tools 2.0.2 and 2.1.2


Is there another way to do this?

Thanks,
DW


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 23, 2004 1:37 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
ALL the mapping files have to be present!

have you used the cvs version ?
Have you checked with ant -debug what sequence it is actually sending into hbm2java ?
can you show the complete stack trace ?

_________________
Max
Don't forget to rate


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