-->
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.  [ 4 posts ] 
Author Message
 Post subject: Class Inheritance and Mapping
PostPosted: Tue Sep 15, 2009 6:34 pm 
Newbie

Joined: Mon Jun 18, 2007 2:08 pm
Posts: 10
Folks,
I would like to get some input from experts on this problem.
I have a library of domain objects that have already been mapped. I am now trying extending some -if not all - of the domain classes in this library but I ran into a problem: Extending classes that have already been mapped seems to be impossible. Here is what I have:
Code:
@Entity
@Table("CLIENT")
public class  Client  extends BaseClient   // Note: BaseClient already has the @MappedSuperclass
{
   
    List<ClientAddress> getClientAddresses() { .. }
}

Now, I would like to extend this class, let's call it ClientEx and here is what I am doing:

Code:
@Entity
@Table("CLIENT")
public class ClientEx extends Client{

    List<ClientAddressEx> getClientAddresses() { .. }

}


The above does not work because the Client Class already has it own anotations.

Has anyone run into this problem,? Please provide some feedback on some possible soluctions (The solution I have rigtht now causes duplicates a lot of code)


Thank you in advance.


Top
 Profile  
 
 Post subject: Re: Class Inheritance and Mapping
PostPosted: Wed Sep 16, 2009 1:22 pm 
Senior
Senior

Joined: Mon Jul 07, 2008 4:35 pm
Posts: 141
Location: Berlin
Hi harringf,

I'm afraid you need to lay hands on your parent class if you want to map the child classes to the same table the parent data goes. You'll require an inheritance strategy provided by the parent.

Annotate the parent class with
Code:
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorValue("some string")


Use @DiscriminatorValue for each child class with a unique value.

You may also choose content based discrimination, see http://docs.jboss.org/hibernate/stable/core/reference/en/html/example-mappings.html#example-mappings-content-discrimination that would not lead to an additional discriminator column as the previous approach will. But that's probably not possible.

CU
Froestel

_________________
Have you tried turning it off and on again? [Roy]


Top
 Profile  
 
 Post subject: Re: Class Inheritance and Mapping
PostPosted: Mon Oct 05, 2009 11:06 am 
Newbie

Joined: Mon Jun 18, 2007 2:08 pm
Posts: 10
Hello Froestel:

Thank you so much for taking the time to reply. You gave me some good pointers and I think that I can make some use of it
as explained in here:
http://java.boot.by/scbcd5-guide/apes05.html

However, I am not sure if I will go that route because of other issues. Example: (Based on the example they give on the link above: Person & Employee).

Now, let's say that Person also has a collection objects (say, cars) represented by Car .. so in Person we would have

class Person{
@<Collection mapping goes here>
List<Car> getCars() {..}
}


Now, assumme also that Employees have a collection of Trucks .. thus we have

class Trucks extends Cars
{
...
}


Then the employee class would be:

class Employee extends Person
{
List<Truck> getTrucks()
{
}
}

..
I think that at this point, we would have 2 * n quesries for the collections to be populated. (First, the base class for List<Car> then for the parent class for List<Truck>... and this is the situation we have....

I believe it to be very unusual for the application to be like that... but that is what we have.. an application that runs by itslef, but this app also can be used as a lib for yet another app that sits on to of it and "tries" to re-use the domain
objects of the lib.

I wonder is there is a better approach.

Thanks again.

Harring.


Top
 Profile  
 
 Post subject: Re: Class Inheritance and Mapping
PostPosted: Mon Oct 05, 2009 5:37 pm 
Senior
Senior

Joined: Mon Jul 07, 2008 4:35 pm
Posts: 141
Location: Berlin
Hi Harring,

well, I think you should stick to the track. I think Hibernate's capabilities involving polymorphism are amazing -- supposed you let Hibernate do what it wants and not interfere too much (which caused more problems than necessary in my first approach).

So just to give you another hint on that. I suppose you would be mapping Car and Truck to the same table (what is actually not the real point). You described that Truck extends Car. Basing on this think about Java Generics and classes like
Code:
class Person<C extends Car> {
...
Collection<C> getCars() {...}
...
}
and
Code:
class Employee<T extends Truck> extends Person<T> {
...
Collection<T> getCars() {...}
...
}

Would this be some idea that helps?

CU
Froestel

_________________
Have you tried turning it off and on again? [Roy]


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