-->
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.  [ 2 posts ] 
Author Message
 Post subject: Relationship Column per Subclass with Discriminator Column
PostPosted: Thu Nov 12, 2009 6:08 am 
Newbie

Joined: Thu Nov 12, 2009 5:43 am
Posts: 3
Code:
@Entity
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
public abstract class TE {
   
   @Id
   @GenericGenerator(name = "idGenerator", strategy = "assigned")
   @GeneratedValue(generator = "idGenerator")
   @Column(name="TE_KEY")
   private String id;
   
   @OneToMany
   @JoinColumn(name="TA_KEY", referencedColumnName="TE_KEY", insertable=false, updatable=false)
   private List<TA> taList;
}

public class TE1 extends TE {
}

public class TE2 extends TE {
}


Code:
@Entity
@Inheritance(strategy=InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name="ta_type")
public abstract class TA {
   
   @Id
   @GenericGenerator(name = "idGenerator", strategy = "assigned")
   @GeneratedValue(generator = "idGenerator")
   @Column(name="TA_KEY")
   private String id;
   
   public abstract void setTE(TE TE);

   public abstract TE getTE();
}

@Entity
@DiscriminatorValue("TE1")
public class TA2 extends TA {
   
   @ManyToOne(targetEntity=TE1.class)
   @JoinColumn(name="TE_ENTITY_FK")
   private TE TE;

   public TE getTE() {
   return TE;
   }

   public void setTE(TE TE) {
      this.TE = TE;
   }
}

@Entity
@DiscriminatorValue("TE2")
public class TA2 extends TA {
   
   @ManyToOne(targetEntity=TE2.class)
   @JoinColumn(name="TE_ENTITY_FK")
   private TE TE;

   public TE getTE() {
      return TE;
   }

   public void setTE(TE TE) {
      this.TE = TE;
   }
   
}



Criteria crit = super.getSession().createCriteria(TA.class);
crit.createAlias("TE", "trk");
crit.add(Restrictions.like("trk.id", "%"));


result in :

select ...............
from TA this_
inner join TE1 trk1_ on this_.TE_ENTITY_FK=trk1_.TE_KEY
inner join TE2 trk1_ on this_.TE_ENTITY_FK=trk1_.TE_KEY
where trk1_.TE_KEY like ?


ORA-00918: column ambiguously defined.

How to change my mapping to be able to query without any errors ?


Top
 Profile  
 
 Post subject: Re: Relationship Column per Subclass with Discriminator Column
PostPosted: Wed Nov 18, 2009 9:52 am 
Newbie

Joined: Thu Nov 12, 2009 5:43 am
Posts: 3
=>In others words Cat and Dog inherit Pet. CatOwner and DogOwner inherit Owner. A Cat has a CatOwner. A Dog has a DogOwner. When fetching all Cat and Dog (All Pets), I want to inner join fetch CatOwner for Cat and DogOwner for Dog.

=> select p from Pet ...
must return query like this :
select ...............
from Pet this_
inner join CatOwner catowner1_ on this_.OWNER_FK=catowner1_.KEY
inner join DogOwner dogowner1_ on this_.OWNER_FK=dogowner1_.KEY

I don't want to have something like this :
select ...............
from Pet this_
inner join (select ... DogOwner union CatOwner) owner_ on owner_.KEY = this_.OWNER_FK
=> It has very bad performance ! For each Pet record, Hibernate generates an SQL UNION between all Owner subclasses to select the right Owner. Suppose Owner is a big table per class inheritance tree...(with 100 000 rows in each table)


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