如何在spring jdbcTemplate中插入后从表中获取自定义生成Id?

时间:2017-01-13 07:16:38

标签: java sql oracle spring-jdbc

我通过在插入之前从表中获取max Id来插入一行,并且一旦插入,我将获得相同的插入ID。我将插入另一个具有我得到的ID的表。

public Boolean saveParentComponentDetails(MultiAdminComponent adminComponentListBean) {
        System.out.println("admincomp "+adminComponentListBean.getActionId());
        JdbcTemplate jdbcTemplate=new JdbcTemplate(dataSource);
        int idvalue=0;
        String key[] = {"COMPONENT_ID"};
        KeyHolder keyHolder = new GeneratedKeyHolder();
            idvalue=jdbcTemplate.update(  new PreparedStatementCreator() {

            public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {

                PreparedStatement ps =
                        connection.prepareStatement(queriesConstants.SAVE_PARENT_COMPONENT_DETAILS,key);
                    ps.setString(1, adminComponentListBean.getRowId());
                    ps.setString(2, adminComponentListBean.getComponentIdentification());
                    ps.setString(3, adminComponentListBean.getComponentName());
                    ps.setString(4,adminComponentListBean.getComponentType());
                    ps.setString(5, adminComponentListBean.getComponentState());
                    ps.setInt(6, adminComponentListBean.getRevision());
                    return ps;
                }
            },
            keyHolder);
            Long newPersonId = keyHolder.getKey().longValue();

        int idvalues=jdbcTemplate.update(queriesConstants.SAVE_COMPONENT_TO_ACTION, new Object[] {

                adminComponentListBean.getActionId(),
                newPersonId,
                "test",
                1,0
            });

        return idvalue>0;

 }

我的查询是: -

  

INSERT INTO COMPONENT_MASTER   (COMPONENT_ID,ROW_ID,COM​​PONENT_IDENTIFICATION,COMPONENT_NAME,COMPONENT_TYPE,COMPONENT_STATE,FECHA_DE_CREACION,REVISION)   SELECT MAX(COMPONENT_ID)+ 1,?,?,?,?,?,CURRENT_TIMESTAMP,?从   COMPONENT_MASTER

错误是: -

org.springframework.jdbc.BadSqlGrammarException: PreparedStatementCallback; bad SQL grammar []; nested exception is java.sql.SQLSyntaxErrorException: ORA-00933: SQL command not properly ended
12:32:49,105 ERROR [stderr] (default task-26) 
12:32:49,106 ERROR [stderr] (default task-26)   at org.springframework.jdbc.support.SQLExceptionSubclassTranslator.doTranslate(SQLExceptionSubclassTranslator.java:91)
12:32:49,107 ERROR [stderr] (default task-26)   at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:73)
12:32:49,108 ERROR [stderr] (default task-26)   at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
12:32:49,108 ERROR [stderr] (default task-26)   at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:645)
12:32:49,108 ERROR [stderr] (default task-26)   at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:900)
12:32:49,108 ERROR [stderr] (default task-26)   at com.caja.creditprocess.daoimpl.AdminComponentDAOImpl.saveParentComponentDetails(AdminComponentDAOImpl.java:55)
12:32:49,108 ERROR [stderr] (default task-26)   at com.caja.creditprocess.adminmanager.AdminComponentEjb.saveComponentDetails(AdminComponentEjb.java:41)
12:32:49,108 ERROR [stderr] (default task-26)   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
12:32:49,108 ERROR [stderr] (default task-26)   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
12:32:49,108 ERROR [stderr] (default task-26)   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
12:32:49,108 ERROR [stderr] (default task-26)   at java.lang.reflect.Method.invoke(Method.java:498)

提前致谢。

0 个答案:

没有答案