在HQL中插入stmt

时间:2013-07-23 06:51:54

标签: hibernate hql

我是HQL的新手, 想要使用INSERT stmt将数据直接插入数据库。使用以下代码时: CompanyMaster是型号名称( CompanyMaster.java

public void insertCompanyData(CompanyMaster company) {
    logger.info("Entering insertCompanyData");

    Session session = null;
    try{
        session = getSession();
        String hql = "INSERT INTO CompanyMaster (CompName,description,status) SELECT  (company.getCompName(),company.getDescription(),company.getStatus())";
        //String hql = "INSERT INTO CompanyMaster company(CompName,description,status) VALUES (company.getCompName(),company.getDescription(),company.getStatus())";
        Query query = session.createQuery(hql);
        //query.setString("groupId", groupId);
        query.executeUpdate();
    } finally{
        releaseSession(session);
    }

    logger.info("Exiting insertCompanyData");
}

我收到这样的错误

2013-07-23 09:34:53 INFO  [http-8080-2] CompanyDaoImpl.java:157 - Entering insertCompanyData
2013-07-23 09:34:53 ERROR [http-8080-2] ErrorCounter.java:56 - <AST>:0:0: unexpected end of subtree
2013-07-23 09:34:53 ERROR [http-8080-2] ErrorCounter.java:56 - <AST>:0:0: unexpected AST node: {vector}
2013-07-23 09:34:53 INFO  [http-8080-2] CompanyMasterServiceImpl.java:121 - Entering buildCompanyMasterDropdowns .. 
2013-07-23 09:34:53 INFO  [http-8080-2] CodesDaoImpl.java:27 - Entering findCodesByType()
Hibernate: select this_.CODE_MASTER_ID as CODE1_8_0_, this_.CR_TS as CR2_8_0_, this_.CR_USR as CR3_8_0_, this_.MD_TS as MD4_8_0_, this_.MD_USR as MD5_8_0_, this_.CODE_ID as CODE6_8_0_, this_.CODE_MASTER_TYPE as CODE7_8_0_, this_.CODE_DESC as CODE8_8_0_, this_.STATUS as STATUS8_0_ from FBMS.MST_CODE this_ where this_.CODE_MASTER_TYPE=? and this_.STATUS=? order by this_.CODE_DESC asc
2013-07-23 09:34:53 INFO  [http-8080-2] CodesDaoImpl.java:33 - Exiting findCodesByType() ..

请建议如何纠正此错误...

1 个答案:

答案 0 :(得分:0)

查询的选择部分必须是有效的HQL选择查询,返回与insert子句中指定的列匹配的列。

如果要从内存中的值插入新行,只需使用persist()

CompanyMaster c = new CompanyMaster();
c.setName(someName);
...
session.persist(c);