org.hibernate.MappingException:在实体[ab.cdef.ghijklm.db.entity.RefOnlineShop]

时间:2018-08-29 10:32:41

标签: hibernate mappingexception

启动TOMCAT 8.0.27时,出现以下异常:

org.hibernate.MappingException: property [refCompany] not found on entity [ab.cdef.ghijklm.db.entity.RefOnlineShop]

实体类是使用Hibernate Tools 3.2.2GA自动生成的。

<!-- Hibernate library -->
<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-core</artifactId>
    <version>4.2.0.Final</version>
</dependency>

我有一个hibernate.cfg.xml文件(但没有hibernate.properties文件):

<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
<session-factory name="sessionFactory">
    <!-- Database connection settings -->
    <property name="connection.driver_class">org.postgresql.Driver</property>
    <property name="connection.url">jdbc:postgresql://localhost:5432/mydb</property>
    <property name="connection.username">myname</property>
    <property name="connection.password">mypwd</property>

    <!-- JDBC connection pool (use the built-in) -->
    <property name="connection.pool_size">1</property>

    <!-- SQL dialect -->
    <property name="dialect">org.hibernate.dialect.PostgreSQLDialect</property>

    <!-- Enable Hibernate's automatic session context management -->
    <property name="current_session_context_class">thread</property>

    <!-- Disable the second-level cache  -->
    <property name="cache.provider_class">org.hibernate.cache.NoCacheProvider</property>

    <!-- Echo all executed SQL to stdout -->
    <property name="show_sql">true</property>

    <property name="hbm2ddl.auto">validate</property>

</session-factory>

和hibernate.reveng.xml(此处仅删除了与问题相关的两个表/其他表已删除):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-reverse-engineering PUBLIC "-//Hibernate/Hibernate Reverse Engineering DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-reverse-engineering-3.0.dtd" >

<hibernate-reverse-engineering>

    <type-mapping>
        <!-- jdbc-type is name for java.sql.Types -->
        <sql-type jdbc-type="BIT" not-null="true" hibernate-type="Boolean" />
    </type-mapping>

    <table-filter match-schema="public" package="ab.cdef.ghijklm.db.entity" match-name="ref_online_shop" />
    <table-filter match-schema="public" package="ab.cdef.ghijklm.db.entity" match-name="ref_company" />


    <table schema="public" name="ref_online_shop">
        <primary-key>
            <generator class="sequence-identity">
                <param name="sequence">ref_online_shop_id_seq</param>
            </generator>
        </primary-key>
    </table>

    <table schema="public" name="ref_company">
        <primary-key>
            <generator class="sequence-identity">
                <param name="sequence">ref_company_id_seq</param>
            </generator>
        </primary-key>
    </table>

</hibernate-reverse-engineering>

创建底层数据库表的SQL:

CREATE TABLE public.ref_online_shop
(
    id bigint NOT NULL DEFAULT nextval('ref_online_shop_id_seq'::regclass),
    name character varying COLLATE pg_catalog."default" NOT NULL,
    company_id bigint NOT NULL,
    valid boolean NOT NULL DEFAULT true,
    CONSTRAINT ref_online_shop_pk PRIMARY KEY (id)
)

CREATE TABLE public.ref_company
(
    id bigint NOT NULL DEFAULT nextval('ref_company_id_seq'::regclass),
    name character varying COLLATE pg_catalog."default" NOT NULL,
    short_name character varying COLLATE pg_catalog."default" NOT NULL,
    country_id bigint NOT NULL,
    cdef_group boolean NOT NULL,
    domains character varying COLLATE pg_catalog."default" NOT NULL,
    valid boolean NOT NULL DEFAULT true,
    CONSTRAINT ref_company_pk PRIMARY KEY (id)
)

applicationContext.xml:

<!-- Session Factory Declaration -->
<bean id="SessionFactory"
    class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
    <property name="dataSource" ref="DataSource" />
    <property name="annotatedClasses">
        <list>
            ...
            <value>ab.cdef.ghijklm.db.entity.RefCompany</value>
            <value>ab.cdef.ghijklm.db.entity.RefOnlineShop</value>
<!-- add new entity here -->
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</prop>
            <prop key="hibernate.show_sql">false</prop>
            <!-- prop key="hibernate.current_session_context_class">thread</prop -->
            <prop key="hibernate.hbm2ddl.auto">validate</prop>
            <prop key="cache.provider_class">org.hibernate.cache.NoCacheProvider</prop>
            <prop key="hibernate.jdbc.batch_size">512</prop>
            <prop key="hibernate.generate_statistics">true</prop>
        </props>
    </property>
</bean>


<bean id="RefOnlineShopService"
    class="ab.cdef.ghijklm.service.data.ktable.impl.OnlineShopService">
    <property name="dao" ref="RefOnlineShopDAO" />
</bean>
<bean id="RefOnlineShopDAO" class="ab.cdef.ghijklm.db.dao.data.ktable.impl.OnlineShopDAO">
    <property name="sessionFactory" ref="SessionFactory" />
</bean>

<bean id="RefCompanyService" class="ab.cdef.ghijklm.service.data.ktable.impl.CompanyService">
    <property name="dao" ref="RefCompanyDAO" />
