use of org.hibernate.engine.spi.SessionImplementor 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.engine.spi.SessionImplementor in project hibernate-orm by hibernate.
the class DynamicBatchFetchTest method testDynamicBatchFetch.
@Test
public void testDynamicBatchFetch() {
Integer aId1 = createAAndB();
Integer aId2 = createAAndB();
Session s = openSession();
s.getTransaction().begin();
List resultList = s.createQuery("from A where id in (" + aId1 + "," + aId2 + ") order by id").list();
A a1 = (A) resultList.get(0);
A a2 = (A) resultList.get(1);
assertEquals(aId1, a1.getId());
assertEquals(aId2, a2.getId());
assertFalse(Hibernate.isInitialized(a1.getB()));
assertFalse(Hibernate.isInitialized(a2.getB()));
assertEquals("foo", a1.getB().getOtherProperty());
assertTrue(Hibernate.isInitialized(a1.getB()));
// a2.getB() is still uninitialized
assertFalse(Hibernate.isInitialized(a2.getB()));
// the B entity has been loaded, but is has not been made the target of a2.getB() yet.
assertTrue(((SessionImplementor) session).getPersistenceContext().containsEntity(new EntityKey(((SessionImplementor) session).getContextEntityIdentifier(a2.getB()), ((SessionImplementor) session).getFactory().getEntityPersister(B.class.getName()))));
// a2.getB() is still uninitialized; getting the ID for a2.getB() did not initialize it.
assertFalse(Hibernate.isInitialized(a2.getB()));
assertEquals("foo", a2.getB().getOtherProperty());
// now it's initialized.
assertTrue(Hibernate.isInitialized(a2.getB()));
s.getTransaction().commit();
s.close();
}
use of org.hibernate.engine.spi.SessionImplementor in project hibernate-orm by hibernate.
the class CollectionCacheEvictionTest method testCachedValueAfterEviction.
@Test
public void testCachedValueAfterEviction() {
CollectionPersister persister = sessionFactory().getCollectionPersister(Company.class.getName() + ".users");
Session session = openSession();
SessionImplementor sessionImplementor = (SessionImplementor) session;
CollectionRegionAccessStrategy cache = persister.getCacheAccessStrategy();
Object key = cache.generateCacheKey(1, persister, sessionFactory(), session.getTenantIdentifier());
Object cachedValue = cache.get(sessionImplementor, key, sessionImplementor.getTimestamp());
assertNull(cachedValue);
Company company = session.get(Company.class, 1);
//should add in cache
assertEquals(1, company.getUsers().size());
session.close();
session = openSession();
sessionImplementor = (SessionImplementor) session;
key = cache.generateCacheKey(1, persister, sessionFactory(), session.getTenantIdentifier());
cachedValue = cache.get(sessionImplementor, key, sessionImplementor.getTimestamp());
assertNotNull("Collection wasn't cached", cachedValue);
session.close();
}
use of org.hibernate.engine.spi.SessionImplementor in project hibernate-orm by hibernate.
the class MultiPathCircleCascadeTest method testEntityWithNonNullableTransientEntity.
private void testEntityWithNonNullableTransientEntity(EntityOperation operation) {
Route route = getUpdatedDetachedEntity();
Node node = (Node) route.getNodes().iterator().next();
route.getNodes().remove(node);
Route routeNew = new Route();
routeNew.setName("new route");
routeNew.getNodes().add(node);
node.setRoute(routeNew);
Session s = openSession();
s.beginTransaction();
try {
operation.doEntityOperation(node, s);
s.getTransaction().commit();
fail("should have thrown an exception");
} catch (Exception ex) {
checkExceptionFromNullValueForNonNullable(ex, ((SessionImplementor) s).getFactory().getSettings().isCheckNullability(), false, operation.isLegacy());
} finally {
s.getTransaction().rollback();
s.close();
cleanup();
}
}
use of org.hibernate.engine.spi.SessionImplementor in project hibernate-orm by hibernate.
the class MultiPathCircleCascadeTest method testEntityWithNonNullablePropSetToNull.
private void testEntityWithNonNullablePropSetToNull(EntityOperation operation) {
Route route = getUpdatedDetachedEntity();
Node node = (Node) route.getNodes().iterator().next();
node.setName(null);
Session s = openSession();
s.beginTransaction();
try {
operation.doEntityOperation(route, s);
s.getTransaction().commit();
fail("should have thrown an exception");
} catch (Exception ex) {
checkExceptionFromNullValueForNonNullable(ex, ((SessionImplementor) s).getFactory().getSettings().isCheckNullability(), true, operation.isLegacy());
} finally {
s.getTransaction().rollback();
s.close();
cleanup();
}
}
Aggregations