HIbernate无状态会话实体配置

时间:2015-11-06 11:08:18

标签: java hibernate session

我们正在使用hibernate用来自多个文件的数据填充ddbb。 Multiples线程正在处理文件,当处理x文件时,我们刷新数据库上的数据。 当我尝试在数据库上写入时,我们得到下一个错误:

  core.exception.SavingException:    org.hibernate.MappingException: Unknown entity: FilesPT
    at databaseaccess.session.GenericManagerImpl.insertBatch(GenericManagerImpl.java:75)
    at FileWriter.flushFiles(FileWriter.java:425)
Caused by: org.hibernate.MappingException: Unknown entity: FilesPT
    at org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:597)
    at org.hibernate.impl.StatelessSessionImpl.getEntityPersister(StatelessSessionImpl.java:376)
    at org.hibernate.impl.StatelessSessionImpl.insert(StatelessSessionImpl.java:80)
    at databaseaccess.dao.GenericDAOImpl.insertList(GenericDAOImpl.java:36)
    at session.GenericManagerImpl.insertBatch(GenericManagerImpl.java:72)
    ... 4 more

我的Hibernate配置文件是:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "classpath://org/hibernate/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
    <property name="hibernate.connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
    <property name="hibernate.connection.url">jdbc:oracle:thin:@%DB_HOST%:%DB_PORT%:%DB_SID%</property>
    <property name="hibernate.connection.username">%DB_USER%</property>
    <property name="hibernate.connection.password">%DB_PASS%</property>
    <property name="hibernate.dialect">org.hibernate.dialect.OracleDialect</property>
    <property name="hibernate.default_schema">%DB_SCHEMA%</property>

    <property name="hibernate.c3p0.min_size">5</property>
    <property name="hibernate.c3p0.max_size">20</property>
    <property name="hibernate.c3p0.timeout">300</property>
    <property name="hibernate.c3p0.max_statements">50</property>
    <property name="hibernate.c3p0.idle_test_period">3000</property>

    <property name="hibernate.show_sql">false</property>
    <property name="hibernate.format_sql">false</property>

    <!-- Enable Hibernate's automatic session context management -->
    <property name="current_session_context_class">thread</property>
        <mapping resource="databaseaccess/entities/Files.hbm.xml"/>
    </session-factory>
</hibernate-configuration>

文件databaseaccess / entities / Files.hbm.xml有两个不同的实体:

<hibernate-mapping>
    <class name="databaseaccess.entities.Files" table="FILES_PT" entity-name="FilesPT">
        <id name="dealId" column="FILE_ID" type="java.lang.String" length="50" />
        <property name="filename" column="FILENAME" type="java.lang.String" length="250" />
        <property name="folder" column="FOLDER" type="java.lang.String" length="30" />
    </class>
    <class name="databaseaccess.entities.Files" table="FILES_ES" entity-name="FilesES">
        <id name="dealId" column="FILE_ID" type="java.lang.String" length="50" />
        <property name="filename" column="FILENAME" type="java.lang.String" length="250" />
        <property name="folder" column="FOLDER" type="java.lang.String" length="30" />
    </class>
</hibernate-mapping>

要保存数据,我们正在使用从hibernateUtil生成的无状态会话:

public void insertList(final String entityName, final Collection entities) {
    StatelessSession session = HibernateUtil.openStatelessSession();
    try {
        Transaction tx = session.beginTransaction();
        try {
            for (Object entity : entities) {
                session.insert(entityName, entity);
            }
            tx.commit();
        } catch (final HibernateException e) {
            logger.error("Error inserting " + entities.size() + " rows in database. " + e.getMessage() + ":\n "
                    + e.getMessage());
            throw e;
        }  finally {
           tx.rollback();
        }
    } finally {
        session.close();
    }
}

hibernate util生成一个静态会话工厂

static {
    try {
        // Create the SessionFactory from config file.
        File fileCfgHibernate = new File(ConfigurationManager.getProperty("Global.properties", "HIBERNATE_CFG_FILE"));
        logger.info("Opening DB session with configuration from: " + fileCfgHibernate.getPath());
        Configuration cfgHibernate = new Configuration().configure(fileCfgHibernate);
        sessionFactory = cfgHibernate.buildSessionFactory();
    } catch (Throwable ex) {
        // Log the exception.
        logger.error("DB session creation failed." + ex);
        throw new ExceptionInInitializerError(ex);
    }
}

public static StatelessSession openStatelessSession() {
    return sessionFactory.openStatelessSession();
}

我们已经检查过会话工厂使用hibernate.cfg文件上的配置,但似乎会话没有得到配置。我们缺少什么?

0 个答案:

没有答案