-->
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: Polymorphic query with a "table-per-class" strateg
PostPosted: Wed Apr 19, 2006 9:57 am 
Newbie

Joined: Fri Jun 10, 2005 9:15 am
Posts: 4
Hibernate version: Hibernate 3.1CR2, Hibernate Annotations 3.1 beta 9, Hibernate EntityManager 3.1 beta 9

Name and version of the database you are using: HSQLDB 1.8.0


Hello,

I am currently testing the different inheritance strategies supported by Hibernate. In one of my tests, I have three classes, Pet, Cat and Dog. Cat and Dog inherit from Pet, and inheritance type is set to "TABLE_PER_CLASS".

Here is my code :

@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
public class Pet implements java.io.Serializable {
protected int id;
protected String name;
protected double weight;
...
}

@Entity
@Table(name = "TABLEPERCLASS_CAT")
public class Cat extends Pet {
private int lives;
...
}

@Entity
@Table(name = "TABLEPERCLASS_DOG")
public class Dog extends Pet {
private int numBones;
...
}

Then, here is the script which create needed tables :

create table TABLEPERCLASS_CAT (
ID bigint,
LIVES bigint,
NAME varchar,
WEIGHT double,
primary key (ID)
);

create table TABLEPERCLASS_DOG (
ID bigint,
NUMBONES bigint,
NAME varchar,
WEIGHT double,
primary key (ID)
);

Then, I try to list all pets, here is the corresponding method :

public Collection<Pet> findAllPets() {
...
try {
Query q = entityManager.createQuery("from Pet");
Collection resultList = q.getResultList();
return resultList;
} catch (RuntimeException e) {
throw e;
}
finally {
entityManager.close();
}
}
}

The only result I have is a Hibernate error :

2006-04-19 15:39:58,460 WARN [org.hibernate.util.JDBCExceptionReporter] - SQL Error: -22, SQLState: S0002
2006-04-19 15:39:58,476 ERROR [org.hibernate.util.JDBCExceptionReporter] - Table not found in statement [select petx0_.id as id7_, petx0_.weight as weight7_, petx0_.name as name7_, petx0_.lives as lives8_, petx0_.numBones as numBones9_, petx0_.clazz_ as clazz_ from ( select null as numBones, null as lives, name, weight, id, 0 as clazz_ from Pet union select null as numBones, lives, name, weight, id, 1 as clazz_ from TABLEPERCLASS_CAT union select numBones, null as lives, name, weight, id, 2 as clazz_ from TABLEPERCLASS_DOG ) petx0_]

When analysing the request above, i remark that it doesn't seem to correspond to the table-per-class strategy as the request try to access a "Pet" table...
So I was wondering if table-per-request was well supported in the current Hibernate release. Or am i doing something wrong ?


Top
 Profile  
 
 Post subject:
PostPosted: Thu Apr 20, 2006 3:27 am 
Expert
Expert

Joined: Sat Oct 25, 2003 8:49 am
Posts: 490
Location: Vrhnika, Slovenia
Define your Pet as abstract.


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.