我正在尝试将我的hibernate 4.3
迁移到hibernate 5.1.16
,最后得到的是QuerySyntaxException
,它一周后无法显示。
我正在使用注释进行映射,并且检查了所有查询,这些查询使用的是我的实体类的相同名称,我的查询中的名称没有冲突,这可以肯定,并且重点是它可以与Hibernate 4.3
一起使用。
网络上所有的解决方案都只指向命名conflicts.__Maintence
是我的第一个表,而映射问题则指向该表。
这是我用于映射的hibernate.cfg
。
<mapping class="wadetech.DB.entity.__Maintenance"/>
这是
my __Maintenance
类@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
中省略了我无法使用的,因为它已被提交,所以我将其更改为以下代码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+。我更喜欢休眠5.1,因为我严格需要使用jdk 1.7
最佳答案
所以最后我弄清楚了我的问题。即使异常并不能指出我们的突破点,真正的问题还是在hibernateUtils上。
在此之前,我的休眠实用程序用于休眠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();