2011-04-07 107 views
0

嗨 IM使用與支柱冬眠..Hibernate的問題

我想一個表中的數據複製到另一個表,所以我做了這樣的事情...

public boolean updateAllScrutinyTables() { 
     // TODO Auto-generated method stub 
     Session session = HibernateUtil.getSessionFactory().getCurrentSession(); 
     String query = ""; 

     try{ 
      query = "insert into SRequisitionAge select * from RequisitionAge ra where ra.Requisition.id = 7"; 


      session.beginTransaction(); 
      session.createQuery(query).executeUpdate(); 
      session.getTransaction().commit(); 
      return true; 
     }catch(Exception e){ 
      e.printStackTrace(); 
      Log.error("Error in SRequisitionPostDAOImpl.updateAllScrutinyTables() : "+e.toString()); 
      return false; 
     } 
    } 

當我將執行此我我會得到這個錯誤

org.hibernate.hql.ast.QuerySyntaxException: expecting OPEN, found 'select' near line 1, column 29 [insert into SRequisitionAge select * from com.sttl.rpsc.dataobject.RequisitionAge ra where ra.Requisition.id = 7] 
    at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:54) 
    at org.hibernate.hql.ast.QuerySyntaxException.convert(QuerySyntaxException.java:47) 
    at org.hibernate.hql.ast.ErrorCounter.throwQueryException(ErrorCounter.java:82) 
    at org.hibernate.hql.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:281) 
    at org.hibernate.hql.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:180) 
    at org.hibernate.hql.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:134) 
    at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:101) 
    at org.hibernate.engine.query.HQLQueryPlan.<init>(HQLQueryPlan.java:80) 
    at org.hibernate.engine.query.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:94) 
    at org.hibernate.impl.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:156) 
    at org.hibernate.impl.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:135) 
    at org.hibernate.impl.SessionImpl.createQuery(SessionImpl.java:1650) 
    at sun.reflect.GeneratedMethodAccessor256.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at org.hibernate.context.ThreadLocalSessionContext$TransactionProtectionWrapper.invoke(ThreadLocalSessionContext.java:342) 
    at $Proxy7.createQuery(Unknown Source) 
    at com.sttl.rpsc.dao.SRequisitionPostDAOImpl.updateAllScrutinyTables(SRequisitionPostDAOImpl.java:135) 
    at com.sttl.rpsc.service.RequisitionServiceImpl.updateAllScrutinizeReqTables(RequisitionServiceImpl.java:268) 
    at com.sttl.rpsc.action.RequisitionAction.setScrutinizePostRequisition(RequisitionAction.java:1894) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) 
    at java.lang.reflect.Method.invoke(Unknown Source) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:441) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:280) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:243) 
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:89) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237) 
    at com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:122) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237) 
    at com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195) 
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237) 
    at org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237) 
    at com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:138) 
    at com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237) 
    at org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237) 
    at com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176) 
    at com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237) 
    at org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52) 
    at org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:488) 
    at org.apache.struts2.dispatcher.FilterDispatcher.doFilter(FilterDispatcher.java:395) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 
    at org.displaytag.filter.ResponseOverrideFilter.doFilter(ResponseOverrideFilter.java:125) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298) 
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859) 
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588) 
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) 
    at java.lang.Thread.run(Unknown Source) 

所以最新的問題??我可以在冬眠時做這樣的事情嗎?如果沒有那麼最新的選擇?

回答

0

也許你並不需要太多參與尖銳溼疣的休眠狀態,這樣你就可以用本機查詢做到這一點 - createSQLQuery(..)

除此之外,您可以:

  • 選擇所有實體
  • 迭代它們,並在每次迭代時從新類型創建一個新對象,並設置保存新實體的相應字段

這將需要更多的疑問,雖然

+0

嗨bozho,謝謝你的回覆,但我沒有在我的系統中使用NativeQuery的權限,所以我必須使用創建查詢,所以是有任何替代或我必須在這裏更正任何東西? – Sanju 2011-04-07 10:58:18

0

試着改變你的查詢是這樣的:

Query query = session.createQuery("insert into Stock(stock_code, stock_name)" + 
       "select stock_code, stock_name from backup_stock"); 

希望這有助於。

+0

嗨nikunj,謝謝你的回覆,我曾試過這個(http://www.mkyong.com/hibernate/hibernate-query-examples-hql/),nu ti在我的表中有一些參考,所以我必須做出查詢字段名或查詢中的POJO名稱? – Sanju 2011-04-07 11:06:59

+0

應該是HQL的POJO屬性。 – Reddy 2011-04-07 11:11:56

+1

INSERT語句的僞語法是:INSERT INTO EntityName properties_list select_statement。 只支持INSERT INTO ... SELECT ...表單;不是INSERT INTO ... VALUES ...表單。 properties_list類似於SQL INSERT語句中的列規範。對於涉及映射繼承的實體,只能在properties_list中使用直接定義在給定類級別上的屬性。超類屬性是不允許的,子類屬性是不合理的。換句話說,INSERT語句本質上是非多態的。 – nIKUNJ 2011-04-07 11:17:22