I would like to implement inheritance in Hibernate.
I created an ObjectClass object:
@Entity @Table(name = "object") @Inheritance(strategy = InheritanceType.JOINED) public class ObjectClass { private id; }
and a CodeTable object that inherits from the Object class:
@Entity @ForeignKey(name = "id") @Table(name = "code_table") public class CodeTable extends ObjectClass{ private String description; }
in the database
table of objects:
CREATE TABLE `object` ( `id` bigint(11) NOT NULL auto_increment, PRIMARY KEY (`id`), )
code_table:
-
CREATE TABLE `code_table` ( `id` bigint(11) NOT NULL auto_increment, `description` varchar(45) character set latin1 default NULL, PRIMARY KEY (`id`), KEY `FK_object` (`id`), CONSTRAINT `FK_object` FOREIGN KEY (`id`) REFERENCES `object` (`id`) ON DELETE NO ACTION ON UPDATE NO ACTION, )
I wrote the following code to extract data from codeTable:
@SuppressWarnings( "unchecked" ) @Transactional( readOnly = true, propagation = Propagation.REQUIRED ) public Collection<CodeTable> findAll() { Session session = getSessionFactory().getCurrentSession(); return session.createCriteria( persistentClass ).setResultTransformer( Criteria.DISTINCT_ROOT_ENTITY ).list(); }
I get an empty list, although there is one entry in the code table.
When I write the following SQL in my database:
SELECT * FROM `code_table`
I get: id = 1, description = company.
What went wrong in my definition of hibernate? How can I get an object?
EDITED : My hibernate.cfg.xml file looks like this:
<hibernate-configuration> <session-factory> <mapping class="com.mycompany.model.CodeTable" /> <mapping class="com.mycompany.model.ObjectClass" /> </session-factory> </hibernate-configuration>