3

I'm trying to migrate my hibernate 4.3 to hibernate 5.1.16 and I am ending up with QuerySyntaxException which am not able to figure after one week.

I am using annotation for mapping and I checked my queries all of those uses the same name of my entity class, there is no conflict in the name in my queries which am sure and also the point is it worked with Hibernate 4.3.

All the solution in the web is only pointing to naming conflicts.__Maintence is my first table and the mapping issue is pointing at this table.

Here is my hibernate.cfg which I use for mapping.

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

This is my __Maintenance class

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

This is 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);
}

And here is my exception


 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) 

Am also adding a section of my HibernateUtil as I have my doubts this is causing due to some transaction issues which I changed after moving to 5.1 Before I used tx.wasCommitted();

which I can't use anymore as was Committed is omitted in hibernate 5.1 so I changed it to the below code tx.getStatus() == TransactionStatus.COMMITTED

Here is my 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
}

I want to stick on with hibernate 5.1 and don't want to migrate to hibernate 5.2 as 5.2 uses jdk 8+. I prefer hibernate 5.1 because I strictly need to use jdk 1.7

Lilac
  • 580
  • 1
  • 7
  • 25
  • @Dushyant Tankariya Do you have any idea about my post.It will be helpful thanks. – Lilac Sep 13 '19 at 14:29
  • Not sure, but you can have a look at [one of the StackOverflow question](https://stackoverflow.com/questions/50207195/how-to-upgrade-hibernate-from-version-4-3-to-5-2-for-migration-to-jdk-10) – Dushyant Tankariya Sep 13 '19 at 14:37
  • I want to stick with jdk 1.7 that's why I choose hibernate 5.1 as all the above versions of hibernate 5.1.16 use jdk 1.8. I have only this mapping issue which I am looking to sort out. Thanks – Lilac Sep 13 '19 at 14:40
  • Else you can go through the [github migration-guide](https://github.com/hibernate/hibernate-orm/blob/5.0/migration-guide.adoc) which may help you somehow. – Dushyant Tankariya Sep 13 '19 at 14:46
  • Possible duplicate of [How to upgrade Hibernate from version 4.3 to 5.2 for migration to JDK 10?](https://stackoverflow.com/questions/50207195/how-to-upgrade-hibernate-from-version-4-3-to-5-2-for-migration-to-jdk-10) – Sterconium Sep 13 '19 at 14:56
  • @Sterconium Thank you for your response.But Its not. I am using hibernate **5.1 **which still use ** jdk 1.7**. So I don't want to migrate my jdk. – Lilac Sep 13 '19 at 15:05
  • Then you should edit your question to specify you want to keep the same jdk – Sterconium Sep 13 '19 at 15:14
  • @Sterconium Do so,Thanks – Lilac Sep 13 '19 at 15:19

1 Answers1

2

So finally I solve the above issue was.Even though the exception din't help to point the issue. The real problem was at hibernateUtils. Before this was how my hiberenate utils for hibernate 4.3.

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

and I changed it to

registry = new StandardServiceRegistryBuilder().configure().build();
                 MetadataSources sources = new MetadataSources(registry);
                 Metadata metadata = sources.getMetadataBuilder().build();
                 sessionFactory = metadata.getSessionFactoryBuilder().build();
Lilac
  • 580
  • 1
  • 7
  • 25