-->
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.  [ 7 posts ] 
Author Message
 Post subject: Using abstract classes as base classes to persistent objects
PostPosted: Thu Sep 28, 2006 2:22 pm 
Newbie

Joined: Mon Jul 24, 2006 3:21 pm
Posts: 14
Consider the class hierarchy here...

Code:
public abstract class MyBase
{
  protected String name;
  // with getters and setters and other stuff
}

public class Person extends MyBase
{
  protected Long personId;
  protected Integer age;
  // with getters and setters
}

public class Pet extends MyBase
{
  protected Long petId;
  protected String species;
  // with getters and setters
}

Is it possible in Hibernate (3.0+) to setup mappings for the Person and Pet classes that include columns for 'name'? In other words, can I map a class to a table and have that table include columns for all the base class properties (i.e. 'name' in this example)?

I would like to make all of my datamodel classes extend from a BaseDataModelObject class and implement a bunch of common methods in that class. I don't want to map that class to a table. I just want any properties of that class to get mapped into the tables for each concrete class.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 28, 2006 4:13 pm 
Beginner
Beginner

Joined: Thu Aug 24, 2006 6:01 am
Posts: 49
Location: sophia-antipolis, France
you sure can. Just give your abstract class these two annotations:
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)

Since the class is abstract, there won't be a table created for it, but a table for each child class.

I'm doing the same thing in a project I'm working on and this works just fine.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 28, 2006 5:13 pm 
Beginner
Beginner

Joined: Tue Sep 26, 2006 11:46 pm
Posts: 33
I think you're looking for the @MappedSuperclass annotation:
http://www.hibernate.org/hib_docs/annot ... tml#d0e859

Basically this will make all the subclasses persist the properties of the superclass without the superclass being persisted as such.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 28, 2006 5:18 pm 
Beginner
Beginner

Joined: Thu Aug 24, 2006 6:01 am
Posts: 49
Location: sophia-antipolis, France
Hi,

it really depends if you want your base class to be abstract I think. I tried having an abstract class and putting the @MappedSuperclass and that gives you a big Hibernate exception (and that was the reason I started posting on this forum in the first place).

In fact in my class hierarchy, I have a base class which all of my persistent classes extend and this class is annotated @MappedSuperclass. Then elsewhere in the class hierarchy, I have abstract classes with child classes and these I annotate as I mentioned above.

I forgot to mention that your 2 child classes need the @Entity annotations.

Now that I think about it, you probably don't want the abstract class, you just want a normal class with the annotation @MappedSuperclass. The inheritance route should only be for classes which really share meaning and not just attributes.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Sep 28, 2006 10:06 pm 
Newbie

Joined: Mon Jul 24, 2006 3:21 pm
Posts: 14
Thanks for the responses, but I'm looking for a solution that can be configured in the hbm.xml mapping files. We aren't using Hiberate's annotations. Our Java classes have absolutely no annotations.

I think I got everything working today. I simply have to make sure the mapping files for each concrete classes contain each of the base class' properties that I want to be included as columns. So the mapping files for the concrete subclasses can't be used to generate code for the subclasses because they would add properties that were already included in the base class. We don't use hbm2java much, so this isn't a problem for us. As long as you don't create a hbm.xml mapping file for the base class, no table will be created for it.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Sep 29, 2006 12:06 am 
Beginner
Beginner

Joined: Tue Sep 26, 2006 11:46 pm
Posts: 33
Both these approaches can be done with hbm.xml files as well as annotations.

The first is the table per concrete class approach:
http://www.hibernate.org/hib_docs/v3/re ... erconcrete

The equivalent of the @MappedSuperclass is the table per concrete class with explicit polymorphism.
http://www.hibernate.org/hib_docs/v3/re ... lymorphism

For this latter one you define the common properties in an XML file and include it using XML entities.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Jan 25, 2007 6:23 pm 
Newbie

Joined: Tue Jan 16, 2007 8:43 pm
Posts: 5
In table-per-concrete, is it possible to mark a union-subclass to not be generated by hbm2java?

I have
Code:
<class name="Payment">
    <id name="id" type="long" column="PAYMENT_ID">
        <generator class="sequence"/>
    </id>
    <property name="amount" column="AMOUNT"/>
    ...
    <union-subclass name="CreditCardPayment" table="CREDIT_PAYMENT">
        <property name="creditCardType" column="CCTYPE"/>
        ...
    </union-subclass>
</class>


I'd like Payment to be generated by hbm2java, but not CreditCardPayment.


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