org.springframework.orm.hibernate3.HibernateTemplate Java Examples

The following examples show how to use org.springframework.orm.hibernate3.HibernateTemplate. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MaterialRepositoryIntegrationTest.java    From gocd with Apache License 2.0 6 votes vote down vote up
@Test
public void findModificationsFor_shouldCacheModifications() {
    HibernateTemplate mockTemplate = mock(HibernateTemplate.class);
    repo.setHibernateTemplate(mockTemplate);

    ArrayList modifications = new ArrayList();
    when(mockTemplate.find("FROM Modification WHERE materialId = ? AND id BETWEEN ? AND ? ORDER BY id DESC", new Object[]{10L, -1L, -1L})).thenReturn(modifications);
    MaterialInstance materialInstance = material().createMaterialInstance();
    materialInstance.setId(10);
    when(mockTemplate.findByCriteria(any(DetachedCriteria.class))).thenReturn((List) asList(materialInstance));

    PipelineMaterialRevision pmr = pipelineMaterialRevision();
    List<Modification> actual;
    actual = repo.findModificationsFor(pmr);
    actual = repo.findModificationsFor(pmr);

    assertSame(modifications, actual);

    verify(mockTemplate, times(1)).find("FROM Modification WHERE materialId = ? AND id BETWEEN ? AND ? ORDER BY id DESC", new Object[]{10L, -1L, -1L});
}
 
Example #2
Source File: BaseDao.java    From JgFramework with Apache License 2.0 6 votes vote down vote up
@Override
public List<IBaseModel> findAll(int start, int limit) {
    log.debug("finding all Object instances");
    try {
        HibernateTemplate ht = getHibernateTemplate();
        int PRE_MAX_RESULT = ht.getMaxResults();
        int PRE_LIMIT = ht.getFetchSize();
        ht.setMaxResults(limit);
        ht.setFetchSize(start);
        String queryString = "from " + this.modelName;
        List<IBaseModel> list = ht.find(queryString);
        ht.setMaxResults(PRE_MAX_RESULT);
        ht.setFetchSize(PRE_LIMIT);
        return list;
    } catch (RuntimeException re) {
        log.error("find all failed", re);
        throw re;
    }
}
 
Example #3
Source File: HibernateSearchDependentObjectsReindexer.java    From projectforge-webapp with GNU General Public License v3.0 6 votes vote down vote up
private void reindexDependents(final HibernateTemplate hibernateTemplate, final Session session, final BaseDO< ? > obj,
    final List<Entry> entryList, final Set<String> alreadyReindexed)
{
  if (CollectionUtils.isEmpty(entryList) == true) {
    // Nothing to do.
    return;
  }
  for (final Entry entry : entryList) {
    final RegistryEntry registryEntry = Registry.instance().getEntryByDO(entry.clazz);
    if (registryEntry == null) {
      // Nothing to do
      return;
    }
    final List< ? > result = getDependents(hibernateTemplate, registryEntry, entry, obj);
    if (result != null) {
      for ( Object dependentObject : result) {
        if (dependentObject instanceof Object[]) {
          dependentObject = ((Object[])dependentObject)[0];
        }
        if (dependentObject instanceof BaseDO) {
          reindexDependents(hibernateTemplate, session, (BaseDO< ? >) dependentObject, alreadyReindexed);
        }
      }
    }
  }
}
 
Example #4
Source File: HibernateSearchDependentObjectsReindexer.java    From projectforge-webapp with GNU General Public License v3.0 6 votes vote down vote up
public void reindexDependents(final HibernateTemplate hibernateTemplate, final BaseDO< ? > obj)
{
  new Thread() {
    @Override
    public void run()
    {
      final SessionFactory sessionFactory = hibernateTemplate.getSessionFactory();
      final HibernateTemplate template = new HibernateTemplate(sessionFactory);
      final Session session = template.getSessionFactory().openSession();
      final Set<String> alreadyReindexed = new HashSet<String>();
      final List<Entry> entryList = map.get(obj.getClass());
      reindexDependents(template, session, obj, entryList, alreadyReindexed);
      session.disconnect();
      final int size = alreadyReindexed.size();
      if (size >= 10) {
        log.info("Re-indexing of " + size + " objects done after updating " + obj.getClass().getName() + ":" + obj.getId());
      }
    }
  }.start();
}
 
