P.Péter
P.Péter

Reputation: 1547

hibernate error when using single table inheritance

If I specify

@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@Table(name = "tablename")

for the superclass, and

@Table(name = "tablename")

for the subclasses, hibernate throws an exception during application startup (only displaying final cause):

Caused by: org.hibernate.AnnotationException: Foreign key circularity dependency involving the following tables: 
at org.hibernate.cfg.Configuration.buildRecursiveOrderedFkSecondPasses(Configuration.java:1570)
at org.hibernate.cfg.Configuration.processFkSecondPassInOrder(Configuration.java:1511)
at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.java:1420)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1844)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl$4.perform(EntityManagerFactoryBuilderImpl.java:850)
... 98 more

As you can see in the above example, it does not say which tables are involved in the dependency circularity.

If anyone has any suggestions what may be the problem I would be very happy.

Upvotes: 1

Views: 1894

Answers (1)

P.Péter
P.Péter

Reputation: 1547

Meanwhile, I managed to find the problem. The problem is simple and straightforward, only the exception message was enormously unhelpful.

The root of the problem was that there was a superclass of the superclass (lets call it baseclass) that was explicitly specified as @Inheritance(strategy = InheritanceType.JOINED) as that was intended just as a common abstract class for many-many types of objects. It would, for example, specify a common @Id field, timestamps, whatnot.

It had to be marked as an @Entity as @Id declaration would not work otherwise (and would have to specify a @Id for each subclass of baseclass). The final solution seems to be that we have to do exactly that -- specify the id separately for each subclass of baseclass and maybe creating an intermediate class for non-singletable-inherited subclasses of baseclass to avoid lots of cloning.

Update: The nice solution was to use @MappedSuperclass, so the superclass does not have to be an @Entity, but subclasses can inherit their fields and the fields' annotations, including @Id.

Upvotes: 1

Related Questions