-->
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.  [ 6 posts ] 
Author Message
 Post subject: Problem with polymorphic collections
PostPosted: Fri Sep 22, 2006 6:27 am 
Newbie

Joined: Tue Feb 08, 2005 6:16 am
Posts: 6
Hibernate version: hibernate-3.2
Annotation version: hibernate-annotations-3.2.0.CR2

Hi,

I'm trying now for days to solve my problem, which I'm not sure if it is a problem or expected behaviour, since it seems nobody else has this problem.

My problem is as follows:

I have an object called BillingUnit which contains many different types of costs. All costs inherit from the same base class. I use single-table inheritance with a descriminator.

When I call a cost method from BillingUnit to get a specific cost, I get all the costs and not just the correct cost type. It seems that in the SQL the descriminator does not get evaluated or is ignored, which makes me think that there is something wrong with my mapping!

Here is the data model, respectively the mappings. For simplicity only the relavent fields are included.

Code:
@Entity()
@Table(name = "AE")
public class BillingUnit implements DomainModel {
   private Integer _id;

   private Set<HeatingWarmwaterCostTree> _heatingWarmwaterCostTrees;

   @Id
   @Column(name = "AE_IDPK", nullable = false)
   public Integer getId() {
      return _id;
   }

   @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
   @JoinColumn(name = "KOST_DAID")
   public Set<HeatingWarmwaterCostTree> getHeatingWarmwaterCostTrees() {
      return _heatingWarmwaterCostTrees;
   }

   public void setHeatingWarmwaterCostTrees(Set<HeatingWarmwaterCostTree> heatingWarmwaterCostTrees) {
      _heatingWarmwaterCostTrees = heatingWarmwaterCostTrees;
   }

   }



Code:
@Entity
@Table(name = "KOSTEN")
@Inheritance(strategy= InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="KOST_HZWWORHNK", discriminatorType=DiscriminatorType.INTEGER)
public class CostTree implements DomainModel {
   private Integer _id;

   public CostTree() {
   }

   @Id
   @Column(name = "KOST_IDPK")
   public Integer getId() {
      return _id;
   }

   public void setId(Integer id) {
      _id = id;
   }
}


Code:
@Entity
@DiscriminatorValue(value = "0")
public class HeatingWarmwaterCostTree extends CostTree {
   public HeatingWarmwaterCostTree() {
   }
}


I'm using Spring for Session management
Code:
   
public BillingUnit find(int id) throws DaoException {
      try {
         return (BillingUnit) getHibernateTemplate().load(BillingUnit.class, id);
      } catch (DataAccessException e) {
         throw new DaoException(e, LOG);
      }
   }


UnitTest
Code:
   public void testBillingUnit() throws Exception {
      BillingUnitService billingUnitService = getBean("billingUnitService");

      BillingUnit billingUnit = billingUnitService.getBillingUnit(2499);
      assertNotNull(billingUnit);

      for (HeatingWarmwaterCostTree heatingWarmwaterCostTree : billingUnit.getHeatingWarmwaterCostTrees()) {
         System.err.println(heatingWarmwaterCostTree.getDescription());
      }
   }


When I run the test it prints all the different costs which are associated with this billingunit, but actually there is only one HeatingWarmWaterCost in the DB.

Any help would be appreciated!


Top
 Profile  
 
 Post subject:
PostPosted: Mon Sep 25, 2006 7:55 am 
Newbie

Joined: Tue Feb 08, 2005 6:16 am
Posts: 6
The only solution I found to this problem is using the @Where annotation
Code:
@Where(clause="discriminator_column_name='discriminator_value'")

as described in http://forum.hibernate.org/viewtopic.php?t=961213

Is this the correct way to do this?

What I was also wondering is that the @Method annotation comes from package 'org.hibernate.annotations', which is hibernate specific!


Top
 Profile  
 
 Post subject:
PostPosted: Tue Dec 05, 2006 10:30 am 
Newbie

Joined: Thu Oct 26, 2006 5:44 am
Posts: 4
We have the same problem with:

Hibernate Core 3.2.1 GA
Hibernate Annotations 3.2.0 GA
Hibernate EntityManager 3.2.0 GA

Any comments from Hibernate support team? Is this a bug? Thanks.


Top
 Profile  
 
 Post subject:
PostPosted: Thu Dec 07, 2006 1:17 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
nope, not a bug.
If you want specific association per subtype you need a specific join column for each.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Dec 08, 2006 7:41 am 
Newbie

Joined: Thu Oct 26, 2006 5:44 am
Posts: 4
emmanuel wrote:
nope, not a bug.
If you want specific association per subtype you need a specific join column for each.

Thank you for your reply, Emmanuel.

However I have a different opinion. From a view of native Hibernate this may be not a bug, but from a view of JPA this must be a bug. Anyway, TopLink Essentials can handle this properly, without any code or db modification in our application. Our persistence code is strictly at the level of JPA.


Top
 Profile  
 
 Post subject:
PostPosted: Sun Dec 17, 2006 4:42 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
this is an implementation side effect

_________________
Emmanuel


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