Example usage for org.hibernate.dialect Dialect getSequenceNextValString

List of usage examples for org.hibernate.dialect Dialect getSequenceNextValString

Introduction

In this page you can find the example usage for org.hibernate.dialect Dialect getSequenceNextValString.

Prototype

public String getSequenceNextValString(String sequenceName) throws MappingException 

Source Link

Document

Generate the appropriate select statement to to retrieve the next value of a sequence.

Usage

From source file:com.tsoft.app.domain.identifier.StringSequenceIdentifier.java

@Override
public void configure(Type type, Properties params, ServiceRegistry serviceRegistry) throws MappingException {
    final JdbcEnvironment jdbcEnvironment = serviceRegistry.getService(JdbcEnvironment.class);
    final Dialect dialect = jdbcEnvironment.getDialect();

    final ConfigurationService configurationService = serviceRegistry.getService(ConfigurationService.class);
    String globalEntityIdentifierPrefix = configurationService.getSetting("entity.identifier.prefix",
            String.class, "SEQ_");

    sequencePrefix = ConfigurationHelper.getString(SEQUENCE_PREFIX, params, globalEntityIdentifierPrefix);

    final String sequencePerEntitySuffix = ConfigurationHelper.getString(
            SequenceStyleGenerator.CONFIG_SEQUENCE_PER_ENTITY_SUFFIX, params,
            SequenceStyleGenerator.DEF_SEQUENCE_SUFFIX);

    final String defaultSequenceName = ConfigurationHelper
            .getBoolean(SequenceStyleGenerator.CONFIG_PREFER_SEQUENCE_PER_ENTITY, params, false)
                    ? params.getProperty(JPA_ENTITY_NAME) + sequencePerEntitySuffix
                    : SequenceStyleGenerator.DEF_SEQUENCE_NAME;

    sequenceCallSyntax = dialect.getSequenceNextValString(
            ConfigurationHelper.getString(SequenceStyleGenerator.SEQUENCE_PARAM, params, defaultSequenceName));
}

From source file:net.sf.mmm.persistence.impl.hibernate.HibernateSequenceManager.java

License:Apache License

/**
 * {@inheritDoc}/*from   www  .j  av  a 2 s  .  c  o  m*/
 */
@Override
protected String createNextValueSql(Sequence sequence) {

    Dialect d = getDialect();
    if (d == null) {
        throw new ResourceMissingException("hibernateDialect");
    }
    return d.getSequenceNextValString(getSequenceAsString(sequence));
}

From source file:org.hyperic.tools.ant.dbupgrade.CrispoTask.java

License:Open Source License

public int createCrispo(Dialect d, ConfigResponse cr) throws SQLException {
    Statement stmt = null;//from   w  ww.j  ava  2  s .  c  o  m
    ResultSet rs = null;
    int crispoId;

    try {
        String sql = d.getSequenceNextValString(CRISPO_ID_SEQ);
        stmt = getConnection().createStatement();
        rs = stmt.executeQuery(sql);
        rs.next();
        crispoId = rs.getInt(1);

        sql = "insert into " + CRISPO_TABLE + " (id, version_col) VALUES (" + crispoId + ", 1)";
        log(sql);
        stmt.execute(sql);
    } finally {
        DBUtil.closeJDBCObjects(LOGCTX, null, stmt, rs);
    }

    for (Iterator i = cr.getKeys().iterator(); i.hasNext();) {
        String key = (String) i.next();
        String val = cr.getValue(key);
        createCrispoOpt(d, crispoId, key, val);
    }

    return crispoId;
}

From source file:org.hyperic.tools.ant.dbupgrade.CrispoTask.java

License:Open Source License

