-->
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: Selection based on subclass attributes (polymorphic queries)
PostPosted: Wed Jun 13, 2007 3:11 pm 
Newbie

Joined: Wed Oct 25, 2006 2:41 am
Posts: 7
Hello,

I don't know whether I am doing something incorrectly or Hibernate does not support where clauses that reference properties on a subclass that are not present on a superclass.

Basically, I have class P which has a many to many relationship with class L.

Class L has a type field that is in integer.

Class LS is a subclass of L containing a string attribute (it's getter being getString()).

Class LSK is a subclass of LS which sets the type integer (above) to 1 via its constructor.

Class LSC is also a subclass of LS which sets the type integer (above) to 2 also via its constructor

I would like to be able to select all P for which L.type = 1 and LS.string = 'somevalue'.

However, I get a property not found for LS.string (since it's not available on L). I've also tried LS.label.

Having RTFM, RTFF (forum) and SFG (searched F'ing Google), I cannot find a single HQL example which shows a selection based upon the attribute of a subclass. Can it even be done?

The code is below.

Thanks,

Matthew

Class - P
Code:
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;


@Entity
@Table(name = "P")
@Inheritance(strategy = InheritanceType.JOINED)
public class P
{

   private Long m_Id;
   
   private Set<L> m_L = new HashSet<L>(0);
   
   public P()
   {
   }

   public P(Long id)
   {
      this.m_Id = id;
   }


   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   @Column(name = "Id", unique = true, nullable = false)
   public Long getId()
   {
      return this.m_Id;
   }

   public void setId(Long id)
   {
      this.m_Id = id;
   }

   
   
   @ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REFRESH }, fetch = FetchType.LAZY)
   @JoinTable(name = "PL", joinColumns = { @JoinColumn(name = "IdP") }, inverseJoinColumns = { @JoinColumn(name = "IdL") })
   public Set<L> getLs()
   {
      return m_L;
   }
   
   public void setLs(Set<L> pValue)
   {
      m_L = pValue;
   }

}


Class - L

Code:
@Entity
@Table(name = "L")
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class L
{
   private int m_Type;
   
   protected L(int pType)
   {
      m_Type = pType;
   }

   @Override
   @Id
   @GeneratedValue(strategy = GenerationType.AUTO)
   @Column(name = "Id", unique = true, nullable = false)
   public Long getId()
   {
      return super.getId();
   }
   
   public int getType()
   {
      return m_Type;
   }
   
   public void setType(int pValue)
   {
      m_Type = pValue;
   }
   
   
}


Class - LS

Code:
import javax.persistence.Column;
import javax.persistence.MappedSuperclass;

@MappedSuperclass
public abstract class LS extends L
{
   private String m_String;

   public LS(int pType)
   {
      super(pType);
   }
   
   public LS(int pType, String pString)
   {
      super(pType);
      m_String = pString;
   }

   @Column(name = "label", unique = true)
   public String getString()
   {
      return m_String;
   }
   
   public void setString(String pValue)
   {
      m_String = pValue;
   }

}


Class - LSK

Code:
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Transient;


@Entity
@Table(name = "LSK")
public class LSK extends LS
{
   
   public LSK()
   {
      super(2);
   }

   public LSK(String pString)
   {
      super(2, pString);
   }
   
   
}


Classs - LSC

Code:
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Transient;


@Entity
@Table(name = "LSC")
public class LSC extends LS
{
   
   public LSC()
   {
      super(1);
   }

   public LSC(String pString)
   {
      super(1, pString);
   }
   
   
}


b]Hibernate version:[/b]

3.x

Code between sessionFactory.openSession() and session.close():

Using the EntityManager's createQuery

Full stack trace of any exception that occurs:

Am getting multiple different traces depending upon what I try

Name and version of the database you are using:

MySQL 5.x (but query is never generated)

The generated SQL (show_sql=true):

Hibernate cannot form the query


Top
 Profile  
 
 Post subject:
PostPosted: Wed Jun 13, 2007 3:24 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
AFAIK it cannot be done, but the query can probably be rewritten the other way around to be able to work on an LS type

_________________
Emmanuel


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.