Example #5
Source File: MaterialRepositoryIntegrationTest.java    From gocd with Apache License 2.0 6 votes vote down vote up
@Test
public void findLatestModifications_shouldCacheResults() {
    SvnMaterial material = MaterialsMother.svnMaterial();
    MaterialInstance materialInstance = material.createMaterialInstance();
    repo.saveOrUpdate(materialInstance);

    Modification mod = ModificationsMother.oneModifiedFile("file3");
    mod.setId(8);

    HibernateTemplate mockTemplate = mock(HibernateTemplate.class);
    repo.setHibernateTemplate(mockTemplate);
    when(mockTemplate.execute(any())).thenReturn(mod);

    repo.findLatestModification(materialInstance);

    Modification modification = repo.findLatestModification(materialInstance);
    assertSame(mod, modification);

    verify(mockTemplate, times(1)).execute(any());

}
 
Example #6
Source File: MaterialRepositoryIntegrationTest.java    From gocd with Apache License 2.0 6 votes vote down vote up
@Test
public void findModificationsSince_shouldCacheResults() {
    SvnMaterial material = MaterialsMother.svnMaterial();
    MaterialRevision zero = saveOneScmModification(material, "user1", "file1");
    MaterialRevision first = saveOneScmModification(material, "user1", "file1");
    MaterialRevision second = saveOneScmModification(material, "user2", "file2");
    MaterialRevision third = saveOneScmModification(material, "user2", "file2");

    repo.findModificationsSince(material, first);

    HibernateTemplate mockTemplate = mock(HibernateTemplate.class);
    repo.setHibernateTemplate(mockTemplate);
    List<Modification> modifications = repo.findModificationsSince(material, first);
    assertThat(modifications.size(), is(2));
    assertEquals(third.getLatestModification(), modifications.get(0));
    assertEquals(second.getLatestModification(), modifications.get(1));
    verifyNoMoreInteractions(mockTemplate);
}
 
Example #7
Source File: HibernateDaoSupportTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void testHibernateDaoSupportWithHibernateTemplate() throws Exception {
	HibernateTemplate template = new HibernateTemplate();
	final List test = new ArrayList();
	HibernateDaoSupport dao = new HibernateDaoSupport() {
		@Override
		protected void initDao() {
			test.add("test");
		}
	};
	dao.setHibernateTemplate(template);
	dao.afterPropertiesSet();
	assertEquals("Correct HibernateTemplate", template, dao.getHibernateTemplate());
	assertEquals("initDao called", test.size(), 1);
}
 
Example #8
Source File: MaterialRepositoryIntegrationTest.java    From gocd with Apache License 2.0 5 votes vote down vote up
@Test
public void findPipelineMaterialRevisions_shouldCacheResults() {
    HibernateTemplate mockTemplate = mock(HibernateTemplate.class);
    repo.setHibernateTemplate(mockTemplate);

    repo.findPipelineMaterialRevisions(2);
    repo.findPipelineMaterialRevisions(2);

    verify(mockTemplate, times(1)).find("FROM PipelineMaterialRevision WHERE pipelineId = ? ORDER BY id", 2L);
}
 
Example #9
Source File: MaterialRepositoryIntegrationTest.java    From gocd with Apache License 2.0 5 votes vote down vote up
@Test
public void findLatestModifications_shouldQueryIfNotEnoughElementsInCache() {
    SvnMaterial material = MaterialsMother.svnMaterial();
    MaterialRevision mod = saveOneScmModification(material, "user2", "file3");
    goCache.remove(repo.latestMaterialModificationsKey(repo.findMaterialInstance(material)));
    HibernateTemplate mockTemplate = mock(HibernateTemplate.class);
    repo.setHibernateTemplate(mockTemplate);
    when(mockTemplate.execute(any(HibernateCallback.class))).thenReturn(mod.getModification(0));
    Modification modification = repo.findLatestModification(repo.findMaterialInstance(material));

    assertThat(modification, is(mod.getLatestModification()));
    verify(mockTemplate).execute(any(HibernateCallback.class));
}
 
Example #10
Source File: MaterialRepositoryIntegrationTest.java    From gocd with Apache License 2.0 5 votes vote down vote up
@Test
public void findOrCreateFrom_shouldCacheMaterialInstanceOnCreate() throws Exception {
    Material svn = MaterialsMother.svnMaterial("url", null, "username", "password", false, null);

    MaterialInstance instance = repo.findOrCreateFrom(svn);
    assertThat(instance, is(notNullValue()));

    HibernateTemplate mockTemplate = mock(HibernateTemplate.class);
    repo.setHibernateTemplate(mockTemplate);

    MaterialInstance cachedInstance = repo.findMaterialInstance(svn);
    assertSame(instance, cachedInstance);

    verifyNoMoreInteractions(mockTemplate);
}
 
Example #11
Source File: AbstractTestBase.java    From projectforge-webapp with GNU General Public License v3.0 5 votes vote down vote up
private static void deleteAllDBObjects(final HibernateTemplate hibernateTemplate, final String entity)
{
  final List< ? > all = hibernateTemplate.find("from " + entity + " o");
  if (all != null && all.size() > 0) {
    hibernateTemplate.deleteAll(all);
    hibernateTemplate.flush();
  }
}
 
Example #12
Source File: AbstractTestBase.java    From projectforge-webapp with GNU General Public License v3.0 5 votes vote down vote up
private static void deleteFrom(final HibernateTemplate hibernateTemplate, final String entity)
{
  try {
    final Query query = hibernateTemplate.getSessionFactory().getCurrentSession().createQuery("delete from " + entity);
    query.executeUpdate();
    hibernateTemplate.flush();
  } catch (final Exception ex) {
    log.info("delete from " + entity + " failed (may-be OK). Trying to use HibernateTemplate.deleteAll(List) instead.");
    deleteAllDBObjects(hibernateTemplate, entity);
  }
}
 
Example #13
Source File: HibernateSearchDependentObjectsReindexer.java    From projectforge-webapp with GNU General Public License v3.0 5 votes vote down vote up
private List< ? > getDependents(final HibernateTemplate hibernateTemplate, final RegistryEntry registryEntry, final Entry entry,
  final BaseDO< ? > obj)
  {
final String queryString;
if (entry.setOrCollection == true) {
  queryString = "from " + registryEntry.getDOClass().getName() + " o join o." + entry.fieldName + " r where r.id=?";
} else {
  queryString = "from " + registryEntry.getDOClass().getName() + " o where o." + entry.fieldName + ".id=?";
}
if (log.isDebugEnabled() == true) {
  log.debug(queryString + ", id=" + obj.getId());
}
final List< ? > result = hibernateTemplate.find(queryString, obj.getId());
return result;
  }
 
Example #14
Source File: HibernateSearchDependentObjectsReindexer.java    From projectforge-webapp with GNU General Public License v3.0 5 votes vote down vote up
private void reindexDependents(final HibernateTemplate hibernateTemplate, final Session session, final BaseDO< ? > obj,
    final Set<String> alreadyReindexed)
{
  if (alreadyReindexed.contains(getReindexId(obj)) == true) {
    if (log.isDebugEnabled() == true) {
      log.debug("Object already re-indexed (skipping): " + getReindexId(obj));
    }
    return;
  }
  session.flush(); // Needed to flush the object changes!
  final FullTextSession fullTextSession = Search.getFullTextSession(session);
  fullTextSession.setFlushMode(FlushMode.AUTO);
  fullTextSession.setCacheMode(CacheMode.IGNORE);
  try {
    BaseDO< ? > dbObj = (BaseDO< ? >) session.get(obj.getClass(), obj.getId());
    if (dbObj == null) {
      dbObj = (BaseDO< ? >) session.load(obj.getClass(), obj.getId());
    }
    fullTextSession.index(dbObj);
    alreadyReindexed.add(getReindexId(dbObj));
    if (log.isDebugEnabled() == true) {
      log.debug("Object added to index: " + getReindexId(dbObj));
    }
  } catch (final Exception ex) {
    // Don't fail if any exception while re-indexing occurs.
    log.info("Fail to re-index " + obj.getClass() + ": " + ex.getMessage());
  }
  // session.flush(); // clear every batchSize since the queue is processed
  final List<Entry> entryList = map.get(obj.getClass());
  reindexDependents(hibernateTemplate, session, obj, entryList, alreadyReindexed);
}
 
Example #15
Source File: MaterialRepositoryIntegrationTest.java    From gocd with Apache License 2.0 5 votes vote down vote up
@Test
public void findMaterialInstance_shouldCacheMaterialInstance() throws Exception {
    Material svn1 = MaterialsMother.svnMaterial("url", null, "username", "password", false, null);
    repo.saveOrUpdate(svn1.createMaterialInstance());

    MaterialInstance instance = repo.findMaterialInstance(svn1);

    HibernateTemplate mockTemplate = mock(HibernateTemplate.class);
    repo.setHibernateTemplate(mockTemplate);

    MaterialInstance cachedInstance = repo.findMaterialInstance(svn1);
    assertSame(instance, cachedInstance);

    verifyNoMoreInteractions(mockTemplate);
}
 
