-->
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.  [ 5 posts ] 
Author Message
 Post subject: mapping java inheritance using hbm2java
PostPosted: Sat Sep 17, 2005 11:23 am 
Beginner
Beginner

Joined: Sat Sep 17, 2005 10:41 am
Posts: 49
Need help with Hibernate? Read this first:
http://www.hibernate.org/ForumMailingli ... AskForHelp

Hibernate version: 3.0.5

Hi...

My issue is related to the following, but going in reverse:

http://forum.hibernate.org/viewtopic.php?t=947457

(Preceding uses XDoclet, java -> hbm, I want to use hbm2java.)

Suppose you had a base class:

Code:
class Persistent implements java.io.Serializable
{
private String id ;
private Date createTime ;

public String getId () ;
public void setId (String id) ;
public Date getCreateTime () ;
public void setCreateTime (Date createTime) ;
}


And you have a subclass:

Code:
class FooBar extends Persistent
{
public String getFoo () ;
public void setFoo (String foo) ;
}


Suppose I want FooBar to map to the following table:
TABLE FooBar (
id varchar,
createTime timestamp,
foo varchar
)

The question is, how do I get hbm2java to generate the java properly?[/code]


Top
 Profile  
 
 Post subject:
PostPosted: Sat Sep 17, 2005 2:24 pm 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
if you use xdoclet why do you need hbm2java ?

but in any case - just map the class and add a <meta attribute="extends">Persistent</meta> to the mapping.

Then hbm2java will generate a class that extends Persistent

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 19, 2005 10:39 am 
Beginner
Beginner

Joined: Sat Sep 17, 2005 10:41 am
Posts: 49
Since you were curious, I'm actually in the process of evaluating hbm2java and xDoclet for use with a new project, so opinions on that debate are greatly appreciated.

As for the problem at hand, I have tried the "extends" meta-attribute. The problem is that hbm2java re-generates the setters and getters for the sub-class. Here's what I have:

Persistent.java (hand-coded)
Code:
public class Persistent   {
private Long id ;
private Date createTime ;
public Long getId() {return this.id ;}
public void setId(Long id) { this.id = id ; }
public Date getCreateTime() { return this.createTime ; }
public void setCreateTime(Date createTime) { this.createTime = createTime ; }
}   


Contact.hbm.xml (hand-coded xml snippet)
Code:
  <class name="Contact">
    <meta attribute="extends">Persistent</meta>
    <id name="id" type="long">
      <generator class="native"/>
    </id>
    <property name="createTime" type="date" />

    <property name="firstName"    type="string" length="32"  />
    <property name="lastName"     type="string" length="32"  />
    <property name="companyName"  type="string" length="32"  />
    <property name="address1"     type="string" length="128" />
    <property name="address2"     type="string" length="128" />
    <property name="city"         type="string" length="128" />
    <property name="postalCode"   type="string" length="32"  />

    <many-to-one name="state"   class="State"   />
    <many-to-one name="country" class="Country" />

    <property name="email"        type="string" length="64"  />
    <property name="workPhone"    type="string" length="32"  />
    <property name="homePhone"    type="string" length="32"  />
    <property name="mobilePhone"  type="string" length="32"  />
    <property name="otherPhone"   type="string" length="32"  />
  </class>

Contact.java (generated by hbm2java)
Code:
/**
* Contact generated by hbm2java
*/
public class Contact extends Persistent implements java.io.Serializable {

    // Fields

     private Long id;
     private Date createTime;
     private String firstName;
     private String lastName;
     private String companyName;
     private String address1;
     private String address2;
     private String city;
     private String postalCode;
     private State state;
     private Country country;
     private String email;
     private String workPhone;
     private String homePhone;
     private String mobilePhone;
     private String otherPhone;


    // Constructors

    /** default constructor */
    public Contact() {
    }

    /** constructor with id */
    public Contact(Long id) {
        this.id = id;
    }

   // Property accessors

    /**
     *
     */
    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    /**
     *
     */
    public Date getCreateTime() {
        return this.createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    /**
     *
     */
    public String getFirstName() {
        return this.firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

(... snip ...)
}   


As you can see, hbm2java generated code that shadows the base class' id and createTime properties. I suppose I could change the Persistent class and declare getId/setId, etc. to be abstract methods, but this effectively prevents me from coding property-specific behavior.

I could also take out the following lines from Contact.hbm.xml:
Code:
    <id name="id" type="long">
      <generator class="native"/>
    </id>
    <property name="createTime" type="date" />


And this would result in the java code that I want. The problem is that I would not have the database mapping that I wanted. (I want id and createTime to be part of the Contact table.)

Are there other ways to manipulate hbm2java output? (I'd rather not resort to tweaking the Hibernate source, or even "pojo.vm".) Or, should I declare the base class properties (set/get id and createTime) abstract? Or should I go with XDoclet?


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 19, 2005 10:48 am 
Hibernate Team
Hibernate Team

Joined: Tue Aug 26, 2003 6:10 am
Posts: 8615
Location: Neuchatel, Switzerland (Danish)
this is the usecase for <meta attribute="generate">false</meta> which we dont have implemented yet, but is on the roadmap.

_________________
Max
Don't forget to rate


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 20, 2005 11:59 am 
Regular
Regular

Joined: Tue Nov 29, 2005 12:31 pm
Posts: 75
Hi,

Is it now implemented ?

Thanks.


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