简体   繁体   中英

Error in Insert into select from table in hibernate

We are converting the SQL queries in our system to HQL to make our system database independent. We are facing problem converting insert queries from SQL to HQL. Following is one of the query where we are facing problems.

We have converted the query to HQL:

<query name="Basoseext2TO_CopyFromExtrFile">
       insert into Basoseext2TO(id.location,id.masterco,id.symbol,id.policyno,id.module,id.filename,id.thedata)
       select 
       substring(id.thedata, 1, 2),
       substring(id.thedata, 3, 2),
       substring(id.thedata, 5, 3),
       substring(id.thedata, 8, 7),
       substring(id.thedata, 15, 2),
       substring(id.thedata, 17, 10),
       substring(id.thedata, 27, 3976)
       from com.csc.pt.svc.data.to.ExtrfileTO ExtrfileTO
</query>

Getting the following exceptions at startup

[org.hibernate.hql.internal.ast.ErrorCounter](ERROR): <AST>:2:36: unexpected AST node: .
    [org.hibernate.hql.internal.ast.ErrorCounter](ERROR): <AST>:2:36: unexpected AST node: .
    <AST>:2:36: unexpected AST node: .
           at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.insertablePropertySpec(HqlSqlBaseWalker.java:984)
           at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.intoClause(HqlSqlBaseWalker.java:849)
           at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.insertStatement(HqlSqlBaseWalker.java:497)
           at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:271)
           at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:248)
           at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:183)
           at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)
           at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:105)
           at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
           at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:168)
           at org.hibernate.internal.SessionFactoryImpl.checkNamedQueries(SessionFactoryImpl.java:1081)
           at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:519)
           at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1760)
           at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1798)
           at bphx.c2ab.hibernate.HibernateSessionFactory.rebuildSessionFactory(HibernateSessionFactory.java:216)
           at bphx.c2ab.hibernate.HibernateSessionFactory.rebuildSessionFactory(HibernateSessionFactory.java:462)
           at bphx.c2ab.hibernate.HibernateSessionFactory$1.run(HibernateSessionFactory.java:157)
           at java.lang.Thread.run(Thread.java:745)

    2.   
           org.hibernate.QueryException: could not resolve property:  of: com.csc.pt.svc.data.to.Basoseext2TO [
           insert into Basoseext2TO(id.location,id.masterco,id.symbol,id.policyno,id.module,id.filename,id.thedata)
           select 
           substring(id.thedata, 1, 2),
           substring(id.thedata, 3, 2),
           substring(id.thedata, 5, 3),
           substring(id.thedata, 8, 7),
           substring(id.thedata, 15, 2),
           substring(id.thedata, 17, 10),
           substring(id.thedata, 27, 3976)
           from com.csc.pt.svc.data.to.ExtrfileTO ExtrfileTO
    ]
           at org.hibernate.persister.entity.AbstractPropertyMapping.propertyException(AbstractPropertyMapping.java:83)
           at org.hibernate.persister.entity.AbstractPropertyMapping.toType(AbstractPropertyMapping.java:77) 
    at org.hibernate.persister.entity.AbstractEntityPersister.getSubclassPropertyTableNumber(AbstractEntityPersister.java:1985)
           at org.hibernate.hql.internal.ast.tree.IntoClause.isSuperclassProperty(IntoClause.java:229)
           at org.hibernate.hql.internal.ast.tree.IntoClause.visitPropertySpecNodes(IntoClause.java:171)
           at org.hibernate.hql.internal.ast.tree.IntoClause.initializeColumns(IntoClause.java:153)
           at org.hibernate.hql.internal.ast.tree.IntoClause.initialize(IntoClause.java:65)
           at org.hibernate.hql.internal.ast.HqlSqlWalker.createIntoClause(HqlSqlWalker.java:1154)
           at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.intoClause(HqlSqlBaseWalker.java:857)
           at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.insertStatement(HqlSqlBaseWalker.java:497)
           at org.hibernate.hql.internal.antlr.HqlSqlBaseWalker.statement(HqlSqlBaseWalker.java:271)
           at org.hibernate.hql.internal.ast.QueryTranslatorImpl.analyze(QueryTranslatorImpl.java:248)
           at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:183)
           at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)
           at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:105)
           at org.hibernate.engine.query.spi.HQLQueryPlan.<init>(HQLQueryPlan.java:80)
           at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:168)
           at org.hibernate.internal.SessionFactoryImpl.checkNamedQueries(SessionFactoryImpl.java:1081)
           at org.hibernate.internal.SessionFactoryImpl.<init>(SessionFactoryImpl.java:519)
           at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1760)
           at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1798)
           at bphx.c2ab.hibernate.HibernateSessionFactory.rebuildSessionFactory(HibernateSessionFactory.java:216)
           at bphx.c2ab.hibernate.HibernateSessionFactory.rebuildSessionFactory(HibernateSessionFactory.java:462)
           at bphx.c2ab.hibernate.HibernateSessionFactory$1.run(HibernateSessionFactory.java:157)
           at java.lang.Thread.run(Thread.java:745)

Any idea how to resolve or code such queries?

Is this not working because insert into select queries do not allow composite keys in hibernate class?

Based on the hibernate documentation, the structure should be: INSERT INTO EntityName properties_list select_statement Example from the hibernate documentation, you code should look like:

<query name="Basoseext2TO_CopyFromExtrFile">
       insert into Basoseext2TO(field_properties)
       select 
       substring(id.thedata, 1, 2),
       substring(id.thedata, 3, 2),
       substring(id.thedata, 5, 3),
       substring(id.thedata, 8, 7),
       substring(id.thedata, 15, 2),
       substring(id.thedata, 17, 10),
       substring(id.thedata, 27, 3976)
       from com.csc.pt.svc.data.to.ExtrfileTO ExtrfileTO
</query>

Check here: HQL Syntax for INSERT

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM