use of org.hibernate.engine.spi.SessionImplementor in project hibernate-orm by hibernate.
the class MultipleSessionCollectionTest method testDeleteCommitCopyToNewOwnerInNewSession.
@Test
@TestForIssue(jiraKey = "HHH-9518")
public void testDeleteCommitCopyToNewOwnerInNewSession() {
Parent p1 = new Parent();
p1.nickNames.add("nick");
Parent p2 = new Parent();
Session s1 = openSession();
s1.getTransaction().begin();
s1.save(p1);
s1.save(p2);
s1.getTransaction().commit();
s1.close();
s1 = openSession();
s1.getTransaction().begin();
s1.delete(p1);
s1.flush();
s1.getTransaction().commit();
// need to commit afterQuery flushing; otherwise, will get lock failure when try to move the collection below
assertNull(((SessionImplementor) s1).getPersistenceContext().getEntry(p1));
CollectionEntry ceChildren = ((SessionImplementor) s1).getPersistenceContext().getCollectionEntry((PersistentCollection) p1.children);
CollectionEntry ceNickNames = ((SessionImplementor) s1).getPersistenceContext().getCollectionEntry((PersistentCollection) p1.nickNames);
assertNull(ceChildren);
assertNull(ceNickNames);
assertNull(((AbstractPersistentCollection) p1.children).getSession());
assertNull(((AbstractPersistentCollection) p1.oldChildren).getSession());
assertNull(((AbstractPersistentCollection) p1.nickNames).getSession());
assertNull(((AbstractPersistentCollection) p1.oldNickNames).getSession());
// Assign the deleted collection to a different entity with same collection role (p2.nickNames)
p2.nickNames = p1.nickNames;
Session s2 = openSession();
s2.getTransaction().begin();
s2.saveOrUpdate(p2);
s2.getTransaction().commit();
s2.close();
s1.close();
}
use of org.hibernate.engine.spi.SessionImplementor in project hibernate-orm by hibernate.
the class MultipleSessionCollectionTest method testDeleteCommitCopyToNewOwnerNewCollectionRoleInNewSession.
@Test
@TestForIssue(jiraKey = "HHH-9518")
public void testDeleteCommitCopyToNewOwnerNewCollectionRoleInNewSession() {
Parent p1 = new Parent();
p1.nickNames.add("nick");
Parent p2 = new Parent();
Session s1 = openSession();
s1.getTransaction().begin();
s1.save(p1);
s1.save(p2);
s1.getTransaction().commit();
s1.close();
s1 = openSession();
s1.getTransaction().begin();
s1.delete(p1);
s1.flush();
s1.getTransaction().commit();
// need to commit afterQuery flushing; otherwise, will get lock failure when try to move the collection below
assertNull(((SessionImplementor) s1).getPersistenceContext().getEntry(p1));
CollectionEntry ceChildren = ((SessionImplementor) s1).getPersistenceContext().getCollectionEntry((PersistentCollection) p1.children);
CollectionEntry ceNickNames = ((SessionImplementor) s1).getPersistenceContext().getCollectionEntry((PersistentCollection) p1.nickNames);
assertNull(ceChildren);
assertNull(ceNickNames);
assertNull(((AbstractPersistentCollection) p1.children).getSession());
assertNull(((AbstractPersistentCollection) p1.oldChildren).getSession());
assertNull(((AbstractPersistentCollection) p1.nickNames).getSession());
assertNull(((AbstractPersistentCollection) p1.oldNickNames).getSession());
// Assign the deleted collection to a different entity with different collection role (p2.oldNickNames)
p2.oldNickNames = p1.nickNames;
Session s2 = openSession();
s2.getTransaction().begin();
s2.saveOrUpdate(p2);
s2.getTransaction().commit();
s2.close();
s1.close();
}
use of org.hibernate.engine.spi.SessionImplementor in project hibernate-orm by hibernate.
the class ClearEventListenerTest method testAutoClear.
@Test
public void testAutoClear() {
listener.callCount = 0;
Session s = openSession();
((SessionImplementor) s).setAutoClear(true);
s.beginTransaction();
assertEquals(0, listener.callCount);
s.getTransaction().commit();
assertEquals(1, listener.callCount);
s.close();
assertEquals(1, listener.callCount);
}
use of org.hibernate.engine.spi.SessionImplementor in project hibernate-orm by hibernate.
the class TestAutoFlushBeforeQueryExecution method testAutoflushIsNotRequiredWithUnrelatedCollectionChange.
@Test
public void testAutoflushIsNotRequiredWithUnrelatedCollectionChange() {
Session s = openSession();
Transaction txn = s.beginTransaction();
Publisher publisher = new Publisher();
publisher.setName("name");
s.save(publisher);
assertTrue("autoflush entity create", s.createQuery("from Publisher p").list().size() == 1);
publisher.setName("name");
assertTrue("autoflush entity update", s.createQuery("from Publisher p where p.name='name'").list().size() == 1);
UnrelatedEntity unrelatedEntity = new UnrelatedEntity();
s.save(unrelatedEntity);
txn.commit();
s.close();
s = openSession();
txn = s.beginTransaction();
unrelatedEntity = (UnrelatedEntity) s.get(UnrelatedEntity.class, unrelatedEntity.getId());
publisher = (Publisher) s.get(Publisher.class, publisher.getId());
assertTrue(publisher.getAuthors().isEmpty());
final PersistenceContext persistenceContext = ((SessionImplementor) s).getPersistenceContext();
final ActionQueue actionQueue = ((SessionImpl) s).getActionQueue();
assertEquals(1, persistenceContext.getCollectionEntries().size());
assertEquals(1, persistenceContext.getCollectionsByKey().size());
assertTrue(persistenceContext.getCollectionEntries().containsKey(publisher.getAuthors()));
assertTrue(persistenceContext.getCollectionsByKey().values().contains(publisher.getAuthors()));
assertEquals(0, actionQueue.numberOfCollectionRemovals());
Author author1 = new Author();
author1.setPublisher(publisher);
publisher.getAuthors().add(author1);
assertTrue(s.createQuery("from UnrelatedEntity").list().size() == 1);
assertEquals(2, persistenceContext.getCollectionEntries().size());
assertEquals(1, persistenceContext.getCollectionsByKey().size());
assertTrue(persistenceContext.getCollectionEntries().containsKey(publisher.getAuthors()));
assertTrue(persistenceContext.getCollectionEntries().containsKey(author1.getBooks()));
assertTrue(persistenceContext.getCollectionsByKey().values().contains(publisher.getAuthors()));
assertEquals(0, actionQueue.numberOfCollectionRemovals());
author1.setPublisher(null);
s.delete(author1);
publisher.getAuthors().clear();
assertEquals(0, actionQueue.numberOfCollectionRemovals());
assertTrue(s.createQuery("from UnrelatedEntity").list().size() == 1);
assertEquals(2, persistenceContext.getCollectionEntries().size());
assertEquals(1, persistenceContext.getCollectionsByKey().size());
assertTrue(persistenceContext.getCollectionEntries().containsKey(publisher.getAuthors()));
assertTrue(persistenceContext.getCollectionEntries().containsKey(author1.getBooks()));
assertTrue(persistenceContext.getCollectionsByKey().values().contains(publisher.getAuthors()));
assertEquals(0, actionQueue.numberOfCollectionRemovals());
Set<Author> authorsOld = publisher.getAuthors();
publisher.setAuthors(new HashSet<Author>());
Author author2 = new Author();
author2.setName("author2");
author2.setPublisher(publisher);
publisher.getAuthors().add(author2);
List results = s.createQuery("from UnrelatedEntity").list();
assertEquals(1, results.size());
assertEquals(4, persistenceContext.getCollectionEntries().size());
assertEquals(1, persistenceContext.getCollectionsByKey().size());
assertTrue(persistenceContext.getCollectionEntries().containsKey(publisher.getAuthors()));
assertTrue(persistenceContext.getCollectionEntries().containsKey(author2.getBooks()));
assertTrue(persistenceContext.getCollectionEntries().containsKey(authorsOld));
assertTrue(persistenceContext.getCollectionEntries().containsKey(author1.getBooks()));
assertTrue(persistenceContext.getCollectionsByKey().values().contains(authorsOld));
assertEquals(0, actionQueue.numberOfCollectionRemovals());
s.flush();
assertEquals(2, persistenceContext.getCollectionEntries().size());
assertEquals(2, persistenceContext.getCollectionsByKey().size());
assertTrue(persistenceContext.getCollectionEntries().containsKey(publisher.getAuthors()));
assertTrue(persistenceContext.getCollectionEntries().containsKey(author2.getBooks()));
assertTrue(persistenceContext.getCollectionsByKey().values().contains(publisher.getAuthors()));
assertTrue(persistenceContext.getCollectionsByKey().values().contains(author2.getBooks()));
assertEquals(0, actionQueue.numberOfCollectionRemovals());
s.delete(publisher);
assertTrue("autoflush delete", s.createQuery("from UnrelatedEntity").list().size() == 1);
s.delete(unrelatedEntity);
txn.commit();
s.close();
}
use of org.hibernate.engine.spi.SessionImplementor in project hibernate-orm by hibernate.
the class HQLScrollFetchTest method testIncompleteScroll.
@Test
@TestForIssue(jiraKey = "HHH-1283")
public void testIncompleteScroll() {
Session s = openSession();
s.beginTransaction();
ScrollableResults results = s.createQuery(QUERY + " order by p.name asc").scroll();
results.next();
Parent p = (Parent) results.get(0);
assertResultFromOneUser(p);
// get the other parent entity from the persistence context along with its first child
// retrieved from the resultset.
Parent pOther = null;
Child cOther = null;
for (Object entity : ((SessionImplementor) s).getPersistenceContext().getEntitiesByKey().values()) {
if (Parent.class.isInstance(entity)) {
if (entity != p) {
if (pOther != null) {
fail("unexpected parent found.");
}
pOther = (Parent) entity;
}
} else if (Child.class.isInstance(entity)) {
if (!p.getChildren().contains(entity)) {
if (cOther != null) {
fail("unexpected child entity found");
}
cOther = (Child) entity;
}
} else {
fail("unexpected type of entity.");
}
}
// check that the same second parent is obtained by calling Session.get()
assertNull(pOther);
assertNull(cOther);
s.getTransaction().commit();
s.close();
}
Aggregations