Home > database >  org.hibernate.MappingException on creation of entity manager where column name differs from property
org.hibernate.MappingException on creation of entity manager where column name differs from property

Time:10-26

Currently I am upgrading an application from Hibernate 4 to Hibernate 5 (5.4.32), and hang on the following issue:

On startup of the application, when one of some data sources is initialized, I get the following exception:

Exception sending context initialized event to listener instance of class org.springframework.web.context.ContextLoaderListener
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'logger-entityManagerFactory' defined in URL [jar:file:/commons-functional-logging-service/2.0.99-SNAPSHOT/mvb-commons-functional-logging-service-2.0.99-SNAPSHOT.jar!/META-INF/spring/cfl-datasource.xml]: Invocation of init method failed; nested exception is org.hibernate.MappingException: property [properties] not found on entity [xxx.model.MiscMessageProperties]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1786)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:602)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524)
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1154)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:908)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:401)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:292)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:103)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4797)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5221)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
Caused by: org.hibernate.MappingException: property [properties] not found on entity [xxx.model.MiscMessageProperties]
    at org.hibernate.mapping.PersistentClass.getProperty(PersistentClass.java:514)
    at org.hibernate.mapping.PersistentClass.getProperty(PersistentClass.java:525)
    at org.hibernate.cfg.IndexOrUniqueKeySecondPass.doSecondPass(IndexOrUniqueKeySecondPass.java:83)
    at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1693)
    at org.hibernate.boot.internal.InFlightMetadataCollectorImpl.processSecondPasses(InFlightMetadataCollectorImpl.java:1661)
    at org.hibernate.boot.model.process.spi.MetadataBuildingProcess.complete(MetadataBuildingProcess.java:295)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.metadata(EntityManagerFactoryBuilderImpl.java:1224)
    at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:1255)
    at org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:58)
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:365)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:409)
    at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:396)
    at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.afterPropertiesSet(LocalContainerEntityManagerFactoryBean.java:341)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1845)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1782)
    ... 21 more

At no point in the XML-configuration the property "properties" is mentioned (and of course neither in the code). The configuration of "logger-entityManagerFactory" is as follows:

  <bean id="logger-entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="dataSource" ref="logger-dataSource" />
    <property name="jpaVendorAdapter">
      <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
        <property name="generateDdl" value="true" />
        <property name="database" value="${cfl.database.dialect}" />
      </bean>
    </property>
    <property name="jpaPropertyMap">
      <map>
        <entry key="javax.persistence.validation.factory" value-ref="validator" />
        <entry key="hibernate.show_sql" value="${hibernate.show_sql}"/>
        <entry key="hibernate.hbm2ddl.auto" value="${hibernate.hbm2ddl}"/>
        <entry key="org.hibernate.envers.revision_on_collection_change" value="true"/>
      </map>
    </property>
    <property name="persistenceUnitName" value="xxx.commons.logging" />
    <property name="persistenceUnitManager" ref="logger-persistenceUnitManager" />
  </bean>

The mentioned class is found via the persistence.xml and looks like this:

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;

import de.mvbonline.commons.IdGenerator;

@Entity(name = "miscmessage_properties")
public class MiscMessageProperties {

    @Id
    private PropertiesKey propertiesKey = new PropertiesKey();

    @Column(name = "properties", length = 256)
    private String value;
    
    public MiscMessageProperties() {

    }

    public String getId() {
        return this.propertiesKey.getId();
    }

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

    public String getKey() {
        return this.propertiesKey.getKey();
    }

    public void setKey(String key) {
        this.propertiesKey.setKey(key);
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }
}

As far as I know it is not uncommon and allowed to use a different name for the property and the column name. I also found lots of examples for that which looked more or less like my code. The only known issue I stumbled upon was the use of @Index annotation on the same filed, which is not the case here. With Hibernate 4 this code worked somehow.

As this is a dependency which I cannot easily change I would prefer not to solve this by renaming either column or property. What can I do to solve this?

CodePudding user response:

I guess that you should correct this:

@Entity(name = "miscmessage_properties")
public class MiscMessageProperties {
 ...

to this:

import javax.persistence.Table;

@Entity
@Table(name="miscmessage_properties")
public class MiscMessageProperties {
 ...

if your intention was to define a table name for the entity MiscMessageProperties but not renaming the entity that is not necessary in common case.

See also this section of the hibernate documentation.

CodePudding user response:

Please correct as @SternK Suggested and remove assignment on @Id property as in

@Entity
@Table(name = "miscmessage_properties")
public class MiscMessageProperties {

    @Id
    private PropertiesKey propertiesKey;
}
  • Related