String sqlInsert ="INSERT INTO FKPT_Customer " 
+" (id, customerNumber, shortname, openedDate, salesManager, fullname, address_province, address_city, address_postalcode,"
+" address_street, address_country,businessScope, phone, email, dueTimes) values ("
+"SELECT C.id, C.customerNumber, C.shortname, CA.createdTimestamp, E.name, C.fullname, C.address_province, C.address_city, "
+" C.address_postalcode, C.address_street, C.address_country, C.businessScope, C.phone, C.email, CA.frequencyPastdue "
+" FROM Customer AS C "
+" JOIN CreditAccount AS CA ON CA.customerId=C.ID "
+" LEFT JOIN Employee2Customer AS E2C ON E2C.Cust_Id=C.ID "
+" LEFT JOIN Employee AS E ON E.ID=E2C.Emp_Id "
+" WHERE CA.customerId=C.id AND C.id IN (SELECT customerId FROM CreditAccount "
+" WHERE CA.createdTimestamp > ( "
+" SELECT C1.createdTimestamp FROM FKPT_Customer AS C1 WHERE 1=1"
+" ORDER BY C1.createdTimestamp DESC FETCH FIRST 1 ROWS ONLY)))";
Query customerInsert = session.createQuery(sqlInsert);
customerInsert.executeUpdate();

解决方案 »

  1.   

    执行时报错:
    19:26:08,091 [http-0.0.0.0-80-3] ERROR [PARSER] line 1:219: unexpected token: values
    19:26:08,107 [http-0.0.0.0-80-3] ERROR [ExceptionMappingInterceptor]
    java.lang.reflect.InvocationTargetException
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    是不是insert不能包含select?
      

  2.   

    注:往fkpt_customer表中插入的可能是多条记录,即:是结果集
      

  3.   

    你的SQL可够长哇! 你先试试在PL/SQL下能运行不,如果可以的话,程序就可以。如果PL/SQL下都运行不了就好好改改SQL吧!
      

  4.   

    把你的sql打印出来在工具上执行一下,看看能不能执行
    怀疑是你的sql有问题
      

  5.   

    Query customerInsert = session.createQuery(sqlInsert);
    customerInsert.executeUpdate();原生SQL不是这样用的,要使用 session.createSQLQuery(sqlInsert);方法
      

  6.   

    我用了session.createSQLQuery(sqlInsert);方法,这个提示只支持HQL
      

  7.   


    ? 有这种事情,那我记错了,反正你用 Hibernate Native SQl的方式,API上有一段,我不记得了。SQL查询是通过SQLQuery接口来控制的,它是通过调用Session.createSQLQuery()方法来获得 
      

  8.   

    你只要SQL正确,就用上面的 createSQLQuery 啊,我一直都是这样用的,我现在的机器上没有开发环境,没法帮你看呀,你到 googel里面,随便搜一下 hibernate 执行原生 sql,就能搜到资料啊。
      

  9.   

    真的假的啊!难道我以前做错了,无论是用本地sql,还是用hql语句都不能直接执行insert 语句的。也可能是我语句写的有问题。不过我找到一个解决办法。
    session有一个getConnection()方法,这样你可以直接拿到一个和数据库的连接。
    接着就像写jdbc时那样往数据库插入数据就可以了。
    我当时是这么解决的。
    还望高人指教………………
      

  10.   


    public List FindPayById(Integer id) throws Exception{
    return hibernateSession.createSQLQuery("select * from EmolumentLeafletInfo emo where emo.payment_Id="+id).addEntity(EmolumentLeafletInfo.class).list();

    }
    原生sql的用法   给你个例子
      

  11.   

      session.createSQLQuery(sql).executeUpdate();
                tx.commit();