Example #16
Source File: PipelineRepositoryTest.java    From gocd with Apache License 2.0 5 votes vote down vote up
private void stubPipelineInstancesInDb(Object[]... rows) {
    pipelineRepository.setHibernateTemplate(new HibernateTemplate() {
        @Override
        public <T> T execute(HibernateCallback<T> action) throws DataAccessException {
            try {
                return action.doInHibernate(session);
            } catch (SQLException e) {
                throw new RuntimeException();
            }
        }
    });
    when(session.createSQLQuery(nullable(String.class))).thenReturn(sqlQuery);
    when(sqlQuery.list()).thenReturn(Arrays.asList(rows));
}
 
Example #17
Source File: PipelineRepositoryTest.java    From gocd with Apache License 2.0 5 votes vote down vote up
@Before
public void setup() {
    session = mock(Session.class);
    sqlQuery = mock(SQLQuery.class);
    sessionFactory = mock(SessionFactory.class);
    hibernateTemplate = mock(HibernateTemplate.class);
    goCache = mock(GoCache.class);
    databaseStrategy = mock(Database.class);
    when(databaseStrategy.getQueryExtensions()).thenReturn(mock(QueryExtensions.class));
    pipelineRepository = new PipelineRepository(sessionFactory, goCache, databaseStrategy);
    pipelineRepository.setHibernateTemplate(hibernateTemplate);
    transactionTemplate = mock(TransactionTemplate.class);
    transactionSynchronizationManager = mock(TransactionSynchronizationManager.class);
}
 
Example #18
Source File: UserGroupCache.java    From projectforge-webapp with GNU General Public License v3.0 4 votes vote down vote up
public void setHibernateTemplate(final HibernateTemplate hibernateTemplate)
{
  this.hibernateTemplate = hibernateTemplate;
}
 
Example #19
Source File: KontoCache.java    From projectforge-webapp with GNU General Public License v3.0 4 votes vote down vote up
public void setHibernateTemplate(final HibernateTemplate hibernateTemplate)
{
  this.hibernateTemplate = hibernateTemplate;
}
 
Example #20
Source File: RechnungCache.java    From projectforge-webapp with GNU General Public License v3.0 4 votes vote down vote up
public void setHibernateTemplate(final HibernateTemplate hibernateTemplate)
{
  this.hibernateTemplate = hibernateTemplate;
}
 
Example #21
Source File: KostCache.java    From projectforge-webapp with GNU General Public License v3.0 4 votes vote down vote up
public void setHibernateTemplate(HibernateTemplate hibernateTemplate)
{
  this.hibernateTemplate = hibernateTemplate;
}
 
Example #22
Source File: XmlDump.java    From projectforge-webapp with GNU General Public License v3.0 4 votes vote down vote up
public HibernateTemplate getHibernate()
{
  Validate.notNull(hibernate);
  return hibernate;
}
 
Example #23
Source File: XmlDump.java    From projectforge-webapp with GNU General Public License v3.0 4 votes vote down vote up
public void setHibernate(final HibernateTemplate hibernate)
{
  this.hibernate = hibernate;
}
 
Example #24
Source File: BaseDaoImpl.java    From xmu-2016-MrCode with GNU General Public License v2.0 4 votes vote down vote up
public HibernateTemplate getHibernateTemplate() {
	return hibernateTemplate;
}
 
Example #25
Source File: Registry.java    From projectforge-webapp with GNU General Public License v3.0 4 votes vote down vote up
/**
 * @return the hibernateTemplate
 */
public HibernateTemplate getHibernateTemplate()
{
  return hibernateTemplate;
}
 
Example #26
Source File: Registry.java    From projectforge-webapp with GNU General Public License v3.0 4 votes vote down vote up
void setHibernateTemplate(final HibernateTemplate hibernateTemplate)
{
  this.hibernateTemplate = hibernateTemplate;
}
 
Example #27
Source File: DaoRegistry.java    From projectforge-webapp with GNU General Public License v3.0 4 votes vote down vote up
public void setHibernateTemplate(final HibernateTemplate hibernateTemplate)
{
  this.hibernateTemplate = hibernateTemplate;
}
 
Example #28
Source File: HibernateSearchReindexer.java    From projectforge-webapp with GNU General Public License v3.0 4 votes vote down vote up
/**
 * @param hibernate the hibernate to set
 * @return this for chaining.
 */
public void setHibernate(final HibernateTemplate hibernate)
{
  this.hibernate = hibernate;
}
 
Example #29
Source File: AbstractTestBase.java    From projectforge-webapp with GNU General Public License v3.0 4 votes vote down vote up
public HibernateTemplate getHibernateTemplate()
{
  return this.hibernate;
}
 
Example #30
Source File: HibernateDao.java    From jdal with Apache License 2.0 4 votes vote down vote up
public HibernateTemplate getHibernateTemplate() {
	return hibernateTemplate;
}