插入到休眠

时间:2015-12-02 10:54:54

标签: java hibernate

我们正在将系统中的SQL查询转换为HQL,以使我们的系统数据库独立。我们正面临将插入查询从SQL转换为HQL的问题。以下是我们面临问题的查询之一。

我们已将查询转换为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>

启动时获取以下异常

[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)

知道如何解决或编码此类查询吗?

这不起作用,因为插入select查询不允许在hibernate类中使用复合键吗?

1 个答案:

答案 0 :(得分:0)

基于hibernate文档,结构应该是: INSERT INTO EntityName properties_list select_statement 来自hibernate文档的示例,您的代码应如下所示:

<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>

点击此处:HQL Syntax for INSERT