-->
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.  [ 7 posts ] 
Author Message
 Post subject: @MappedSuperclass and inherited objects in same table
PostPosted: Thu Aug 10, 2006 3:15 pm 
Newbie

Joined: Mon Apr 18, 2005 10:37 am
Posts: 8
Hibernate version: 3.2.0.cr2, jdk 1.5

Code:

Code:
package test;

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

@Entity
@Table(name="test")
class TestChild extends Test {
   public TestChild () {}
}

package test;

import java.util.List;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import org.hibernate.LockMode;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;

@Entity @MappedSuperclass
public class Test {
   @Id
   Integer id;
   String txt;
   public Integer getId() {
      return id;
   }
   public void setId(Integer id) {
      this.id = id;
   }
   public String getTxt() {
      return txt;
   }
   public void setTxt(String txt) {
      this.txt = txt;
   }

   @SuppressWarnings("unchecked")
   public static void main(String[] args) {
      SessionFactory factory = new AnnotationConfiguration()
         .addAnnotatedClass(Test.class)
         .addAnnotatedClass(TestChild.class)
         .setProperty("hibernate.connection.driver_class", "org.postgresql.Driver")
         .setProperty("hibernate.connection.url", "jdbc:postgresql://")
         .setProperty("hibernate.connection.username", "")
         .setProperty("hibernate.connection.password", "")
         .setProperty("hibernate.dialect", "org.hibernate.dialect.PostgreSQLDialect")
         .setProperty("hibernate.show_sql", "true")
         .setProperty("hibernate.hbm2ddl.auto", "update")
         .buildSessionFactory();
      Session s = factory.openSession();
      List<Test> l = s.createQuery("from Test").list();
      for(Test t: l) {
         System.out.println(t.getId());
      }
      Test t = (Test)s.createQuery("from Test e where txt=?")
         .setString(0, "1").uniqueResult();
      s.close();
   }



Code:
insert into test values (1,1)
insert into test values (2,2)


Full stack trace of any exception that occurs:
Code:
Exception in thread "main" org.hibernate.NonUniqueResultException: query did not return a unique result: 2
   at org.hibernate.impl.AbstractQueryImpl.uniqueElement(AbstractQueryImpl.java:765)
   at org.hibernate.impl.AbstractQueryImpl.uniqueResult(AbstractQueryImpl.java:756)
   at test.Test.main(Test.java:49)


The generated SQL (show_sql=true):
Code:
Hibernate: select testchild0_.id as id1_, testchild0_.txt as txt1_ from test testchild0_ where testchild0_.txt=? for update of testchild0_
Hibernate: select test0_.id as id0_, test0_.txt as txt0_ from Test test0_ where test0_.txt=? for update of test0_


Why is output doubled:
Code:
1
2
1
2

and uniqueResult() fails?
Selecting from TestChild works fine...


Top
 Profile  
 
 Post subject:
PostPosted: Thu Aug 10, 2006 11:29 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
@entity and @MappedSuper class at the same time does not make sense

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 11, 2006 3:29 am 
Newbie

Joined: Mon Apr 18, 2005 10:37 am
Posts: 8
Without @Entity on Test it throws org.hibernate.hql.ast.QuerySyntaxException: Test is not mapped [from Test]
Without @MappedSuperclass it pushes me to have DTYPE column in the table.

With @Entity and @MappedSuperclass it does not throw an exception but returns weird results.

I need to have inherited entities and need to have them in the same table. Does it make sense now? :)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 11, 2006 1:47 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
@MappedSuperclass
public ...

should not raise Test not mapped (at least with Hibernate). You must have something wrong somewhere. Try and start from one of the hibernate annotations unit test example and update it until it reflect your needs

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 11, 2006 2:00 pm 
Newbie

Joined: Mon Apr 18, 2005 10:37 am
Posts: 8
But it raises Test is not mapped! Just try example above, it is simple as it can be.
Or should I fill a bug report?

Code:
Exception in thread "main" org.hibernate.hql.ast.QuerySyntaxException: Test is not mapped [from Test]
   at org.hibernate.hql.ast.util.SessionFactoryHelper.requireClassPersister(SessionFactoryHelper.java:158)
   at org.hibernate.hql.ast.tree.FromElementFactory.addFromElement(FromElementFactory.java:87)
   at org.hibernate.hql.ast.tree.FromClause.addFromElement(FromClause.java:70)
   at org.hibernate.hql.ast.HqlSqlWalker.createFromElement(HqlSqlWalker.java:267)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElement(HqlSqlBaseWalker.java:3049)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromElementList(HqlSqlBaseWalker.java:2938)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.fromClause(HqlSqlBaseWalker.java:688)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.query(HqlSqlBaseWalker.java:544)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.selectStatement(HqlSqlBaseWalker.java:281)
   at org.hibernate.hql.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:229)
   at org.hibernate.hql.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:227)
   at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:159)
   at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:110)
   at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:77)
   at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:56)
   at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:71)
   at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)
   at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
   at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1612)
   at test.Test.main(Test.java:49)


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 11, 2006 2:30 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
from test.Test
A mapped super class is not an entity so no alias are defined. I usually always use "from " + MyClass.class.getName(), this ease refactoring.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Fri Aug 11, 2006 2:50 pm 
Newbie

Joined: Mon Apr 18, 2005 10:37 am
Posts: 8
That is the point! test.Test works where Test does not.
Thank you!


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 7 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.