Sleep Inheritance

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> 
+4
source share
2 answers

Your mappings and table structure are (approximately) correct for the JOINED inheritance strategy, and I cannot reproduce your problem.

I use the following mappings (which you basically provided):

 @Entity @Table(name = "object") @Inheritance(strategy = InheritanceType.JOINED) public class ObjectClass { @Id @GeneratedValue private Long id; public ObjectClass() { } public Long getId() { return id; } public void setId(Long id) { this.id = id; } } 

and

 @Entity @ForeignKey(name = "id") @Table(name = "code_table") public class CodeTable extends ObjectClass{ private String description; public CodeTable() { } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } @Override public String toString() { return "CodeTable [getDescription()=" + getDescription() + ", getId()=" + getId() + "]"; } } 

The following tables:

 create table code_table ( description varchar(255), id bigint not null, primary key (id) ) create table object ( id bigint not null, primary key (id) ) alter table code_table add constraint id foreign key (id) references object 

And the following parent / child entries:

 insert into object values (1); insert into code_table(id, description) values (1, 'foo'); 

And fulfill the query by the criteria:

 session.createCriteria(CodeTable.class) .setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY) .list(); 

Return:

 CodeTable [getDescription()=foo, getId()=1] 

Everything works as expected.

References

+2
source

What does your mapping look like?

Have you read this section in a Hibernate document? Sleep Inheritance Display

As you can read in the link above, your mapping is incorrect. You must let Hibernate know that the code_table class inherits from the class of the object, and you must let Hibernate know how this link exists in the database.

+1
source

All Articles