use of org.hibernate.sql.SimpleSelect in project hibernate-orm by hibernate.
the class OrderByTest method testInverseIndex.
@Test
@TestForIssue(jiraKey = "HHH-5732")
public void testInverseIndex() {
final CollectionPersister transactionsPersister = sessionFactory().getCollectionPersister(BankAccount.class.getName() + ".transactions");
assertTrue(transactionsPersister.isInverse());
Session s = openSession();
s.getTransaction().begin();
BankAccount account = new BankAccount();
account.addTransaction("zzzzz");
account.addTransaction("aaaaa");
account.addTransaction("mmmmm");
s.save(account);
s.getTransaction().commit();
s.close();
s = openSession();
s.getTransaction().begin();
try {
final QueryableCollection queryableCollection = (QueryableCollection) transactionsPersister;
SimpleSelect select = new SimpleSelect(getDialect()).setTableName(queryableCollection.getTableName()).addColumn("code").addColumn("transactions_index");
PreparedStatement preparedStatement = ((SessionImplementor) s).getJdbcCoordinator().getStatementPreparer().prepareStatement(select.toStatementString());
ResultSet resultSet = ((SessionImplementor) s).getJdbcCoordinator().getResultSetReturn().extract(preparedStatement);
Map<Integer, String> valueMap = new HashMap<Integer, String>();
while (resultSet.next()) {
final String code = resultSet.getString(1);
assertFalse("code column was null", resultSet.wasNull());
final int indx = resultSet.getInt(2);
assertFalse("List index column was null", resultSet.wasNull());
valueMap.put(indx, code);
}
assertEquals(3, valueMap.size());
assertEquals("zzzzz", valueMap.get(0));
assertEquals("aaaaa", valueMap.get(1));
assertEquals("mmmmm", valueMap.get(2));
} catch (SQLException e) {
fail(e.getMessage());
} finally {
s.getTransaction().rollback();
s.close();
}
}
use of org.hibernate.sql.SimpleSelect in project hibernate-orm by hibernate.
the class PersistentListTest method testInverseListIndex.
@Test
@TestForIssue(jiraKey = "HHH-5732")
public void testInverseListIndex() {
// make sure no one changes the mapping
final CollectionPersister collectionPersister = sessionFactory().getCollectionPersister(ListOwner.class.getName() + ".children");
assertTrue(collectionPersister.isInverse());
// do some creations...
Session session = openSession();
session.beginTransaction();
ListOwner root = new ListOwner("root");
ListOwner child1 = new ListOwner("c1");
root.getChildren().add(child1);
child1.setParent(root);
ListOwner child2 = new ListOwner("c2");
root.getChildren().add(child2);
child2.setParent(root);
session.save(root);
session.getTransaction().commit();
session.close();
// now, make sure the list-index column gotten written...
final Session session2 = openSession();
session2.beginTransaction();
session2.doWork(new Work() {
@Override
public void execute(Connection connection) throws SQLException {
final QueryableCollection queryableCollection = (QueryableCollection) collectionPersister;
SimpleSelect select = new SimpleSelect(getDialect()).setTableName(queryableCollection.getTableName()).addColumn("NAME").addColumn("LIST_INDEX").addCondition("NAME", "<>", "?");
PreparedStatement preparedStatement = ((SessionImplementor) session2).getJdbcCoordinator().getStatementPreparer().prepareStatement(select.toStatementString());
preparedStatement.setString(1, "root");
ResultSet resultSet = ((SessionImplementor) session2).getJdbcCoordinator().getResultSetReturn().extract(preparedStatement);
Map<String, Integer> valueMap = new HashMap<String, Integer>();
while (resultSet.next()) {
final String name = resultSet.getString(1);
assertFalse("NAME column was null", resultSet.wasNull());
final int position = resultSet.getInt(2);
assertFalse("LIST_INDEX column was null", resultSet.wasNull());
valueMap.put(name, position);
}
assertEquals(2, valueMap.size());
// c1 should be list index 0
assertEquals(Integer.valueOf(0), valueMap.get("c1"));
// c2 should be list index 1
assertEquals(Integer.valueOf(1), valueMap.get("c2"));
}
});
session2.delete(root);
session2.getTransaction().commit();
session2.close();
}
use of org.hibernate.sql.SimpleSelect in project hibernate-orm by hibernate.
the class PessimisticReadSelectLockingStrategy method generateLockString.
protected String generateLockString(int lockTimeout) {
final SessionFactoryImplementor factory = getLockable().getFactory();
final LockOptions lockOptions = new LockOptions(getLockMode());
lockOptions.setTimeOut(lockTimeout);
final SimpleSelect select = new SimpleSelect(factory.getDialect()).setLockOptions(lockOptions).setTableName(getLockable().getRootTableName()).addColumn(getLockable().getRootTableIdentifierColumnNames()[0]).addCondition(getLockable().getRootTableIdentifierColumnNames(), "=?");
if (getLockable().isVersioned()) {
select.addCondition(getLockable().getVersionColumnName(), "=?");
}
if (factory.getSessionFactoryOptions().isCommentsEnabled()) {
select.setComment(getLockMode() + " lock " + getLockable().getEntityName());
}
return select.toStatementString();
}
use of org.hibernate.sql.SimpleSelect in project hibernate-orm by hibernate.
the class PessimisticWriteSelectLockingStrategy method generateLockString.
protected String generateLockString(int lockTimeout) {
final SessionFactoryImplementor factory = getLockable().getFactory();
final LockOptions lockOptions = new LockOptions(getLockMode());
lockOptions.setTimeOut(lockTimeout);
final SimpleSelect select = new SimpleSelect(factory.getDialect()).setLockOptions(lockOptions).setTableName(getLockable().getRootTableName()).addColumn(getLockable().getRootTableIdentifierColumnNames()[0]).addCondition(getLockable().getRootTableIdentifierColumnNames(), "=?");
if (getLockable().isVersioned()) {
select.addCondition(getLockable().getVersionColumnName(), "=?");
}
if (factory.getSessionFactoryOptions().isCommentsEnabled()) {
select.setComment(getLockMode() + " lock " + getLockable().getEntityName());
}
return select.toStatementString();
}
use of org.hibernate.sql.SimpleSelect in project hibernate-orm by hibernate.
the class SelectLockingStrategy method generateLockString.
protected String generateLockString(int timeout) {
final SessionFactoryImplementor factory = getLockable().getFactory();
final LockOptions lockOptions = new LockOptions(getLockMode());
lockOptions.setTimeOut(timeout);
final SimpleSelect select = new SimpleSelect(factory.getDialect()).setLockOptions(lockOptions).setTableName(getLockable().getRootTableName()).addColumn(getLockable().getRootTableIdentifierColumnNames()[0]).addCondition(getLockable().getRootTableIdentifierColumnNames(), "=?");
if (getLockable().isVersioned()) {
select.addCondition(getLockable().getVersionColumnName(), "=?");
}
if (factory.getSessionFactoryOptions().isCommentsEnabled()) {
select.setComment(getLockMode() + " lock " + getLockable().getEntityName());
}
return select.toStatementString();
}
Aggregations