-->
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.  [ 8 posts ] 
Author Message
 Post subject: Does hibernate with Xdoclet support Inheritance
PostPosted: Mon Sep 12, 2005 10:22 am 
Newbie

Joined: Mon Jan 10, 2005 9:16 am
Posts: 18
Hello,

I'm new in useing hibernate and generate the Mappingfiles with Xdoclet. But I'm not sure how to describe the inherited attributes of an a superclass in a subclass. There are no inherited attributes automatically generated in the mapping file. But I added the XDoclet instructions for all attributes in the super- and in the subclass.

How does this work normaly? Have to do anything else?


Regards

Lars


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 12, 2005 11:26 am 
Red Hat Associate
Red Hat Associate

Joined: Mon Aug 16, 2004 11:14 am
Posts: 253
Location: Raleigh, NC
My base classes are usually not mapped as entities, but they serve to avoid duplicated work. As such, there is no @hibernate.class on them. The properties that I want to show up in my subclasses (and in the schema for EACH subclass's table) do have @hibernate.property tags.

Because the actual entities extend the base class, XDoclet generates the appropriate mappings in the .hbm.xml file from the base classes in each entity that extends one. I make no mention of the base class's properties in the subclasses. XDoclet is aware of the class hierarchy.

In the example below, the COUPON table gets a DATECREATED property automatically.

Hope this helps. Don't forget to rate.

-Chris

Base Class:
Code:
public abstract class AbstractModel implements Serializable {
    protected Date dateCreated = null;

    /**
     * @hibernate.property column="DATECREATED" insert="false" update="false"
     */
    public Date getDateCreated() {
        return dateCreated;
    }


Subclass:
Code:
/**
* @hibernate.class table="COUPON"
*/
public class Coupon extends AbstractModel {
    private Boolean enabled = Boolean.FALSE;

    /**
     * @return <code>true</code> if this Coupon is enabled
     *
     * @hibernate.property column="ENABLEDFLAG"
     */
    public Boolean getEnabled() {
        return enabled;
    }

    public void setEnabled(Boolean enabled) {
        this.enabled = enabled;
    }

}


Top
 Profile  
 
 Post subject: Mapping Strategie
PostPosted: Mon Sep 12, 2005 11:57 am 
Newbie

Joined: Mon Jan 10, 2005 9:16 am
Posts: 18
Hello,

thanks a lot. What's your Mapping Strategie? l'm using "By Subclass"

Regards

Lars


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 12, 2005 1:44 pm 
Red Hat Associate
Red Hat Associate

Joined: Mon Aug 16, 2004 11:14 am
Posts: 253
Location: Raleigh, NC
What I described above is not a polymorphic mapping strategy, but a way of leveraging Java's inheritance to avoid duplicated work.

If you mean to generate entities that are polymorphic in Hibernate, you need to use @hibernate.subclass or @hibernate.joined-subclass for your mappings, and the base class has to have a @hibernate.class on it. If you're using @hibernate.subclass, then you also need a discriminator column (defined on the base class) and discriminator values for each persistent entity.

Cheers,

Chris


Top
 Profile  
 
 Post subject: An short Example
PostPosted: Tue Sep 13, 2005 4:41 am 
Newbie

Joined: Mon Jan 10, 2005 9:16 am
Posts: 18
Hello,

so I have extracted an Example of my Mapping. But after generating the mapping files, there are no property buildDate in it. And by running an insert Statement I get the Error --> Column does not Exists. And I don't want to use discriminator columns, is that possible? Or have I to use discriminator columns? So coult you please help me to find the mistake?


Subclass:

Code:

/**
* @hibernate.joined-subclass
* table="COMPOSITEPART"
* @hibernate.joined-subclass-key
* column="ID"
*
*/
public class CompositePartImpl    extends DesignObjectImpl
                        implements java.io.Serializable,CompositePart
{

   private Set          parts         = null;
   
    public CompositePartImpl() {
        parts      = new HashSet();
    }


    public void addPart( AtomicPartImpl x ) {
        parts.add( x );
    }

    /**
    * @hibernate.set
    * cascade="all"
    * inverse="true"
    * table="AtomicPart"
    * @hibernate.collection-key
    * column="part_id"
    * @hibernate.collection-one-to-many
    * class="de.tmobile.ebf.blizzard.assembly.hbm.AtomicPartImpl"
    */
   public Set getParts() {
      return parts;
   }
}


Baseclass:
Code:
/**
* * @hibernate.class
* table="DESIGNOBJECT"
*
*
*/
public abstract class DesignObjectImpl implements DesignObject
{   
   private long    id;
        private Date    buildDate   = null;

    public DesignObjectImpl() {
    }
   /**
    * @hibernate.id
    * column="ID"
    * unsaved-value="null"
    * generator-class="sequence"
    * @hibernate.generator-param
    * name="sequence"
    * value="id_sequence"
    */
   public long getId() {
      return id;
   }

/**
    * @hibernate.property
    * column="buildDate"
    * type="date"
    */
   public Date getBuildDate() {
      return buildDate;
   }
}


Cheers

Lars


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 13, 2005 8:45 am 
Red Hat Associate
Red Hat Associate

Joined: Mon Aug 16, 2004 11:14 am
Posts: 253
Location: Raleigh, NC
OK now I see what you want. Someone who has done this (I have not) would be the right person to answer for sure, but it looks like what you want is outlined in the manual in the following section:

10.1.4. Mixing table per class hierarchy with table per subclass

Best of Luck.

-Chris


Top
 Profile  
 
 Post subject:
PostPosted: Tue Sep 13, 2005 9:17 am 
Newbie

Joined: Mon Jan 10, 2005 9:16 am
Posts: 18
Hello,

I don't want mixing mapping strategies. I'm just using 'By Subclass
'. I have the XDoclet Tags for the explained Example. But the Inheritence mapping by XDoclet doesn't work. If I'm creating the mapping by Hand. It works. But by using XDoclet, the inherited Attributes are not in generated mapping file for the subclass. Is this a bug or my fault?

The generated Mapping file

Code:
<hibernate-mapping
>
    <class
        name="DesignObjectImpl"
        table="DESIGNOBJECT"
    >

        <id
            name="id"
            column="ID"
            type="long"
            unsaved-value="null"
        >
            <generator class="sequence">
                <param name="sequence">id_sequence</param>
            </generator>
        </id>

        <property
            name="buildDate"
            type="date"
            update="true"
            insert="true"
            column="buildDate"
        />
        <joined-subclass
            name="CompositePartImpl"
            table="COMPOSITEPART"
        >
            <key
                column="ID"
            />
       
        <set
            name="parts"
            table="AtomicPart"
            lazy="false"
            inverse="true"
            cascade="all"
            sort="unsorted"
        >

            <key
                column="part_id"
            >
            </key>

            <one-to-many
                  class="AtomicPartImpl"
            />

        </set>

        .
        .
        .

        </joined-subclass>
       


Stacktrace
Code:
[java] Hibernate: insert into CompositePart (buildDate, type, id) values (?, ?, ?)
     [java] 3334 [2005-09-13 15:08:14,711] WARN  org.hibernate.util.JDBCExceptionReporter  - SQL Error: 904, SQLState: 42000
     [java] 3334 [2005-09-13 15:08:14,711] ERROR org.hibernate.util.JDBCExceptionReporter  - ORA-00904: Unknown Column
     [java] 3344 [2005-09-13 15:08:14,721] ERROR org.hibernate.event.def.AbstractFlushingEventListener  - Could not synchronize database state with session
     [java] org.hibernate.exception.SQLGrammarException: could not insert: [CompositePartImpl]
     [java] at org.hibernate.exception.ErrorCodeConverter.convert(ErrorCodeConverter.java:70)
     [java] at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
     [java] at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:1859)
     [java] at org.hibernate.persister.entity.BasicEntityPersister.insert(BasicEntityPersister.java:2190)
     [java] at org.hibernate.action.EntityInsertAction.execute(EntityInsertAction.java:46)
.
.
.


So you can see that the property buildDate is in the generated Mapping for my Baseclass (DesignObjectImpl), but not in the subclass (CompositePartImpl). What can I do that the inherited attributs like buildDate is also generated in the subclass (CompositePartImpl)?


Cheers

Lars


Top
 Profile  
 
 Post subject:
PostPosted: Sat Nov 19, 2005 4:11 pm 
Regular
Regular

Joined: Sat Nov 19, 2005 2:46 pm
Posts: 69
I have it working this way:
Code:
/**
* @hibernate.class table="organization"
*/
public class Organization implements Serializable
{
...
/**
  * @hibernate.id generator-class="native" column="id" type="long" unsaved-value="-1"
  */
  public long getId()
  {
    return this.id;
  }

Code:
/**
* @hibernate.joined-subclass table="client" column="organization_id"
* @hibernate.joined-subclass-key column="organization_id"
*/
public class Client extends Organization
{
...

XDoclet generated Orgnaization.hbm.xml:
Code:
<hibernate-mapping
>
    <class
        name="com.regul8.hibernate.organization.Organization"
        table="organization"
    >

        <id
            name="id"
            column="id"
            type="long"
            unsaved-value="-1"
        >
            <generator class="native">
              <!-- 
                  To add non XDoclet generator parameters, create a file named
                  hibernate-generator-params-Organization.xml
                  containing the additional parameters and place it in your merge dir.
              -->
            </generator>
        </id>
.....
        <joined-subclass
            name="com.regul8.hibernate.organization.Client"
            table="client"
        >
            <key
                column="organization_id"
            />
..........
        </joined-subclass>

    </class>

</hibernate-mapping>

The tables are defined like this:
Code:
CREATE TABLE `organization`
(
  `id` int(11) NOT NULL auto_increment,
  PRIMARY KEY  (`id`),
)

Code:
CREATE TABLE `client`
(
  `organization_id` int(11) NOT NULL,
  KEY `organization_id` (`organization_id`),
  CONSTRAINT `client_ibfk_1` FOREIGN KEY (`organization_id`) REFERENCES `organization` (`id`)
)



This works absolutely fine :-)

_________________
Stewart
London, UK


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