-->
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.  [ 13 posts ] 
Author Message
 Post subject: hbm2java interface generation issue
PostPosted: Fri Oct 21, 2005 4:11 am 
Newbie

Joined: Tue Oct 04, 2005 1:20 pm
Posts: 16
Location: The Netherlands
Hibernate version: 3.1-rc1

Mapping documents:
<?xml version="1.0" encoding='UTF-8'?>
<!DOCTYPE hibernate-mapping PUBLIC
"-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
<hibernate-mapping package="nl.anwb.happi.acc.model">
<class name="Relatie" table="relatie">
<meta attribute="interface">true</meta>
<meta attribute="generated-class">nl.anwb.happi.acc.model.hibernate.HibernateRelatie</meta>
<id name="id" type="java.lang.Long">
<generator class="native" />
</id>
<joined-subclass name="Accommodatiehouder"
table="accommodatiehouder">
<meta attribute="interface">true</meta>
<meta attribute="generated-class">nl.anwb.happi.acc.model.hibernate.HibernateRelatie</meta>
<key column="id" />
</joined-subclass>
</class>
</hibernate-mapping>

Description:
When I run hbm2java I expect the following interface hierarchy:
Code:
package nl.anwb.happi.acc.model.hibernate;
interface HibernateRelatie {...}

package nl.anwb.happi.acc.model.hibernate;
interface HibernateAccommodatiehouder extends HibernateRelatie {...}


However, hbm2java generates a faulty Relatie interface instead of the Accommodatiehouder:
Code:
package nl.anwb.happi.acc.model.hibernate.HibernateRelatienl.anwb.happi.acc.model.hibernate.HibernateRelatienl.anwb.happi.acc.model.hibernate;

public interface HibernateRelatie extends nl.anwb.happi.acc.model.Relatie {...}


Is this a bug or do I use the wrong metatags?

Kind regards,
Bas


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 21, 2005 4:39 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
hmm seems we have some bad logic about appending class/package names when using generated-class.

please put this in jira.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 21, 2005 8:12 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
I didnt notice this at first glance but you have a couple of errors.

first "generated-class" should have inherit="false" otherwise it will be appended to the <joined-subclass>

second you list the same generated-class name for <class> and <joined-subclass> ...that does not make sense. They should be separate.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 21, 2005 10:13 am 
Newbie

Joined: Tue Oct 04, 2005 1:20 pm
Posts: 16
Location: The Netherlands
Hello Max,

Excuse me for the typo. I added inherit=false to the "generated-class" meta tag and the generated interface is almost right. It only extends the super-interface from the wrong package:

Code:
public interface HibernateAccommodatiehouder extends nl.anwb.happi.acc.model.Relatie  {...}

Expected:
Code:
public interface HibernateAccommodatiehouder extends nl.anwb.happi.acc.model.hibernate.Relatie  {...}

BTW: public is a redundant modifier (according to CS).

Thanks again,
Bas


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 21, 2005 10:25 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
well - somewhere your interface/class should implement the mapped entities.

generated-class is to allow you to write a subclass that extends the generated-class ...subclasses of that should extend your generated class.

and yes public is a redundant modifier, but I hate interfaces that is not explicit about their "public and staticness" :)

i think you need to have some separate templates and use those to solve your generation issues (which looks very specific/weird from my pov ;)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 22, 2005 8:27 am 
Newbie

Joined: Tue Oct 04, 2005 1:20 pm
Posts: 16
Location: The Netherlands
Hello Max,

Perhaps it's interesting to explain why I chose this (rare) generation strategy. Currently I'm working on a project which is bound to the following criterion.

Quote:
The definition of all domain classes and their relational counterparts is strictly maintained in the Hibermate mapping documents.


My first approach was to generate domain classes with the default hbm2java settings. However, when behaviour was added to the domain classes and something changed later on in the mappings document, so that the domain classes had to be re-generated, the added behaviour was overridden.

To overcome this issue I decided to generate an abstract class hierarchy so that behaviour could be added to their implementation. At first this seemed an excellent separation of persistent and inferred behaviour. However, I couldn't implement inheritence. An example:

Code:
abstract class AbstractSuperClazz { // generated }
abstract class AbstractSubClazz { // generated }

class SuperClazz extends AbstractSuperClazz { // added behaviour }
class SubClazz extends ?


My current strategy is to generate a full interface hierarchy and to implement all behaviour by hand. For example:

Code:
interface ISuperClazz { // generated }
interface ISubClazz { // generated }

class SuperClazz implements ISuperClazz { ... }
class SubClazz extends SuperClazz implements ISubClazz { ... }


Please let me know if you have any suggestions!

Kind regards,
Bas


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 22, 2005 8:37 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
why dont you *just* use "generated-class" ? this is exactly the usecase it is there for!

no need for interfaces etc.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 22, 2005 10:54 am 
Newbie

Joined: Tue Oct 04, 2005 1:20 pm
Posts: 16
Location: The Netherlands
Because my teammembers want to add behaviour to the generated classes and they want to keep the class hierarchy in-sync with those classes. You can't have both without multiple inheritance as I illustrated with the abstract class example above.

I realise that my strategy is far from optimal and I'm open for any alternatives.

Kind regards,
Bas


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 22, 2005 11:08 am 
Hibernate Team
Hibernate Team

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

B extends A is mapped in a hbm.xml

normally that would create

class A {}
class B extends A {}

if you adds generated-class meta attribute to each of these,e.g. BaseA and BaseB the following will be created:

class BaseA {}
class BaseB extends A {}

Meaning that you write the A and B class which look like:

class A extends BaseA {}
class B extends BaseB {}

There is *no* multiinheritance here and you mapped stuff is kept in sync and developers can add their custom stuff.

beaware that this model has an advantage (from my POV) that you only need to add generated-class to those classes when you actually needed and the hiearchy/dependencies will still be in sync.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 22, 2005 9:19 pm 
Newbie

Joined: Tue Oct 04, 2005 1:20 pm
Posts: 16
Location: The Netherlands
Quote:
if you adds generated-class meta attribute to each of these,e.g. BaseA and BaseB the following will be created:

class BaseA {}
class BaseB extends A {}

Meaning that you write the A and B class which look like:

class A extends BaseA {}
class B extends BaseB {}


OK, here comes the problem: if you add method m() to class A then A.m() is not overloaded to class B. Losing this OO principle is unfortunately not an option.

Kind regards,
Bas


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 23, 2005 3:20 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
huuh ? why is A.m() not overloaded to class B ?

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 23, 2005 8:20 am 
Newbie

Joined: Tue Oct 04, 2005 1:20 pm
Posts: 16
Location: The Netherlands
Oh, I see!! So the class hierarchy is:

B -> BaseB -> A -> BaseA

If I generate BaseA and BaseB with the generated-class meta-tag, how do I force BaseB extends A?

Thanks for your smart thinking!

Bas


Top
 Profile  
 
 Post subject:
PostPosted: Sun Oct 23, 2005 9:19 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
Quote:
If I generate BaseA and BaseB with the generated-class meta-tag, how do I force BaseB extends A?


That is the whole trick about generated-class! It only tells that it should generate another class with another name - it does not affect how other classes refer to it; which make it usable for just your scenario!

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