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 - LCode:
@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 - LSCode:
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 - LSKCode:
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 - LSCCode:
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