HQL 问题 !!!

HQL支持 insert into 表名1 select * from 表名2 吗
[b]问题补充:[/b]
这个是继承了org.springframework.orm.hibernate3.support.HibernateDaoSupport类

——————————————————————————————————————
s = this.getSession();
String hql = "insert into Articles select a.artiId,a.artiName,a.artiSpec,a.artiModel,a.artiMarkPrice,a.artiAssociatorPrice,a.artiInfo,a.artiFlag," +
"ac.artiClassName,ab.artiBrandName,ap.artiPhotoPath " +
"from Article a,Articlass ac,Artibrand ab,Artiphoto ap" +
" where a.artiClassId = ac.artiClassId and a.artiBrandId = ab.artiBrandId and a.artiPhotoId = ap.artiPhotoId";
t = s.beginTransaction();
s.createQuery(hql).executeUpdate();
t.commit();

出现问题
——————————————————————————————————
org.hibernate.hql.ast.QuerySyntaxException: expecting OPEN, found 'select' near line 1, column 22 [insert into Articles select a.artiId,a.artiName,a.artiSpec,a.artiModel,a.artiMarkPrice,a.artiAssociatorPrice,a.artiInfo,a.artiFlag,ac.artiClassName,ab.artiBrandName,ap.artiPhotoPath from com.articleweb.model.Article a,com.articleweb.model.Articlass ac,com.articleweb.model.Artibrand ab,com.articleweb.model.Artiphoto ap where a.artiClassId = ac.artiClassId and a.artiBrandId = ab.artiBrandId and a.artiPhotoId = ap.artiPhotoId]
at org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:59)
at org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:244)
at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:155)
at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:109)
at org.hibernate.engine.query.HQLQueryPlan.(HQLQueryPlan.java:75)
at org.hibernate.engine.query.HQLQueryPlan.(HQLQueryPlan.java:54)
at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:71)
at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:133)
at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:112)
at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1583)
at com.articleweb.model.control.ArticleControl.insertArticles(ArticleControl.java:27)
at com.articleweb.struts.action.ArticlesAction.showarti(ArticlesAction.java:47)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:280)
at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:216)
at org.springframework.web.struts.DelegatingActionProxy.execute(DelegatingActionProxy.java:106)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:507)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:151)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:874)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
at java.lang.Thread.run(Thread.java:619)

[b]问题补充:[/b]
我是在MYSQL数据上 用SQL 试过之后才到HQL用的。
既然HQL支持的就不应该有这样的问题吧。查出来的结果结构和要导入的表结果是相同的。

[b]问题补充:[/b]
Hibernate 3.1

数据库绝对支持这个方法 我试用过后才到HQL里运用的。

[b]问题补充:[/b]
要是可以用SQL的问题就不存在了。郁闷中。
[b]问题补充:[/b]
楼上的高手不是说HQL支持这个语法的吗
[b]问题补充:[/b]
这个跟关联查询有关系吗?字段和表名我都是按类对象来写的。
就是不行。 无奈了。

