-->
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: Subclassing entity classes generated by hbm2java
PostPosted: Wed May 10, 2006 7:59 am 
Newbie

Joined: Wed May 10, 2006 6:03 am
Posts: 3
Hi,

we are using Hibernate 3.1.1 and Hibernate Tools 3.1.0.beta5 to generate entity classes out of our mapping files.

What we would like to do is to create unmapped subclasses of the generated entity classes, to implement further entity specific methods which cannot be created by the generic hbm2java task.

Is it possible to use these subclasses instead of the generated entity classes for all CRUD operations, without creating additional (subclass) mappings?

In particular, would something like this

session.load(CustomerSpecial.class, idValue);

work for an unmapped subclass CustomerSpecial of the mapped class Customer? Does it return a Customer or a CustomerSpecial instance?

Best regards,

Rico Starke


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 11, 2006 5:10 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
is

Code:
<class name="Something">
<meta attribute="generated-class">BaseSomething</meta>
...


not enough for you for this ?

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject: Problems with generated-class
PostPosted: Thu May 11, 2006 9:07 am 
Newbie

Joined: Wed May 10, 2006 6:03 am
Posts: 3
Hi Max,

thank you for hinting to the generated-class meta attribute! It works fine as long as the mapping files do not contain any subclass relationships.

A mapping like the following example causes trouble:
Code:
<hibernate-mapping>

  <class name="package.EntityName" table="A_TABLE">
    <meta attribute="generated-class">package.EntityNameBase</meta>
    ...

    <discriminator column="DISCR" type="string" length="1"/>     

    <!-- Subclasses -->
    <subclass name="package.EntityNameSub1" discriminator-value="i">
      <meta attribute="generated-class">package.EntityNameSub1Base</meta>
     ...             
    </subclass>
    <subclass name="package.EntityNameSub2" discriminator-value="i">
      <meta attribute="generated-class">package.EntityNameSub2Base</meta>
     ...             
    </subclass>                          
  </class>
</hibernate-mapping>


The Class EntityNameBase is created correctly, but the subclasses are not created as files EntityNameSub1Base.java and EntityNameSub2Base.java. The contents of both subclasses appear to share the same physical file instead, and its file name is weird and contains parts of the package declarations. I hope you get what I mean; I can provide screenshots (for example) if it helps.

But we have another issue with the generated-class meta attribute with regard to inheritance and the manual creation of entity subclasses which contain additional methods. In the above example, we would like the following classes and inheritance relationships to be created by the generator:

EntityNameBase
EntityNameSub1Base extends EntityName (NOT extends EntityNameBase)
EntityNameSub2Base extends EntityName (NOT extends EntityNameBase)

where EntityName is the manually created subclass of EntityNameBase, i.e.:
EntityName extends EntityNameBase
EntityNameSub1 extends EntityNameSub1Base
EntityNameSub2 extends EntityNameSub2Base

If the generator creates "extends EntityNameBase" instead of "extends EntityName", the inheritance hierarchy is destroyed in the manually created subclasses (those without the Base name suffix). That means, EntityName would not be a superclass of EntityNameSub1 and EntityNameSub2.
I hope you get the idea although English is not my native language :-)

Regards,

Rico Starke


Top
 Profile  
 
 Post subject:
PostPosted: Thu May 11, 2006 9:30 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
add an inherit="false" to the meta tag and it will not happen or alternatively don't nest the subclass inside the class.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject: Thank you
PostPosted: Thu May 11, 2006 10:40 am 
Newbie

Joined: Wed May 10, 2006 6:03 am
Posts: 3
Thanks a lot for your helpful answers, inherit=false does exactly what we need!!

Perhaps it would be a reasonable default (or at least a recommendation in the docs) for the generated-class attribute.


Top
 Profile  
 
 Post subject:
PostPosted: Fri May 12, 2006 1:09 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
the core doesn't know about the semantics of the meta attributes.

2 "solutions":
1) document it
2) only use the first meta attribute found in this case

_________________
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.