Can no longer use @MappedSuperclass and @Inheritance annotations in Hibernate 5.4

#1

Hello.

I just updated our software from hibernate orm 5.2.12 to 5.4.0 which was as easy as changing the dependency in the project. But after several tries I still encounter a problem which seems to be related to our inheritance strategy.
We have a simple POJO that we call BaseEntity. This class only defines an id property along with the generator strategy. Since we want ALL entities share the same type of id, we made this class a MappedSuperclass. We also wanted to use the JOINED InheritanceType for the whole entity hierarchy. Thus the mapping looks like this:

@MappedSuperclass
@Inheritance(strategy = InheritanceType.JOINED)
@Viewable
public abstract class BaseEntity implements Constants
{
	public final static String PROPERTY_ID = "id";

	@Id
	@Access(AccessType.PROPERTY)
	// 128bit UUID => 16 Byte => as Hex-String 32 chars + four '-' chars
	@Column(name = PROPERTY_ID, length = 36, nullable = false)
	private String id = IdGenerator.generateId();

	public void setId(final String id)
	{
		this.id = id;
	}

	public String getId()
	{
		return id;
	}
// Some more stuff here (equals, hashCode and the like
}

Till the upgrade this mapping worked like a charm for the past 6 years now. But with 5.4 I encounter the following exception when starting up the application:

org.hibernate.AnnotationException: An entity cannot be annotated with both @Inheritance and @MappedSuperclass: eu.inform.model.entities.BaseEntity
	at org.hibernate.cfg.AnnotationBinder.bindClass(AnnotationBinder.java:539)
	at org.hibernate.boot.model.source.internal.annotations.AnnotationMetadataSourceProcessorImpl.processEntityHierarchies(AnnotationMetadataSourceProcessorImpl.java:250)
	at org.hibernate.boot.model.process.spi.MetadataBuildingProcess$1.processEntityHierarchies(MetadataBuildingProcess.java:231)
	at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:274)
	at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.build(MetadataBuildingProcess.java:84)
	at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:474)
	at org.hibernate.boot.internal.MetadataBuilderImpl.build(MetadataBuilderImpl.java:85)
	at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:689)
	at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:724)
	at org.springframework.orm.hibernate5.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:615)
	at org.springframework.orm.hibernate5.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:599)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1804)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1741)
	... 27 more

Is this by design, by decision or only mistaken? What can we do to enforce our database definition? We do not want a central BaseEntity-table with millions of entries.

Kind regards,

Sebastian

0 Likes

#2

The mapping is wrong according to the JPA specs. Remove @MappedSuperclass and it should work just fine.

0 Likes

#3

Isn’t this going to materialize a BaseEntity table in the database that is joined by any subclass when selecting it? If so this is not what we want to achieve.
I worked around it and moved the @Inheritance annotation to any of the subclasses which seems to work properly.

0 Likes

#4

Yes, that’s right. You should use @Inheritance if you want to materialize the Java-based inheritance tree into the DB. If you don’t want that, you use @MappedSuperclass and you move the @Inheritance annotation to the first class down the inheritance tree that will be materialized in the DB.

I worked around it and moved the @Inheritance annotation to any of the subclasses which seems to work properly.

That’s great.

0 Likes

#5

Okay.

Than you may closed this issue.
I summarise that my previous configuration was not valid according to JPA but supported by later hibernate releases. This support stopped in 5.4.0.Final why I had to change my annotation-driven configuration.

Thank you very much.

0 Likes