-->
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.  [ 1 post ] 
Author Message
 Post subject: Exception when querying for property of subclass
PostPosted: Tue Aug 28, 2007 8:57 am 
Newbie

Joined: Tue Aug 28, 2007 8:09 am
Posts: 2
Hi,

I get a SQLGrammarException when executing a hql-query for a superclass that contains properties of subclasses. The property is mapped in more than one subclass. This may be the problem.

Hibernate version:

Hibernate 3.2.5.ga
Hibernate Annotations 3.3.0.ga

Mapping documents:

Using Annotations, annotated classes are:

Code:
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.Table;

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

   @Id
   @Column(name = "a_id")
   private String id;

   public String getId() {
      return id;
   }

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

Code:
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;

@Entity
@Table(name = "b")
@PrimaryKeyJoinColumn(name = "b_id")
public class B extends A {

   @Column(name = "b_foo", nullable = false)
   private String foo;
   @Column(name = "b_bar", nullable = false)
   private String bar;
   
   public String getBar() {
      return bar;
   }
   
   public void setBar(String bar) {
      this.bar = bar;
   }
   
   public String getFoo() {
      return foo;
   }
   
   public void setFoo(String foo) {
      this.foo = foo;
   }
}

Code:
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;

@Entity
@Table(name = "c")
@PrimaryKeyJoinColumn(name = "c_id")
public class C extends A {
   
   @Column(name = "c_foo", nullable = false)
   private String foo;
   @Column(name = "c_bas", nullable = false)
   private String bas;
   
   public String getBas() {
      return bas;
   }
   
   public void setBas(String bas) {
      this.bas = bas;
   }
   
   public String getFoo() {
      return foo;
   }
   
   public void setFoo(String foo) {
      this.foo = foo;
   }
}

Code:
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.PrimaryKeyJoinColumn;
import javax.persistence.Table;

@Entity
@Table(name = "d")
@PrimaryKeyJoinColumn(name = "d_id")
public class D extends A {

   @Column(name = "b_bar", nullable = false)
   private String bar;
   @Column(name = "b_bas", nullable = false)
   private String bas;
   
   public String getBar() {
      return bar;
   }
   
   public void setBar(String bar) {
      this.bar = bar;
   }
   
   public String getBas() {
      return bas;
   }
   
   public void setBas(String bas) {
      this.bas = bas;
   }
}


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

session.createQuery("select a from A a where a.foo = :foo").setParameter("foo", "foo").list();

Full stack trace of any exception that occurs:

org.hibernate.exception.SQLGrammarException: could not execute query
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:67)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.loader.Loader.doList(Loader.java:2223)
at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2104)
at org.hibernate.loader.Loader.list(Loader.java:2099)
at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:378)
at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:338)
at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:172)
at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1121)
at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)
at SubclassPropertyTest.testQuery(SubclassPropertyTest.java:28)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at junit.framework.TestCase.runTest(TestCase.java:164)
at junit.framework.TestCase.runBare(TestCase.java:130)
at junit.framework.TestResult$1.protect(TestResult.java:110)
at junit.framework.TestResult.runProtected(TestResult.java:128)
at junit.framework.TestResult.run(TestResult.java:113)
at junit.framework.TestCase.run(TestCase.java:120)
at junit.framework.TestSuite.runTest(TestSuite.java:228)
at junit.framework.TestSuite.run(TestSuite.java:223)
at org.junit.internal.runners.OldTestClassRunner.run(OldTestClassRunner.java:35)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:38)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
Caused by: java.sql.SQLException: Column not found: A0_1_.C_FOO in statement [select a0_.a_id as a1_0_, a0_1_.b_bar as b2_1_, a0_1_.b_foo as b3_1_, a0_2_.c_bas as c2_2_, a0_2_.c_foo as c3_2_, a0_3_.b_bar as b2_3_, a0_3_.b_bas as b3_3_, case when a0_1_.b_id is not null then 1 when a0_2_.c_id is not null then 2 when a0_3_.d_id is not null then 3 when a0_.a_id is not null then 0 end as clazz_ from a a0_ left outer join b a0_1_ on a0_.a_id=a0_1_.b_id left outer join c a0_2_ on a0_.a_id=a0_2_.c_id left outer join d a0_3_ on a0_.a_id=a0_3_.d_id where a0_1_.c_foo=?]
at org.hsqldb.jdbc.Util.throwError(Unknown Source)
at org.hsqldb.jdbc.jdbcPreparedStatement.<init>(Unknown Source)
at org.hsqldb.jdbc.jdbcConnection.prepareStatement(Unknown Source)
at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:505)
at org.hibernate.jdbc.AbstractBatcher.getPreparedStatement(AbstractBatcher.java:423)
at org.hibernate.jdbc.AbstractBatcher.prepareQueryStatement(AbstractBatcher.java:139)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1547)
at org.hibernate.loader.Loader.doQuery(Loader.java:673)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:236)
at org.hibernate.loader.Loader.doList(Loader.java:2220)
... 27 more

Name and version of the database you are using:

hsqldb 1.8.0.7

The generated SQL (show_sql=true):

select a0_.a_id as a1_0_, a0_1_.b_bar as b2_1_, a0_1_.b_foo as b3_1_, a0_2_.c_bas as c2_2_, a0_2_.c_foo as c3_2_, a0_3_.b_bar as b2_3_, a0_3_.b_bas as b3_3_, case when a0_1_.b_id is not null then 1 when a0_2_.c_id is not null then 2 when a0_3_.d_id is not null then 3 when a0_.a_id is not null then 0 end as clazz_ from a a0_ left outer join b a0_1_ on a0_.a_id=a0_1_.b_id left outer join c a0_2_ on a0_.a_id=a0_2_.c_id left outer join d a0_3_ on a0_.a_id=a0_3_.d_id where a0_1_.c_foo=?


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

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.