-->
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.  [ 16 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Generate hbm2java abstract-concrete class pairs?
PostPosted: Tue Oct 04, 2005 2:14 pm 
Newbie

Joined: Tue Oct 04, 2005 1:20 pm
Posts: 16
Location: The Netherlands
Hibernate Tools version: 3.1 alpha 5

Hello,

I'd like to generate abstract-concrete class pairs with hbm2java (ala MyEclipse wizard) as follows:

Code:
abstract class AbstractThing {// getters & setters}
public class Thing extends AbstractThing {// empty}


Chapter 15.2.1 of the Hibernate 2.x reference guide explains that this can be accomplished with following element in the configuration file:

Code:
<codegen>
  <generate prefix="Base" renderer="net.sf.hibernate.tool.hbm2java.BasicRenderer"/>
  <generate renderer="net.sf.hibernate.tool.hbm2java.BasicRenderer">
    <param name="generate-concrete-empty-classes">true</param>
    <param name="baseclass-prefix">Base</param>
  </generate>
</codegen>


However, the codegen element is not allowed in the Hibernate 3 config & mapping DTDs. Furthermore I can't find any documentation on this feature in the Hibernate 3 reference guide or the Hibernate 3 Ant Tools docs.

Do you know how I can generate abstract-concrete class pairs with the current hbm2java? If not, what are my alternatives?

Thanks in advance!

Kind regards,
Bas[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 05, 2005 1:18 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
Per class: add a <meta attribute="generated">AbstractXXX</meta>

"All classes at once" is not in there at the moment

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 05, 2005 4:25 am 
Newbie

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

I tried your solution, but the class in my meta tag is not generated. My mapping file reads as follows:

Code:
<?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.obj">
   <class name="VerblijfType">
      <meta attribute="generated">AbstractVerblijfType</meta>
      <id name="id" type="java.lang.Integer">
         <generator class="native" />
      </id>
      <property name="type" not-null="true" type="string" />
   </class>
</hibernate-mapping>


Am I doing something wrong?

Thanks,
Bas


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 05, 2005 4:40 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
ah sorry - its generated-class my fault.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 05, 2005 6:25 am 
Newbie

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

I added the meta tag to a mapping that contains a joined subclass, but then hbm2java terminated with the following error:

Code:
ERROR [org.hibernate.util.XMLHelper] - Error parsing XML: XML InputStream(27) The content of element type "class" must match "(meta*,subselect?, ... ,filter*,resultset*,(query|sql-query)*)"


My mapping reads as follows:

Code:
<?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="SuperClazz">
      <id name="id" type="java.lang.Long">
         <generator class="native" />
      </id>
      <meta attribute="generated-class">
         nl.anwb.happi.acc.model.BaseSuperClazz
      </meta>
      <joined-subclass name="SubClazz">
         <meta attribute="generated-class">
            nl.anwb.happi.acc.model.BaseSubClazz
         </meta>
         <key column="id" />
      </joined-subclass>
   </class>
</hibernate-mapping>


What am I doing wrong?

Thanks,
Bas


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 05, 2005 7:29 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
meta attribute needs to be before id as specified by the dtd

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 05, 2005 8:38 am 
Newbie

Joined: Tue Oct 04, 2005 1:20 pm
Posts: 16
Location: The Netherlands
Oops, sorry! BTW:
Code:
<meta attribute="scope-class">public abstract</meta>
did the trick! Thanks for your help, Max.

Bas


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 10, 2005 8:27 am 
Newbie

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

When I generate a class with a different name using the generate-class meta attribute and another class has a reference to that class then the following error occurs:

Code:
org.hibernate.tool.hbm2x.ExporterException: Error while processing template the second time


Any ideas?

Thanks,
Bas


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 10, 2005 8:41 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
i now tried this 3-4 times with a clean wtp 0.7.1 installation and i see no entry in the error logs and everything works fine. ...

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 13, 2005 4:46 am 
Newbie

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

Could you try to reproduce the hbm2java error

Code:
build.xml: ... : org.hibernate.tool.hbm2x.ExporterException: Error while processing template the second time


with the following hbm.xml file:

Code:
<?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="Contigent" table="contigent">
      <meta attribute="generated-class">
         nl.anwb.happi.acc.model.BaseContigent
      </meta>
      <meta attribute="scope-class">public abstract</meta>
      <id type="java.lang.Integer" column="id" name="id">
         <generator class="native"></generator>
      </id>
      <property name="ingangsDatum" type="java.util.Calendar"
         not-null="true" />
      <property name="einddatum" type="java.util.Calendar"
         not-null="true" />
      <property name="aantal" type="java.lang.Integer"
         not-null="true" />
      <property name="indicatieBeschikbaarheid" not-null="true"
         type="boolean" />
   </class>
</hibernate-mapping>


Thanks!
Bas


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 13, 2005 10:58 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
eh - did you try and look in the stacktrace ?

It clearly shows that your classname has spaces in it thus don't put them there ;)

(and add a request for enhancement in jira to trim that string)

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 14, 2005 3:42 am 
Newbie

Joined: Tue Oct 04, 2005 1:20 pm
Posts: 16
Location: The Netherlands
That was the problem! I'll file an issue for that. BTW: Where did you get that stacktrace, I haven't seen one on my console. Thanks again Max!


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 14, 2005 4:33 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
double click on the line and it should contain a stacktrace with a cause

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Sun Jan 18, 2009 12:17 pm 
Newbie

Joined: Thu Jan 15, 2009 10:58 am
Posts: 1
Hello max,

I also want to generate 2 classes as follow:
/*
abstract class AbstractThing // my existing class
public class Thing extends AbstractThing // to-be-generated class
*/

So, I create the mapping file:

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>
    <class name="User" table="USER">
       <meta attribute="generated-class">AbstractEntity</meta>
        <id name="Id" type="int">
            <column name="ID" />
            <generator class="assigned" />
        </id>
        <property name="userName" type="string">
            <column name="USER_NAME" />
        </property>
        <property name="password" type="string">
            <column name="PASSWORD" />
        </property>
    </class>
</hibernate-mapping>

and I expect hibernate tool will generate something like:

Code:
public class User extends AbstractEntity {
   private int Id;
   private String userName;
   private String password;

   public User() {}

   public User(int Id) {
      this.Id = Id;
   }

   public User(int Id, String userName, String password) {
      this.Id = Id;
      this.userName = userName;
      this.password = password;
   }

   public int getId() {
      return this.Id;
   }

   public void setId(int Id) {
      this.Id = Id;
   }

   public String getUserName() {
      return this.userName;
   }

   public void setUserName(String userName) {
      this.userName = userName;
   }

   public String getPassword() {
      return this.password;
   }

   public void setPassword(String password) {
      this.password = password;
   }

   @Override
   public String toString() {
      // TODO Auto-generated method stub
      return super.toString() + Id + "/" + userName;
   }
}


And here is my AbstractEntity class:

Code:
public abstract class AbstractEntity implements Serializable {

   public static final int NEW_ENTITY_IDENITY = 0;

   // Technical identity
   private int _id = NEW_ENTITY_IDENITY;

   // Real entity type
   private Class<? extends Object> _entityType;

   // Constructor
   protected AbstractEntity(){

      // Returns the instance class of entity. E.g. User, Country, etc.
      _entityType = this.getClass();

      // TODO: need to check it with hibernate again
      // when it's a proxy class of target entity. E.g. User00000Proxy -> User
      if (_entityType.getName().contains("Proxy"))
         _entityType = _entityType.getSuperclass();
   }

   // Override methods
   public String toString() {
      return _entityType.getName() + "/" + String.valueOf(_id);
   }

   public boolean equals(Object obj){
      // The same pointer
      if (this == obj)
         return true; // no need to check more

      // it's undefined or not a derived class of AbstractEntity
      if (obj == null || !(obj instanceof AbstractEntity))
         return false;

      // check entity type, and its identity
      AbstractEntity target = (AbstractEntity)obj;
      if (_entityType != target._entityType || _id != target._id)
         return false;

      return true;
   }

   public int hashCode(){
      // return the same hash code for entities which has same entity type, and same entity identity
      return toString().hashCode();
   }

   public Object cloneEntity() throws CloneNotSupportedException {
      // Clone fields with primitive type
      AbstractEntity cloneEntity = (AbstractEntity)super.clone();

      // Reset identity
      cloneEntity._id = 0; // never clone Identity, it will be a new entity

      // TODO: How to know composition associations to clone them?

      return cloneEntity;
   }
}


but hibernate tool doesn't generate what I expect.

Do you have any idea about how to solve my problem?

Thank you very much


Top
 Profile  
 
 Post subject:
PostPosted: Mon Jan 19, 2009 5:51 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
"generated-class" controls what we generate.

You are probably looking for using "extends" instead and then have "gen-property" set to false for those things you don't want to overwrite.

If that is not enough then you need to start tweaking the templates.

_________________
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.  [ 16 posts ]  Go to page 1, 2  Next

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.