Example usage for org.springframework.jdbc.core PreparedStatementCreatorFactory setReturnGeneratedKeys

List of usage examples for org.springframework.jdbc.core PreparedStatementCreatorFactory setReturnGeneratedKeys

Introduction

In this page you can find the example usage for org.springframework.jdbc.core PreparedStatementCreatorFactory setReturnGeneratedKeys.

Prototype

public void setReturnGeneratedKeys(boolean returnGeneratedKeys) 

Source Link

Document

Set whether prepared statements should be capable of returning auto-generated keys.

Usage

From source file:org.gridobservatory.greencomputing.dao.TimeseriesDao.java

public BigInteger insert(T timeseriesType) {
    KeyHolder keyHolder = new GeneratedKeyHolder();

    PreparedStatementCreatorFactory preparedStatementCreatorFactory = new PreparedStatementCreatorFactory(
            "insert into time_series (constant_value,start_date,end_date,acquisition_count ) "
                    + "values (?, ?, ?, ?)",
            new int[] { Types.VARCHAR, Types.TIMESTAMP, Types.TIMESTAMP, Types.BIGINT });

    PreparedStatementCreator newPreparedStatementCreator = preparedStatementCreatorFactory
            .newPreparedStatementCreator(new Object[] {
                    timeseriesType.getConstantValue() != null ? timeseriesType.getConstantValue().getValue()
                            : "",
                    new Date(timeseriesType.getStartDate().longValue() * 1000),
                    new Date(timeseriesType.getEndDate().longValue() * 1000),
                    timeseriesType.getAcquisitionCount() });

    preparedStatementCreatorFactory.setReturnGeneratedKeys(true);
    this.getJdbcTemplate().update(newPreparedStatementCreator, keyHolder);
    BigInteger timeSeriesId = BigInteger.valueOf(keyHolder.getKey().longValue());

    insertTimeSeriesAcquisitions(timeSeriesId, timeseriesType.getA());

    return timeSeriesId;
}

From source file:org.easyrec.store.dao.core.impl.ItemAssocDAOMysqlImpl.java

@Override
public int insertItemAssoc(ItemAssocVO<Integer, Integer> itemAssoc) {
    // validate input parameters
    if (itemAssoc == null) {
        throw new IllegalArgumentException("missing 'itemAssoc'");
    }//from www . j a va  2 s.co m

    // validate unique key
    validateUniqueKey(itemAssoc);
    validateAssocValue(itemAssoc);
    validateViewType(itemAssoc);

    if (logger.isDebugEnabled()) {
        logger.debug("inserting 'itemAssoc': " + itemAssoc);
    }

    // @HINT: maybe use UniqueIdService later (instead of auto_imcrement)
    StringBuilder sqlString = new StringBuilder("INSERT INTO ");
    sqlString.append(DEFAULT_TABLE_NAME);
    sqlString.append(" (");
    sqlString.append(DEFAULT_TENANT_COLUMN_NAME);
    sqlString.append(", ");
    sqlString.append(DEFAULT_ITEM_FROM_COLUMN_NAME);
    sqlString.append(", ");
    sqlString.append(DEFAULT_ITEM_FROM_TYPE_COLUMN_NAME);
    sqlString.append(", ");
    sqlString.append(DEFAULT_ASSOC_TYPE_COLUMN_NAME);
    sqlString.append(", ");
    sqlString.append(DEFAULT_ASSOC_VALUE_COLUMN_NAME);
    sqlString.append(", ");
    sqlString.append(DEFAULT_ITEM_TO_COLUMN_NAME);
    sqlString.append(", ");
    sqlString.append(DEFAULT_ITEM_TO_TYPE_COLUMN_NAME);
    sqlString.append(", ");
    sqlString.append(DEFAULT_SOURCE_TYPE_COLUMN_NAME);
    sqlString.append(", ");
    sqlString.append(DEFAULT_SOURCE_INFO_COLUMN_NAME);
    sqlString.append(", ");
    sqlString.append(DEFAULT_VIEW_TYPE_COLUMN_NAME);
    sqlString.append(", ");
    if (itemAssoc.isActive() != null) {
        sqlString.append(DEFAULT_ACTIVE_COLUMN_NAME);
        sqlString.append(", ");
    }
    sqlString.append(DEFAULT_CHANGE_DATE_COLUMN_NAME);
    if (itemAssoc.getChangeDate() == null) {
        itemAssoc.setChangeDate(new Date(System.currentTimeMillis()));
    }
    if (itemAssoc.isActive() != null) {
        sqlString.append(") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
    } else {
        sqlString.append(") VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
    }

    Object[] args;
    int[] argTypes;
    if (itemAssoc.isActive() != null) {
        args = new Object[] { itemAssoc.getTenant(), itemAssoc.getItemFrom().getItem(),
                itemAssoc.getItemFrom().getType(), itemAssoc.getAssocType(), itemAssoc.getAssocValue(),
                itemAssoc.getItemTo().getItem(), itemAssoc.getItemTo().getType(), itemAssoc.getSourceType(),
                itemAssoc.getSourceInfo(), itemAssoc.getViewType(), itemAssoc.isActive(),
                itemAssoc.getChangeDate() };
        argTypes = new int[] { Types.INTEGER, Types.INTEGER, Types.INTEGER, Types.INTEGER, Types.DOUBLE,
                Types.INTEGER, Types.INTEGER, Types.INTEGER, Types.VARCHAR, Types.INTEGER, Types.BOOLEAN,
                Types.TIMESTAMP };
    } else {
        args = new Object[] { itemAssoc.getTenant(), itemAssoc.getItemFrom().getItem(),
                itemAssoc.getItemFrom().getType(), itemAssoc.getAssocType(), itemAssoc.getAssocValue(),
                itemAssoc.getItemTo().getItem(), itemAssoc.getItemTo().getType(), itemAssoc.getSourceType(),
                itemAssoc.getSourceInfo(), itemAssoc.getViewType(), itemAssoc.getChangeDate() };
        argTypes = new int[] { Types.INTEGER, Types.INTEGER, Types.INTEGER, Types.INTEGER, Types.DOUBLE,
                Types.INTEGER, Types.INTEGER, Types.INTEGER, Types.VARCHAR, Types.INTEGER, Types.TIMESTAMP };
    }
    PreparedStatementCreatorFactory factory = new PreparedStatementCreatorFactory(sqlString.toString(),
            argTypes);
    factory.setReturnGeneratedKeys(true);
    KeyHolder keyHolder = new GeneratedKeyHolder();

    int rowsAffected = getJdbcTemplate().update(factory.newPreparedStatementCreator(args), keyHolder);

    // retrieve auto increment id, and set to VO
    itemAssoc.setId(keyHolder.getKey().longValue());

    return rowsAffected;
}