[hibernate-issues] [Hibernate-JIRA] Commented: (HHH-5301) Add support for parametrizing TYPE(e) expressions

Ted Young (JIRA) noreply at atlassian.com
Mon Aug 8 16:03:03 EDT 2011


    [ http://opensource.atlassian.com/projects/hibernate/browse/HHH-5301?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=43190#comment-43190 ] 

Ted Young commented on HHH-5301:
--------------------------------

For what it is worth, you can at least get access to the discriminator by exposing it as a private read-only property.  I wrote up an example here:

http://tedyoung.me/2011/08/08/jpa-and-hibernate-problems-with-polymorphic-queries/

This was particularly important for me where I had to group by and order by type.  It isn't perfect, though.  Note the issues described at the end of the article.  But, it works for now.

> Add support for parametrizing TYPE(e) expressions
> -------------------------------------------------
>
>                 Key: HHH-5301
>                 URL: http://opensource.atlassian.com/projects/hibernate/browse/HHH-5301
>             Project: Hibernate Core
>          Issue Type: Bug
>          Components: query-hql
>    Affects Versions: 3.5.2
>         Environment: Hibernate 3.5.2, Java 6.0.20
>            Reporter: Luis Fernando Planella Gonzalez
>            Assignee: Gail Badner
>         Attachments: query-test-type-expression.patch
>
>
> Support parameters for JPA 2's TYPE(alias) function.
> Test classes:
> @Entity
> @DiscriminatorColumn(name="subclass")
> @Table(name="xs")
> public abstract class XBase {
>     @Id
>     private Integer id;
>     public Integer getId() {
>         return id;
>     }
>     public void setId(Integer id) {
>         this.id = id;
>     }
> }
> @Entity
> @DiscriminatorValue("x1")
> public class X1 extends XBase {
>     @Basic
>     private String x1;
>     public String getX1() {
>         return x1;
>     }
>     public void setX1(String x1) {
>         this.x1 = x1;
>     }
> }
> @Entity
> @DiscriminatorValue("x2")
> public class X2 extends XBase {
>     @Basic
>     private String x2;
>     public String getX2() {
>         return x2;
>     }
>     public void setX2(String x2) {
>         this.x2 = x2;
>     }
> }
> Running the following query works: "select x from XBase x where type(x) = X1"
> However, the following one: "select x from XBase x where type(x) = :t" yelds
> Exception in thread "main" java.lang.UnsupportedOperationException: At the moment this type is not the one actually used to map the discriminator.
> 	at org.hibernate.persister.entity.DiscriminatorType.nullSafeSet(DiscriminatorType.java:111)
> 	at org.hibernate.param.NamedParameterSpecification.bind(NamedParameterSpecification.java:67)
> 	at org.hibernate.loader.hql.QueryLoader.bindParameterValues(QueryLoader.java:567)
> 	at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1612)
> 	at org.hibernate.loader.Loader.doQuery(Loader.java:717)
> 	at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:270)
> 	at org.hibernate.loader.Loader.doList(Loader.java:2294)
> 	at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2172)
> 	at org.hibernate.loader.Loader.list(Loader.java:2167)
> 	at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:448)
> 	at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:363)
> 	at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
> 	at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1258)
> 	at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
> 	at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:241)
> 	at test.Main.main(Main.java:28)
> JPA 2's spec explicitly shows querying by type using parameters in section 4.6.17.4.

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        


More information about the hibernate-issues mailing list