• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Hibernate referencing abstract entity for constraints

 
Sreyan Chakravarty
Ranch Hand
Posts: 65
Firefox Browser MyEclipse IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I have the following error in my Hibernate log. I am using ONLY the JPA facilities of Hibernate to map my tables to MySQL. Now the above error makes since Subject is defined like the following-:




Its an abstract entity and not a mapped super class since I need to use it in queries.

Now I have a subclass of Subject called Theory which is a normal entity.

My question is that why is hibernate TRYING to add a constraint to an abstract entity ? Should'nt it be smart enough and the see the TABLE_PER_CLASS strategy placed there. Should'nt it only refer to the child classes ? If its abstract there is no way of an entity existing.

Is this a bug in the Hibernate code ?

Using version 5.0.0.CR4
 
A.J. Côté
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I do not think it is a bug. Since you have no common table, you would need to define the constraint in every subclass table. I don't think constraints can be inherited.

Consider using InheritanceType.JOINED or even InheritanceType.SINGLE_TABLE if you want to declare the constraint only once for all your subclasses.

That is my understanding but I haven't investigated in depth.
 
Sreyan Chakravarty
Ranch Hand
Posts: 65
Firefox Browser MyEclipse IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A.J. Côté wrote:Hello,

I do not think it is a bug. Since you have no common table, you would need to define the constraint in every subclass table. I don't think constraints can be inherited.

Consider using InheritanceType.JOINED or even InheritanceType.SINGLE_TABLE if you want to declare the constraint only once for all your subclasses.

That is my understanding but I haven't investigated in depth.


Constraints are being inherited. My sub classes are being mapped with the correct constraint. I cannot use JOINED or SINGLE_TABLE because of my requirement and performance reasons.
 
A.J. Côté
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Strange... How can you tell that your constraints are inherited if export schema fails?
 
Kai Prünte
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
IMO this is a bug in Hibernate 5.
I have the same issue in Hibernate 5.0.1. The same code with Hibernate 4.x works fine.
 
Sreyan Chakravarty
Ranch Hand
Posts: 65
Firefox Browser MyEclipse IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kai Prünte wrote:IMO this is a bug in Hibernate 5.
I have the same issue in Hibernate 5.0.1. The same code with Hibernate 4.x works fine.


Are you sure it works fine in Hibernate 4 ?
 
Sreyan Chakravarty
Ranch Hand
Posts: 65
Firefox Browser MyEclipse IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A.J. Côté wrote:Strange... How can you tell that your constraints are inherited if export schema fails?


The schema export does not completely fail. Just for the table that I have given in my topic. All the other tables are exported properly.
 
Kai Prünte
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sreyan Chakravarty wrote:
Kai Prünte wrote:IMO this is a bug in Hibernate 5.
I have the same issue in Hibernate 5.0.1. The same code with Hibernate 4.x works fine.


Are you sure it works fine in Hibernate 4 ?


Yes!
 
A.J. Côté
Ranch Hand
Posts: 417
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sreyan Chakravarty wrote:
A.J. Côté wrote:Strange... How can you tell that your constraints are inherited if export schema fails?


The schema export does not completely fail. Just for the table that I have given in my topic. All the other tables are exported properly.


Strange, for me, schema export either succeeds or fails completely and no tables are created. Of course if your tables already exists in the db, hibernate won't touch them in case of failure.

 
Kai Prünte
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A.J. Côté wrote:
Sreyan Chakravarty wrote:
A.J. Côté wrote:Strange... How can you tell that your constraints are inherited if export schema fails?


The schema export does not completely fail. Just for the table that I have given in my topic. All the other tables are exported properly.


Strange, for me, schema export either succeeds or fails completely and no tables are created. Of course if your tables already exists in the db, hibernate won't touch them in case of failure.



I am using Hibernate to create the DDL scripts, but Hibernate does not execute it. I decorate the generated statements and execute them by myself. However I can compare the generated scripts from Hibernate 4.3.11 and 5.0.1. Hibernate 4 neither create the "create table" nor the "alter table" statement. Hibernate 5 creates only the "alter table" statement.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic