Example usage for org.hibernate.sql SimpleSelect addCondition

List of usage examples for org.hibernate.sql SimpleSelect addCondition

Introduction

In this page you can find the example usage for org.hibernate.sql SimpleSelect addCondition.

Prototype

public SimpleSelect addCondition(String[] lhs, String condition) 

Source Link

Usage

From source file:net.digitalprimates.persistence.translators.hibernate.HibernateSerializer.java

License:Open Source License

/**
 * Query the database and get a result set of IDS that belong to a specific
 * collection//from   w  w  w.  j av  a  2s .c om
 * 
 * @return
 */
private List getPkIds(SessionImplementor session, CollectionPersister persister,
        PersistentCollection collection) throws ClassNotFoundException {
    AbstractCollectionPersister absPersister = (AbstractCollectionPersister) persister;
    String[] keyNames;

    if (absPersister.isOneToMany() || absPersister.isManyToMany()) {
        keyNames = absPersister.getElementColumnNames();
    } else {
        keyNames = absPersister.getKeyColumnNames();
    }
    //String[] columnNames = absPersister.getElementColumnNames();

    SimpleSelect pkSelect = new SimpleSelect(((SessionImpl) session).getFactory().getDialect());
    pkSelect.setTableName(absPersister.getTableName());
    pkSelect.addColumns(keyNames);
    pkSelect.addCondition(absPersister.getKeyColumnNames(), "=?");

    String sql = pkSelect.toStatementString();
    List results = new ArrayList();

    try {
        // int size = absPersister.getSize(collection.getKey(), eventSession);
        Query q2 = ((SessionImpl) session).createSQLQuery(sql).setParameter(0, collection.getKey())
                .setResultTransformer(new PassThroughResultTransformer());

        List hibernateResults = q2.list();
        //return results;

        Type t = persister.getKeyType();

        PreparedStatement stmt = ((SessionImpl) session).connection().prepareStatement(sql);
        if (t instanceof StringType) {
            stmt.setString(1, collection.getKey().toString());
        } else {
            stmt.setObject(1, new Integer(collection.getKey().toString()).intValue());
        }

        ResultSet keyResults = stmt.executeQuery();

        while (keyResults.next()) {
            results.add(keyResults.getObject(1));
        }

    } catch (Exception ex) {
        ex.printStackTrace();
    }

    return results;
}