从 hibernate 4.3 迁移到 hibernate 5.1.16 有 QuerySyntaxException

Migrating to hibernate 5.1.16 from hibernate 4.3 have QuerySyntaxException

我正在尝试将我的 hibernate 4.3 迁移到 hibernate 5.1.16,但我最终得到的是 QuerySyntaxException,一周后无法计算。

我正在使用注释进行映射,我检查了我的查询,所有这些都使用与我的实体相同的名称 class,我的查询中的名称没有冲突,这一点是肯定的,而且重点是它适用于 Hibernate 4.3

网上的所有解决方案都只指向命名conflicts.__Maintence是我的第一个table映射问题指向这个table.

这是我用来作图的hibernate.cfg

<mapping class="wadetech.DB.entity.__Maintenance"/>

这是my __Maintenanceclass

@Entity
@Table(name = "__maintenance", uniqueConstraints = @UniqueConstraint(columnNames = "name"))
public class __Maintenance implements java.io.Serializable {

这是my __MaintenanceDAO

public Collection<__Maintenance> getMaintenanceByName(String name){
    String query = "";
    query += "select m from __Maintenance m";
    query += "  where m.name = :name ";
    query += " order by ";
    query += "   m.startDate desc, m.idMaintenance desc";
    return super.list(query, "name", name);
}

这是我的例外


 wadetech.exceptions.InfrastructureException: org.hibernate.hql.internal.ast.QuerySyntaxException: __Maintenance is not mapped [select m from __Maintenance m  where m.name = :name  order by    m.startDate desc, m.idMaintenance desc]
    at wadetech.DB.base.BaseDAO.anonymousFindByQuery(BaseDAO.java:267)
    at wadetech.DB.base.BaseDAO.findByQuery(BaseDAO.java:255)
    at wadetech.DB.base.BaseDAO.list(BaseDAO.java:243)
    at wadetech.DB.DAOS.__MaintenanceDAO.getMaintenanceByName(__MaintenanceDAO.java:78)
    at com.at.project.utils.runtime.RuntimeModifier.HasExecuted(RuntimeModifier.java:128)
    at wadetech.listeners.ModificationScriptStartupListener.contextInitialized(ModificationScriptStartupListener.java:47)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5016)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5528)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1575)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1565)
    at java.util.concurrent.FutureTask.run(FutureTask.java:262)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: __Maintenance is not mapped [select m from __Maintenance m  where m.name = :name  order by    m.startDate desc, m.idMaintenance desc]
    at org.hibernate.hql.internal.ast.QuerySyntaxException.generateQueryException(QuerySyntaxException.java:79)
    at org.hibernate.QueryException.wrapWithQueryString(QueryException.java:103)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:217)
    at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:141)
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:115)
    at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:76) 

我也在添加我的 HibernateUtil 的一部分,因为我怀疑这是由于一些交易问题引起的,我在搬到 5.1 后改变了这些问题 在我使用 tx.wasCommitted();

之前

我不能再使用了,因为在 hibernate 5.1 中省略了 Committed,所以我将其更改为以下代码 tx.getStatus() == TransactionStatus.COMMITTED

这是我的 hibernateUtil

public static void beginTransaction(boolean readOnly) throws InfrastructureException {

    try {
        if( currentConnectionMode == ConnectionMode.MASTER_SLAVE && readOnly ) {
            // it is a readOnly tx
            Transaction tx = readOnlyThreadTransaction.get();
            if (null == tx || tx.getStatus() == TransactionStatus.COMMITTED || tx.getStatus() == TransactionStatus.ROLLED_BACK) {
                tx = getReadOnlySession().beginTransaction();
                readOnlyThreadTransaction.set(tx);
            }
        } else {
            Transaction tx = threadTransaction.get();
            if (null == tx || tx.getStatus() == TransactionStatus.COMMITTED || tx.getStatus() == TransactionStatus.ROLLED_BACK) {
                tx = getSession().beginTransaction();
                threadTransaction.set(tx);
            }
        } // end if
    } // end try
    catch (HibernateException ex) {
        WLog.DAOLogger.error("Begin transaction", ex);
        throw new InfrastructureException(ex);
    } // end catch
}

我想继续使用 hibernate 5.1,不想迁移到 hibernate 5.2,因为 5.2 使用 jdk 8+。我更喜欢 hibernate 5.1 因为我严格需要使用 jdk 1.7

所以最后我解决了上述问题 was.Even 尽管异常并没有帮助指出问题。真正的问题出在 hibernateUtils 上。 在此之前,我的 hiberenate utils 是如何用于 hibernate 4.3 的。

serviceRegistry = new StandardServiceRegistryBuilder().applySettings(
                        configuration.getProperties()).build();
                sessionFactory = configuration.configure().buildSessionFactory(serviceRegistry);

我把它改成了

registry = new StandardServiceRegistryBuilder().configure().build();
                 MetadataSources sources = new MetadataSources(registry);
                 Metadata metadata = sources.getMetadataBuilder().build();
                 sessionFactory = metadata.getSessionFactoryBuilder().build();