public void createCrispoOpt(Dialect d, int crispoId, String key, String val) throws SQLException {
    if (val == null || val.trim().equals(""))
        return;//  w  w  w . ja va2  s  .c om

    PreparedStatement stmt = null;
    ResultSet rs = null;
    int id;
    String sql;

    try {
        sql = d.getSequenceNextValString(CRISPO_OPT_ID_SEQ);
        stmt = getConnection().prepareStatement(sql);
        rs = stmt.executeQuery();
        rs.next();
        id = rs.getInt(1);

        DBUtil.closeJDBCObjects(LOGCTX, null, stmt, rs);

        // See if the value is too long
        String[] elem = null;
        if (val.length() > 4000) {
            elem = val.split("\\" + ARRAY_DELIMITER);
        }

        if (elem != null && elem.length > 1) {
            sql = "insert into " + CRISPO_OPT_TABLE + " (id, version_col, propkey, crispo_id) " + "VALUES ("
                    + id + ", 1, '" + key + "', " + crispoId + ")";
            stmt = getConnection().prepareStatement(sql);
        } else {
            if (elem != null) // No split, narrow string
                val = val.substring(0, 3999);

            elem = null;
            sql = "insert into " + CRISPO_OPT_TABLE + " (id, version_col, propkey, val, crispo_id) "
                    + "VALUES (" + id + ", 1, '" + key + "', ?, " + crispoId + ")";
            stmt = getConnection().prepareStatement(sql);
            stmt.setString(1, val);
        }

        log("executing query: " + sql);
        stmt.executeUpdate();
        DBUtil.closeStatement(LOGCTX, stmt);

        if (elem != null) {
            stmt = getConnection().prepareStatement(
                    "insert into " + CRISPO_ARR_TABLE + " (opt_id, val, idx) VALUES (" + id + ", ?, ?)");
            for (int i = 0; i < elem.length; i++) {
                stmt.setString(1, elem[i]);
                stmt.setInt(2, i);
                stmt.executeUpdate(sql);
            }
        }
    } finally {
        // Make sure everything's closed
        DBUtil.closeJDBCObjects(LOGCTX, null, stmt, rs);
    }
}

From source file:org.hyperic.tools.ant.dbupgrade.SST_RoleDashboard.java

License:Open Source License

private void _execute() throws BuildException {
    Statement roleStatement = null;
    Statement dashStatement = null;
    Statement checkStatement = null;
    ResultSet roleRS = null;//from   w  w  w.  j ava  2s  .c  o m
    ResultSet checkRS = null;
    Connection conn = null;

    try {
        conn = getConnection();
        if (!InstallDBUtil.checkTableExists(conn, CRISPO_OPT_TABLE)) {
            throw new BuildException("Table eam_crispo_opt doesn't exist");
        }
        if (!InstallDBUtil.checkTableExists(conn, DASH_CONFIG_TABLE)) {
            throw new BuildException("Table eam_dash_config doesn't exist");
        }
        if (!InstallDBUtil.checkTableExists(conn, CRISPO_TABLE)) {
            throw new BuildException("Table eam_crispo doesn't exist");
        }
        if (!InstallDBUtil.checkTableExists(conn, ROLE_TABLE)) {
            throw new BuildException("Table eam_role doesn't exist");
        }

        Dialect d = HQDialectUtil.getDialect(conn);
        String check_sql = "select id from " + ROLE_TABLE + " where id in (select role_id from "
                + DASH_CONFIG_TABLE + ")";
        checkStatement = conn.createStatement();
        log("executed query: " + check_sql);
        checkRS = checkStatement.executeQuery(check_sql);

        Map ignores = new HashMap();
        int check_id_col = checkRS.findColumn("id");
        while (checkRS.next()) {
            ignores.put(new Integer(checkRS.getInt(check_id_col)), "");
            System.out.println("added ignore for: " + check_id_col);
        }
        DBUtil.closeJDBCObjects(LOGCTX, null, checkStatement, checkRS);

        String sql = "select id,name from " + ROLE_TABLE;
        System.out.println("executed query: " + sql);
        roleStatement = conn.createStatement();
        roleRS = roleStatement.executeQuery(sql);

        int id_col = roleRS.findColumn("id");
        int name_col = roleRS.findColumn("name");
        int roleId;
        dashStatement = conn.createStatement();
        while (roleRS.next()) {

            String name = roleRS.getString(name_col);
            if (name.equalsIgnoreCase(RESOURCE_CREATOR_ROLE)) {
                continue;
            }
            System.out.println("creating roleid");
            roleId = roleRS.getInt(id_col);
            if (!ignores.containsKey(new Integer(roleId))) {
                // create crispo
                long crispoId;
                if (props != null) {
                    crispoId = createCrispo(d, props);
                } else
                    throw new BuildException();

                // insert role dash pref
                String seq = d.getSequenceNextValString(DASH_CONFIG_SEQ);

                ResultSet rs = dashStatement.executeQuery(seq);
                rs.next();
                long id = rs.getInt(1);
                DBUtil.closeResultSet(LOGCTX, rs);

                String insertSql = "insert into " + DASH_CONFIG_TABLE
                        + " (id, config_type, version_col, name, crispo_id, role_id, user_id)" + " values ("
                        + id + ", 'ROLE', 0, '" + name + " Dashboard', " + +crispoId + ", " + roleId
                        + ", null)";
                System.out.println("executed query: " + insertSql);
                int rows = dashStatement.executeUpdate(insertSql);
                System.out.println("rows updated: " + rows);

                // Need this to prevent Guest Roles from having a default 
                // selection dialog
                if (name.equalsIgnoreCase(GUEST_ROLE_NAME)) {
                    // Need to get the dashboard config ID

                    ConfigResponse guestRoleProps = new ConfigResponse();
                    guestRoleProps.setValue(".user.dashboard.default.id", id);
                    long prefCrispoId = createCrispo(d, guestRoleProps);

                    // Set the crispo ID as the guest user's preference
                    dashStatement.executeUpdate(
                            "update " + USER_TABLE + " set pref_crispo_id = " + prefCrispoId + " where id = 2");
                }
            }
        }
        System.out.println("done");
    } catch (SQLException e) {
        throw new BuildException(e.getMessage(), e);
    } finally {
        // don't close the connection, it is shared for all tasks
        DBUtil.closeJDBCObjects(LOGCTX, null, roleStatement, roleRS);
        DBUtil.closeJDBCObjects(LOGCTX, null, dashStatement, null);
    }
}