快背熟了 没错别字 还是不行
[b]问题补充:[/b]
insert into Articles select a.artiId,a.artiName,a.artiSpec,a.artiModel,a.artiMarkPrice,a.artiAssociatorPrice,a.artiInfo,a.artiFlag," +
"ac.artiClassName,ab.artiBrandName,ap.artiPhotoPath " +
"from Article a,Articlass ac,Artibrand ab,Artiphoto ap" +
" where a.artiClassId = ac.artiClassId and a.artiBrandId = ab.artiBrandId and a.artiPhotoId = ap.artiPhotoId
[b]问题补充:[/b]
String hql = "insert into Articles(artiId,artiName,artiSpec,artiModel,artiMarkPrice,artiAssociatorPrice,artiInfo,artiFlag,artiClassName,artiBrandName,artiPhotoPath)" +
" select a.artiId,a.artiName,a.artiSpec,a.artiModel,a.artiMarkPrice,a.artiAssociatorPrice,a.artiInfo,a.artiFlag," +
"ac.artiClassName,ab.artiBrandName,ap.artiPhotoPath " +
"from Article a,Articlass ac,Artibrand ab,Artiphoto ap" +
" where a.artiClassId = ac.artiClassId and a.artiBrandId = ab.artiBrandId and a.artiPhotoId = ap.artiPhotoId";
[b]问题补充:[/b]
异常变了
——————————————————————————————————————
org.hibernate.exception.ConstraintViolationException: could not execute update query
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.hql.ast.exec.BasicExecutor.execute(BasicExecutor.java:84)
at org.hibernate.hql.ast.QueryTranslatorImpl.executeUpdate(QueryTranslatorImpl.java:334)
at org.hibernate.engine.query.HQLQueryPlan.performExecuteUpdate(HQLQueryPlan.java:209)
at org.hibernate.impl.SessionImpl.executeUpdate(SessionImpl.java:1126)
at org.hibernate.impl.QueryImpl.executeUpdate(QueryImpl.java:94)
at com.articleweb.model.control.ArticleControl.insertArticles(ArticleControl.java:28)
at com.articleweb.struts.action.ArticlesAction.showarti(ArticlesAction.java:47)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:280)
at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:216)
at org.springframework.web.struts.DelegatingActionProxy.execute(DelegatingActionProxy.java:106)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:507)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:151)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:874)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
at java.lang.Thread.run(Thread.java:619)
Caused by: java.sql.SQLException: Duplicate entry '1' for key 1
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2921)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1570)
at com.mysql.jdbc.ServerPreparedStatement.serverExecute(ServerPreparedStatement.java:1085)
at com.mysql.jdbc.ServerPreparedStatement.executeInternal(ServerPreparedStatement.java:670)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1159)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1076)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1061)
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:102)
at org.hibernate.hql.ast.exec.BasicExecutor.execute(BasicExecutor.java:75)
... 33 more
16:58:21,437 DEBUG JDBCTransaction:152 - rollback
16:58:21,437 DEBUG JDBCTransaction:193 - re-enabling autocommit
16:58:21,437 DEBUG JDBCTransaction:163 - rolled back JDBC Connection
16:58:21,437 DEBUG QueryTranslatorImpl:236 - parse() - HQL: from com.articleweb.model.Articles
16:58:21,437 DEBUG ErrorCounter:68 - throwQueryException() : no errors
16:58:21,437 DEBUG HqlSqlBaseWalker:111 - select << begin [level=1, statement=select]
16:58:21,437 DEBUG FromElement:104 - FromClause{level=1} : com.articleweb.model.Articles (no alias) -> articles0_
16:58:21,437 DEBUG HqlSqlBaseWalker:117 - select : finishing up [level=1, statement=select]
16:58:21,437 DEBUG HqlSqlWalker:511 - processQuery() : ( SELECT ( FromClause{level=1} myweb.articles articles0_ ) )
16:58:21,453 DEBUG HqlSqlWalker:713 - Derived SELECT clause created.
16:58:21,453 DEBUG JoinProcessor:128 - Using FROM fragment [myweb.articles articles0_]
16:58:21,453 DEBUG HqlSqlBaseWalker:123 - select >> end [level=1, statement=select]
16:58:21,453 DEBUG ErrorCounter:68 - throwQueryException() : no errors
16:58:21,453 DEBUG QueryTranslatorImpl:206 - HQL: from com.articleweb.model.Articles
16:58:21,453 DEBUG QueryTranslatorImpl:207 - SQL: select articles0_.ArtiID as ArtiID11_, articles0_.ArtiName as ArtiName11_, articles0_.ArtiSpec as ArtiSpec11_, articles0_.ArtiModel as ArtiModel11_, articles0_.ArtiMarkPrice as ArtiMark5_11_, articles0_.ArtiAssociatorPrice as ArtiAsso6_11_, articles0_.ArtiInfo as ArtiInfo11_, articles0_.ArtiFlag as ArtiFlag11_, articles0_.ArtiClassName as ArtiClas9_11_, articles0_.ArtiBrandName as ArtiBra10_11_, articles0_.ArtiPhotoPath as ArtiPho11_11_ from myweb.articles articles0_
16:58:21,453 DEBUG ErrorCounter:68 - throwQueryException() : no errors
[b]问题补充:[/b]
详细异常
————————————————————————————————————————16:58:20,703 DEBUG JDBCTransaction:54 - begin
16:58:20,718 DEBUG JDBCTransaction:59 - current autocommit status: true
16:58:20,718 DEBUG JDBCTransaction:62 - disabling autocommit
16:58:20,890 DEBUG QueryTranslatorImpl:236 - parse() - HQL: insert into Articles(artiId,artiName,artiSpec,artiModel,artiMarkPrice,artiAssociatorPrice,artiInfo,artiFlag,artiClassName,artiBrandName,artiPhotoPath) select a.artiId,a.artiName,a.artiSpec,a.artiModel,a.artiMarkPrice,a.artiAssociatorPrice,a.artiInfo,a.artiFlag,ac.artiClassName,ab.artiBrandName,ap.artiPhotoPath from com.articleweb.model.Article a,com.articleweb.model.Articlass ac,com.articleweb.model.Artibrand ab,com.articleweb.model.Artiphoto ap where a.artiClassId = ac.artiClassId and a.artiBrandId = ab.artiBrandId and a.artiPhotoId = ap.artiPhotoId
16:58:20,906 DEBUG ErrorCounter:68 - throwQueryException() : no errors
16:58:21,015 DEBUG HqlSqlBaseWalker:111 - insert << begin [level=1, statement=insert]
16:58:21,062 DEBUG HqlSqlBaseWalker:111 - select << begin [level=2, statement=insert]
16:58:21,109 DEBUG FromElement:104 - FromClause{level=1} : com.articleweb.model.Article (a) -> article0_
16:58:21,109 DEBUG FromElement:104 - FromClause{level=1} : com.articleweb.model.Articlass (ac) -> articlass1_
16:58:21,109 DEBUG FromElement:104 - FromClause{level=1} : com.articleweb.model.Artibrand (ab) -> artibrand2_
16:58:21,125 DEBUG FromElement:104 - FromClause{level=1} : com.articleweb.model.Artiphoto (ap) -> artiphoto3_
16:58:21,125 DEBUG FromReferenceNode:51 - Resolved : a -> ArtiId
16:58:21,125 DEBUG DotNode:541 - getDataType() : artiId -> org.hibernate.type.IntegerType@e1cfa7
16:58:21,125 DEBUG FromReferenceNode:51 - Resolved : a.artiId -> article0_.ArtiId
16:58:21,125 DEBUG FromReferenceNode:51 - Resolved : a -> ArtiId
16:58:21,125 DEBUG DotNode:541 - getDataType() : artiName -> org.hibernate.type.StringType@10811b5
16:58:21,125 DEBUG FromReferenceNode:51 - Resolved : a.artiName -> article0_.ArtiName
16:58:21,125 DEBUG FromReferenceNode:51 - Resolved : a -> ArtiId
16:58:21,125 DEBUG DotNode:541 - getDataType() : artiSpec -> org.hibernate.type.StringType@10811b5
16:58:21,125 DEBUG FromReferenceNode:51 - Resolved : a.artiSpec -> article0_.ArtiSpec
16:58:21,156 DEBUG FromReferenceNode:51 - Resolved : a -> ArtiId
16:58:21,156 DEBUG DotNode:541 - getDataType() : artiModel -> org.hibernate.type.StringType@10811b5
16:58:21,156 DEBUG FromReferenceNode:51 - Resolved : a.artiModel -> article0_.ArtiModel
16:58:21,156 DEBUG FromReferenceNode:51 - Resolved : a -> ArtiId
16:58:21,156 DEBUG DotNode:541 - getDataType() : artiMarkPrice -> org.hibernate.type.FloatType@1af5853
16:58:21,156 DEBUG FromReferenceNode:51 - Resolved : a.artiMarkPrice -> article0_.ArtiMarkPrice
16:58:21,156 DEBUG FromReferenceNode:51 - Resolved : a -> ArtiId
16:58:21,156 DEBUG DotNode:541 - getDataType() : artiAssociatorPrice -> org.hibernate.type.FloatType@1af5853
16:58:21,156 DEBUG FromReferenceNode:51 - Resolved : a.artiAssociatorPrice -> article0_.ArtiAssociatorPrice
16:58:21,171 DEBUG FromReferenceNode:51 - Resolved : a -> ArtiId
16:58:21,171 DEBUG DotNode:541 - getDataType() : artiInfo -> org.hibernate.type.StringType@10811b5
16:58:21,171 DEBUG FromReferenceNode:51 - Resolved : a.artiInfo -> article0_.ArtiInfo
16:58:21,171 DEBUG FromReferenceNode:51 - Resolved : a -> ArtiId
16:58:21,171 DEBUG DotNode:541 - getDataType() : artiFlag -> org.hibernate.type.IntegerType@e1cfa7
16:58:21,171 DEBUG FromReferenceNode:51 - Resolved : a.artiFlag -> article0_.ArtiFlag
16:58:21,171 DEBUG FromReferenceNode:51 - Resolved : ac -> ArtiClassId
16:58:21,171 DEBUG DotNode:541 - getDataType() : artiClassName -> org.hibernate.type.StringType@10811b5
16:58:21,171 DEBUG FromReferenceNode:51 - Resolved : ac.artiClassName -> articlass1_.ArtiClassName
16:58:21,171 DEBUG FromReferenceNode:51 - Resolved : ab -> ArtiBrandId
16:58:21,171 DEBUG DotNode:541 - getDataType() : artiBrandName -> org.hibernate.type.StringType@10811b5
16:58:21,171 DEBUG FromReferenceNode:51 - Resolved : ab.artiBrandName -> artibrand2_.ArtiBrandName
16:58:21,171 DEBUG FromReferenceNode:51 - Resolved : ap -> ArtiPhotoId
16:58:21,171 DEBUG DotNode:541 - getDataType() : artiPhotoPath -> org.hibernate.type.StringType@10811b5
16:58:21,171 DEBUG FromReferenceNode:51 - Resolved : ap.artiPhotoPath -> artiphoto3_.ArtiPhotoPath
16:58:21,218 DEBUG FromReferenceNode:51 - Resolved : a -> ArtiId
16:58:21,218 DEBUG DotNode:541 - getDataType() : artiClassId -> org.hibernate.type.IntegerType@e1cfa7
16:58:21,218 DEBUG FromReferenceNode:51 - Resolved : a.artiClassId -> article0_.ArtiClassId
16:58:21,218 DEBUG FromReferenceNode:51 - Resolved : ac -> ArtiClassId
16:58:21,218 DEBUG DotNode:541 - getDataType() : artiClassId -> org.hibernate.type.IntegerType@e1cfa7
16:58:21,218 DEBUG FromReferenceNode:51 - Resolved : ac.artiClassId -> articlass1_.ArtiClassId
16:58:21,218 DEBUG FromReferenceNode:51 - Resolved : a -> ArtiId
16:58:21,218 DEBUG DotNode:541 - getDataType() : artiBrandId -> org.hibernate.type.IntegerType@e1cfa7
16:58:21,218 DEBUG FromReferenceNode:51 - Resolved : a.artiBrandId -> article0_.ArtiBrandId
16:58:21,218 DEBUG FromReferenceNode:51 - Resolved : ab -> ArtiBrandId
16:58:21,218 DEBUG DotNode:541 - getDataType() : artiBrandId -> org.hibernate.type.IntegerType@e1cfa7
16:58:21,218 DEBUG FromReferenceNode:51 - Resolved : ab.artiBrandId -> artibrand2_.ArtiBrandId
16:58:21,218 DEBUG FromReferenceNode:51 - Resolved : a -> ArtiId
16:58:21,218 DEBUG DotNode:541 - getDataType() : artiPhotoId -> org.hibernate.type.IntegerType@e1cfa7
16:58:21,234 DEBUG FromReferenceNode:51 - Resolved : a.artiPhotoId -> article0_.ArtiPhotoId
16:58:21,234 DEBUG FromReferenceNode:51 - Resolved : ap -> ArtiPhotoId
16:58:21,234 DEBUG DotNode:541 - getDataType() : artiPhotoId -> org.hibernate.type.IntegerType@e1cfa7
16:58:21,234 DEBUG FromReferenceNode:51 - Resolved : ap.artiPhotoId -> artiphoto3_.ArtiPhotoId
16:58:21,234 DEBUG HqlSqlBaseWalker:117 - select : finishing up [level=2, statement=insert]
16:58:21,234 DEBUG HqlSqlWalker:511 - processQuery() : ( SELECT ( {select clause} ( article0_.ArtiId ArtiId artiId ) ( article0_.ArtiName ArtiId artiName ) ( article0_.ArtiSpec ArtiId artiSpec ) ( article0_.ArtiModel ArtiId artiModel ) ( article0_.ArtiMarkPrice ArtiId artiMarkPrice ) ( article0_.ArtiAssociatorPrice ArtiId artiAssociatorPrice ) ( article0_.ArtiInfo ArtiId artiInfo ) ( article0_.ArtiFlag ArtiId artiFlag ) ( articlass1_.ArtiClassName ArtiClassId artiClassName ) ( artibrand2_.ArtiBrandName ArtiBrandId artiBrandName ) ( artiphoto3_.ArtiPhotoPath ArtiPhotoId artiPhotoPath ) ) ( FromClause{level=1} myweb.article article0_ myweb.articlass articlass1_ myweb.artibrand artibrand2_ myweb.artiphoto artiphoto3_ ) ( where ( and ( and ( = ( article0_.ArtiClassId ArtiId artiClassId ) ( articlass1_.ArtiClassId ArtiClassId artiClassId ) ) ( = ( article0_.ArtiBrandId ArtiId artiBrandId ) ( artibrand2_.ArtiBrandId ArtiBrandId artiBrandId ) ) ) ( = ( article0_.ArtiPhotoId ArtiId artiPhotoId ) ( artiphoto3_.ArtiPhotoId ArtiPhotoId artiPhotoId ) ) ) ) )
16:58:21,281 DEBUG JoinProcessor:128 - Using FROM fragment [myweb.artiphoto artiphoto3_]
16:58:21,281 DEBUG JoinProcessor:128 - Using FROM fragment [myweb.artibrand artibrand2_]
16:58:21,281 DEBUG JoinProcessor:128 - Using FROM fragment [myweb.articlass articlass1_]
16:58:21,281 DEBUG JoinProcessor:128 - Using FROM fragment [myweb.article article0_]
16:58:21,281 DEBUG HqlSqlBaseWalker:123 - select >> end [level=2, statement=insert]
16:58:21,281 DEBUG HqlSqlBaseWalker:117 - insert : finishing up [level=1, statement=insert]
16:58:21,281 DEBUG HqlSqlBaseWalker:123 - insert >> end [level=1, statement=insert]
16:58:21,281 DEBUG ErrorCounter:68 - throwQueryException() : no errors
16:58:21,328 DEBUG ErrorCounter:68 - throwQueryException() : no errors
16:58:21,437 WARN JDBCExceptionReporter:71 - SQL Error: 1062, SQLState: 23000
16:58:21,437 ERROR JDBCExceptionReporter:72 - Duplicate entry '1' for key 1
org.hibernate.exception.ConstraintViolationException: could not execute update query
at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:71)
at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:43)
at org.hibernate.hql.ast.exec.BasicExecutor.execute(BasicExecutor.java:84)
at org.hibernate.hql.ast.QueryTranslatorImpl.executeUpdate(QueryTranslatorImpl.java:334)
at org.hibernate.engine.query.HQLQueryPlan.performExecuteUpdate(HQLQueryPlan.java:209)
at org.hibernate.impl.SessionImpl.executeUpdate(SessionImpl.java:1126)
at org.hibernate.impl.QueryImpl.executeUpdate(QueryImpl.java:94)
at com.articleweb.model.control.ArticleControl.insertArticles(ArticleControl.java:28)
at com.articleweb.struts.action.ArticlesAction.showarti(ArticlesAction.java:47)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:280)
at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:216)
at org.springframework.web.struts.DelegatingActionProxy.execute(DelegatingActionProxy.java:106)
at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:484)
at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:274)
at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1482)
at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:507)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:269)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:188)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:174)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:117)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:108)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:151)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:874)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:665)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:528)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:81)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:689)
at java.lang.Thread.run(Thread.java:619)
Caused by: java.sql.SQLException: Duplicate entry '1' for key 1
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2921)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1570)
at com.mysql.jdbc.ServerPreparedStatement.serverExecute(ServerPreparedStatement.java:1085)
at com.mysql.jdbc.ServerPreparedStatement.executeInternal(ServerPreparedStatement.java:670)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1159)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1076)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1061)
at org.apache.commons.dbcp.DelegatingPreparedStatement.executeUpdate(DelegatingPreparedStatement.java:102)
at org.hibernate.hql.ast.exec.BasicExecutor.execute(BasicExecutor.java:75)
... 33 more
16:58:21,437 DEBUG JDBCTransaction:152 - rollback
16:58:21,437 DEBUG JDBCTransaction:193 - re-enabling autocommit
16:58:21,437 DEBUG JDBCTransaction:163 - rolled back JDBC Connection
16:58:21,437 DEBUG QueryTranslatorImpl:236 - parse() - HQL: from com.articleweb.model.Articles
16:58:21,437 DEBUG ErrorCounter:68 - throwQueryException() : no errors
16:58:21,437 DEBUG HqlSqlBaseWalker:111 - select << begin [level=1, statement=select]
16:58:21,437 DEBUG FromElement:104 - FromClause{level=1} : com.articleweb.model.Articles (no alias) -> articles0_
16:58:21,437 DEBUG HqlSqlBaseWalker:117 - select : finishing up [level=1, statement=select]
16:58:21,437 DEBUG HqlSqlWalker:511 - processQuery() : ( SELECT ( FromClause{level=1} myweb.articles articles0_ ) )
16:58:21,453 DEBUG HqlSqlWalker:713 - Derived SELECT clause created.
16:58:21,453 DEBUG JoinProcessor:128 - Using FROM fragment [myweb.articles articles0_]
16:58:21,453 DEBUG HqlSqlBaseWalker:123 - select >> end [level=1, statement=select]
16:58:21,453 DEBUG ErrorCounter:68 - throwQueryException() : no errors
16:58:21,453 DEBUG QueryTranslatorImpl:206 - HQL: from com.articleweb.model.Articles
16:58:21,453 DEBUG QueryTranslatorImpl:207 - SQL: select articles0_.ArtiID as ArtiID11_, articles0_.ArtiName as ArtiName11_, articles0_.ArtiSpec as ArtiSpec11_, articles0_.ArtiModel as ArtiModel11_, articles0_.ArtiMarkPrice as ArtiMark5_11_, articles0_.ArtiAssociatorPrice as ArtiAsso6_11_, articles0_.ArtiInfo as ArtiInfo11_, articles0_.ArtiFlag as ArtiFlag11_, articles0_.ArtiClassName as ArtiClas9_11_, articles0_.ArtiBrandName as ArtiBra10_11_, articles0_.ArtiPhotoPath as ArtiPho11_11_ from myweb.articles articles0_
16:58:21,453 DEBUG ErrorCounter:68 - throwQueryException() : no errors

