-->
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: NamedQuery generates invalid SQL
PostPosted: Tue Jan 10, 2012 7:02 am 
Newbie

Joined: Tue Jan 10, 2012 6:27 am
Posts: 1
Hello,

I encountered a problem with a named query that seems to be linked with joined inheritance and maybe OneToOne relationship. The query generates an undefined table alias in the where clause. I'm using Hibernate 4.0.0.Beta5 on the JBoss 7.0.1 server and Oracle express 11 as DBRM.

I tried to extract as small a test as possible.

In the query, if I replace a.b.vb1 with a.b.vb, the query works. So the question seems to be:
How can I access the property of Subclass B1 in the query?

Maybe my approach has some error, but Hibernate doesn't complain about the query on deploy, so I'm not sure if this is a hibernate bug or not. Could you please tell me if this is supposed to work? Thank you.

Here are my Entities:
Code:
/////////////////////////////
// A.java
/////////////////////////////
import java.io.Serializable;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQuery;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity
@Table(name = "A")
@NamedQuery(name = "test",
query = "select a.va from A a where (:x is null or :x = a.b.vb1)")
public  class A implements Serializable {
   private static final long serialVersionUID = 1L;

   @Id
   @Column(name="id")
   private Long _id;

   @OneToOne(mappedBy="a", cascade=CascadeType.ALL)
   private B b;

   @Column(name="va")
   private String va;

    public Long getId() {
      return _id;
   }

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

   public B getB() {
      return b;
   }

   public void setB(B b) {
      this.b = b;
   }

   public String getVa() {
      return va;
   }

   public void setVa(String va) {
      this.va = va;
   }
}


Code:
/////////////////////////////
// B.java
/////////////////////////////
import java.io.Serializable;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;

@Entity
@Table(name = "b")
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class B implements Serializable {
   private static final long serialVersionUID = 1L;

    @Id
    @Column(name="id")
   private Long _id;

    @OneToOne(cascade=CascadeType.ALL)
    @JoinColumn(name="a_fk", nullable=false)
    private A a;

    @Column(name="vb")
    private String vb;

    public Long getId() {
       return _id;
    }

   public void setId(Long id) {
      _id = id;
   }
   
    public A getA() {
       return a;
    }

    public void setA(A a) {
       this.a = a;
    }

    public String getVb() {
        return vb;
    }

    public void setVb(String vb) {
        this.vb = vb;
    }
}


Code:
/////////////////////////////
// B1.java
/////////////////////////////
import java.io.Serializable;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;

@Entity
@Table(name = "b1")
public class B1 extends B implements Serializable {
   private static final long serialVersionUID = 1L;

   @Column(name="vb1")
   private String vb1;

   public String getVb1() {
      return vb1;
   }

   public void setVb1(String vb1) {
      this.vb1 = vb1;
   }
}


Code:
/////////////////////////////
// from my EJB
/////////////////////////////
@PersistenceContext private EntityManager _em;

    public List<A> testHib() {
       Query q = _em.createNamedQuery("test");
       
       q.setParameter("x", "abc");
       
       @SuppressWarnings("unchecked")
       List<A> result = q.getResultList();
       return result;
    }


Code:
/////////////////////////////
// SQL generated by Hibernate
/////////////////////////////
11:15:52,881 INFO  [stdout] (http--127.0.0.1-8080-1) Hibernate:
11:15:52,881 INFO  [stdout] (http--127.0.0.1-8080-1)     select
11:15:52,881 INFO  [stdout] (http--127.0.0.1-8080-1)         a0_.va as col_0_0_
11:15:52,881 INFO  [stdout] (http--127.0.0.1-8080-1)     from
11:15:52,881 INFO  [stdout] (http--127.0.0.1-8080-1)         schema.A a0_,
11:15:52,881 INFO  [stdout] (http--127.0.0.1-8080-1)         schema.b b1_
11:15:52,881 INFO  [stdout] (http--127.0.0.1-8080-1)     where
11:15:52,881 INFO  [stdout] (http--127.0.0.1-8080-1)         a0_.id=b1_.a_fk
11:15:52,881 INFO  [stdout] (http--127.0.0.1-8080-1)         and (
11:15:52,881 INFO  [stdout] (http--127.0.0.1-8080-1)             ? is null
11:15:52,881 INFO  [stdout] (http--127.0.0.1-8080-1)             or ?=b1_1_.vb1
11:15:52,881 INFO  [stdout] (http--127.0.0.1-8080-1)         )


in the next to last line, the b1_1_ table alias is wrong and should be b1_


Top
 Profile  
 
 Post subject: Re: NamedQuery generates invalid SQL
PostPosted: Thu Jan 12, 2012 1:59 pm 
Newbie

Joined: Thu Jan 12, 2012 1:55 pm
Posts: 2
I have a similar problem using Spring 3.1 and Hibernate 4.0.1
Query is generated incorrectly when using Joined Inheritance
Please help us!


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.