From source file:org.jboss.pnc.datastore.repositories.DefaultSequenceHandlerRepository.java

License:Open Source License

@Override
public Long getNextID(final String sequenceName) {

    ReturningWork<Long> maxReturningWork = new ReturningWork<Long>() {
        @Override//from w  w w.  j a  v  a  2s  . c o  m
        public Long execute(Connection connection) throws SQLException {
            DialectResolver dialectResolver = new StandardDialectResolver();
            Dialect dialect = dialectResolver.resolveDialect(getResolutionInfo(connection));
            PreparedStatement preparedStatement = null;
            ResultSet resultSet = null;
            try {
                preparedStatement = connection.prepareStatement(dialect.getSequenceNextValString(sequenceName));
                resultSet = preparedStatement.executeQuery();
                resultSet.next();
                return resultSet.getLong(1);
            } catch (SQLException e) {
                throw e;
            } finally {
                if (preparedStatement != null) {
                    preparedStatement.close();
                }
                if (resultSet != null) {
                    resultSet.close();
                }
            }

        }
    };

    Session session = (Session) entityManager.getDelegate();
    SessionFactory sessionFactory = session.getSessionFactory();

    Long maxRecord = sessionFactory.getCurrentSession().doReturningWork(maxReturningWork);
    return maxRecord;
}

From source file:org.jboss.pnc.datastore.repositories.SequenceHandlerRepository.java

License:Open Source License

public Long getNextID(final String sequenceName) {

    ReturningWork<Long> maxReturningWork = new ReturningWork<Long>() {
        @Override/*from w ww  . j a  v  a 2s. com*/
        public Long execute(Connection connection) throws SQLException {
            DialectResolver dialectResolver = new StandardDialectResolver();
            Dialect dialect = dialectResolver.resolveDialect(connection.getMetaData());
            PreparedStatement preparedStatement = null;
            ResultSet resultSet = null;
            try {
                preparedStatement = connection.prepareStatement(dialect.getSequenceNextValString(sequenceName));
                resultSet = preparedStatement.executeQuery();
                resultSet.next();
                return resultSet.getLong(1);
            } catch (SQLException e) {
                throw e;
            } finally {
                if (preparedStatement != null) {
                    preparedStatement.close();
                }
                if (resultSet != null) {
                    resultSet.close();
                }
            }

        }
    };

    Session session = (Session) entityManager.getDelegate();
    SessionFactory sessionFactory = session.getSessionFactory();

    Long maxRecord = sessionFactory.getCurrentSession().doReturningWork(maxReturningWork);
    return maxRecord;
}