15个回答

主键冲突了,嘿嘿

this.getHibernateTemplate().executeUpdate(new HibernateCallback(){

public Object doInHibernate(Session s) throws HibernateException,SQLException{
String sql = "insert into KybasicAttachment(attName,attUrl) select attName,attUrl from KybasicAttachmentTem t";
session.createQuery(sql).executeUpdate();

});
}

这种语法本身就跟你的数据库有关系,底层数据库不支持怎么搞也不行的,貌似mysql就不支持这种语法

String hql="insert into A (AName,ASex) select b.BName,b.BSex from B b";
getSession().createQuery(hql).executeUpdate();

要确认你的数据库是否支持这种语法..
另外你Hibernate版本是多少..

this.getHibernateTemplate().executeUpdate(new HibernateCallback(){

public Object doInHibernate(Session s) throws HibernateException,SQLException{
Connection conn=s.getDataSource.getConnection();
Statement stmt =con.createStatement();
stmt.executeUpdate("insert into A (AName,ASex) select b.BName,b.BSex from B b");
stmt.close();
conn.close();
}
}

实在不行的话.你可以这种方法..
另外还有一种方法是建一个本地方法.
getSession().createSqlQuery(hql).executeUpdate

不用再试了,这是Hibernate的bug,目前还没有被修复,Hibernate的jira上已经很多人提出了,网上也很多
[url]http://opensource.atlassian.com/projects/hibernate/browse/HHH-1397[/url]
[url]http://lists.jboss.org/pipermail/hibernate-issues/2007-June/005604.html[/url]
[url]http://forum.hibernate.org/viewtopic.php?t=960800[/url]
[url]http://lists.jboss.org/pipermail/hibernate-issues/2007-June/005606.html[/url]
如果还抱有一丝希望建议换成最新版本Hibernate试试,如果还不行建议实用SQLQuery吧!

换成最新版本Hibernate试试吧,也许已经修正这个BUG了呢,不过Hibernate官方并没有明确说要支持这种语法的,也别抱太大希望

你把后面的关联查询去掉试试..
使用HQL语句来写insert into select,按着写法,表名和字段名都按POJO对象来写是可以的

QuerySyntaxException: expecting OPEN, found 'select'

语法分析错误..你能不能把执行的语句贴上来呀..

insert into Articles (把POJO的字段名写上..)

共15条数据 1 尾页
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问