-->
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: Polymorphism question
PostPosted: Sat Oct 06, 2007 3:57 pm 
Newbie

Joined: Mon Oct 01, 2007 12:29 pm
Posts: 8
I have the following classes:

Code:
@javax.persistence.Entity
@Entity(polymorphism = PolymorphismType.EXPLICIT)
@Table(name = "A")
@Inheritance(strategy = InheritanceType.JOINED)
public class A {

@ManyToOne
   @JoinColumn(name = "container_id")
   private Container container;
}

@Entity
@Table(name = "B")
public class B extends A



I get the expected polymorphic behavior when I load these types directly. That is, if I load all entities of type A, then I only get type A, or if I load all entities of type B, then I only get type B.

If I have an association collection of type A in another entity, then I do not get the polymorphic behavior that I would expect. That is, if I have the following class defined:

Code:
@Entity
public class Container {

   @OneToMany(fetch = FetchType.LAZY, mappedBy = "A", targetEntity = A.class)
   private List<A> a;

}


If I load a class of type Container, then its associated collection of A contains entities of type A and type B whereas I expect only A. Can anyone tell me how I have misconfigured my entities to get the behavior I am seeing rather than what I am expecting?

Thanks in advance,
-Anthony


Top
 Profile  
 
 Post subject:
PostPosted: Sat Oct 06, 2007 6:04 pm 
Regular
Regular

Joined: Sun Sep 30, 2007 7:51 pm
Posts: 93
For what you want, you need to set the B type with @Entity(polymorphism = PolymorphismType.EXPLICIT)

Regards,
Pavol


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 08, 2007 7:46 am 
Newbie

Joined: Mon Oct 01, 2007 12:29 pm
Posts: 8
I tried your suggestion but to no avail. I annotated class B as having polymorphism type explicit but it had no effect. I have now tried the following combinations:

1. class A = polymorphism.explicit, class B is none
2. class A = polymorphic.explicit, class B = polymorphism.explicit
3. class A = none, class B = polymorphism.explicit

No combination has the desired effect of having a collection of only type A in a container entity.

Any other ideas? Is there no way to get the behavior I need?

Thanks,
Anthony


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 08, 2007 8:12 am 
Regular
Regular

Joined: Sun Sep 30, 2007 7:51 pm
Posts: 93
Maybe ugly solution, but you can try the @Where clause and just select the A class according discriminator?

Pavol


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 08, 2007 8:29 am 
Newbie

Joined: Mon Oct 01, 2007 12:29 pm
Posts: 8
Could you point me in the direction of some documentation that describes how to use the @Where clause to discriminate based on class type? I can't seem to find anything that describes how to do it.

Thanks,
Anthony


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 08, 2007 9:32 am 
Regular
Regular

Joined: Sun Sep 30, 2007 7:51 pm
Posts: 93
Never tried, but you can use the annotations to set the discriminator like:

@DiscriminatorColumn(name="DTYPE", DiscriminatorType=DiscriminatorType.STRING)
@DiscriminatorValue("CLASSA")
class A

It is a standard col, and use it in Where as DTYPE = CLASSA ..

I don't know however, if you are using JPA (this annotations are JPA ones) or just pure hibernate.

but, an ugly solution as I said. Try to find out why the explicit does not work.

Pavol


Top
 Profile  
 
 Post subject:
PostPosted: Mon Oct 08, 2007 11:23 am 
Newbie

Joined: Mon Oct 01, 2007 12:29 pm
Posts: 8
I would rather not put that discriminator on as there may be more types in the future, and that solution is not so flexible.

I can't figure out why the polymorphism setting isn't working. It seems to always issue two sql requests, one for each type. However, it only issues those two requests for the collection of the base type in the container, and never for direct loads of the respective types.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 13, 2007 7:23 pm 
Newbie

Joined: Thu Dec 13, 2007 9:48 am
Posts: 3
To do this, you can't use JOINED as in the original suggestion because the discriminator is ignored for this strategy. The suggestion is to use a SINGLE_TABLE strategy with SecondaryTables for the subclass (http://opensource.atlassian.com/projects/hibernate/browse/ANN-562)

I was trying to use PolymorphismType for this problem as well, but it seems that it just doesn't work that way by design:
http://opensource.atlassian.com/projects/hibernate/browse/ANN-314
...which is a pity. InheritanceType.JOINED + PolymorphismType.EXPLICIT + discriminators would solve my problem nicely.

-Eoin

pavol.zibrita wrote:
Never tried, but you can use the annotations to set the discriminator like:

@DiscriminatorColumn(name="DTYPE", DiscriminatorType=DiscriminatorType.STRING)
@DiscriminatorValue("CLASSA")
class A

It is a standard col, and use it in Where as DTYPE = CLASSA ..

I don't know however, if you are using JPA (this annotations are JPA ones) or just pure hibernate.

but, an ugly solution as I said. Try to find out why the explicit does not work.

Pavol
[url][/url]


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.