</bean>
<bean id="RefCompanyDAO" class="ab.cdef.ghijklm.db.dao.data.ktable.impl.CompanyDAO">
    <property name="sessionFactory" ref="SessionFactory" />
</bean>

这是减少的异常堆栈跟踪:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jmxExporter' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Cannot resolve reference to bean 'hibernateStatisticsBean' while setting bean property 'beans' with key [TypedStringValue: value [Hibernate:application=Statistics], target type [null]]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateStatisticsBean' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Cannot resolve reference to bean 'SessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'SessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.MappingException: property [refCompany] not found on entity [ab.cdef.ghihklm.db.entity.RefOnlineShop]
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
    at java.lang.Thread.run(Unknown Source)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateStatisticsBean' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Cannot resolve reference to bean 'SessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'SessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.MappingException: property [refCompany] not found on entity [ab.cdef.ghihklm.db.entity.RefOnlineShop]
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
    ... 26 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'SessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.MappingException: property [refCompany] not found on entity [ab.cdef.ghihklm.db.entity.RefOnlineShop]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1455)
    ... 36 more
Caused by: org.hibernate.MappingException: property [refCompany] not found on entity [ab.cdef.ghihklm.db.entity.RefOnlineShop]
    at org.hibernate.mapping.PersistentClass.getProperty(PersistentClass.java:443)
    ... 43 more
Aug 29, 2018 10:52:14 AM org.apache.catalina.core.StandardContext listenerStart
SCHWERWIEGEND: 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 'jmxExporter' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Cannot resolve reference to bean 'hibernateStatisticsBean' while setting bean property 'beans' with key [TypedStringValue: value [Hibernate:application=Statistics], target type [null]]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateStatisticsBean' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Cannot resolve reference to bean 'SessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'SessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.MappingException: property [refCompany] not found on entity [ab.cdef.ghihklm.db.entity.RefOnlineShop]
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateStatisticsBean' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Cannot resolve reference to bean 'SessionFactory' while setting bean property 'sessionFactory'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'SessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.MappingException: property [refCompany] not found on entity [ab.cdef.ghihklm.db.entity.RefOnlineShop]
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:328)
    ... 26 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'SessionFactory' defined in ServletContext resource [/WEB-INF/applicationContext.xml]: Invocation of init method failed; nested exception is org.hibernate.MappingException: property [refCompany] not found on entity [ab.cdef.ghihklm.db.entity.RefOnlineShop]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1455)
    ... 36 more
Caused by: org.hibernate.MappingException: property [refCompany] not found on entity [ab.cdef.ghihklm.db.entity.RefOnlineShop]
    at org.hibernate.mapping.PersistentClass.getProperty(PersistentClass.java:443)
    at org.hibernate.mapping.PersistentClass.getProperty(PersistentClass.java:455)
    at org.hibernate.cfg.VerifyFetchProfileReferenceSecondPass.doSecondPass(VerifyFetchProfileReferenceSecondPass.java:60)
    at org.hibernate.cfg.Configuration.originalSecondPassCompile(Configuration.java:1593)
    at org.hibernate.cfg.Configuration.secondPassCompile(Configuration.jav29 Aug 2018 10:52:14 [localhost-startStop-1] INFO  [ApplicationStartupListener(contextInitialized    : 41)] - DACAPO successfully started...do further initialization
a:1350)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1737)
    at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1788)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBuilder.buildSessionFactory(LocalSessionFactoryBuilder.java:189)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBean.buildSessionFactory(LocalSessionFactoryBean.java:350)
    at org.springframework.orm.hibernate4.LocalSessionFactoryBean.afterPropertiesSet(LocalSessionFactoryBean.java:335)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1514)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1452)
    ... 43 more

此异常的原因可能是什么,我该如何解决?

RefOnlineShop实体类:

package ab.cdef.ghijklm.db.entity;
// Generated 29.08.2018 10:26:19 by Hibernate Tools 3.2.2.GA


import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;
import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Parameter;

import ab.cdef.ghijklm.db.dao.BasicEntity;
/**
 * RefOnlineShop generated by hbm2java
 */
@Entity
@Table(name="ref_online_shop"
    ,schema="public"
)
public class RefOnlineShop extends BasicEntity  implements java.io.Serializable {


    private static final long serialVersionUID = 1L;

     private Long id;
     private String name;
     private long companyId;
     private Boolean valid = Boolean.TRUE;

    public RefOnlineShop() {
    }

    public RefOnlineShop(String name, long companyId, Boolean valid) {
       this.name = name;
       this.companyId = companyId;
       this.valid = valid;
    }

     @GenericGenerator(name="generator", strategy="sequence-identity", parameters=@Parameter(name="sequence", value="ref_online_shop_id_seq"))@Id @GeneratedValue(generator="generator")

    @Column(name="id", unique=true, nullable=false)
    public Long getId() {
        return this.id;
    }

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

    @Column(name="name", nullable=false)
    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Column(name="company_id", nullable=false)
    public long getCompanyId() {
        return this.companyId;
    }

    public void setCompanyId(long companyId) {
        this.companyId = companyId;
    }

    @Column(name="valid", nullable=false)
    public Boolean getValid() {
        return this.valid;
    }

    public void setValid(Boolean valid) {
        this.valid = valid;
    }
}

0 个答案:

没有答案