Why do findXXX () methods in a HibernateTemplate return a non-parameterized list?

Spring 3.0 added many functions for compatibility with Java 5. Currently, many methods are parameterized. For example HibernateTemplate.executeXXX(), HibernateTemplate.getXXX(), HibernateTemplate.mergeXXX()return T, HibernateTemplate.loadAll()returns List<T>.

But the methods findXXX()return plain List, so I have to bring it to something like List<MyEntity>.

Does anyone know what the reason is? Why are the methods not parameterized? Or maybe there is another, parameterized API?

that's what I'm doing.

This is the important part of spring.xml:

  <bean id="hibernateInterceptor" class="org.springframework.orm.hibernate3.HibernateInterceptor" autowire="byName" /><!--sessionFactory will get autowired-->

  <bean id="deviceDaoTarget" class="com.nso.solution.dao.DeviceDAOHibernateImpl" autowire="byName" /><!--sessionFactory will get autowired-->

  <bean id="discoveryDAO" class="org.springframework.aop.framework.ProxyFactoryBean">
    <property name="proxyInterfaces">
      <value>com.nso.solution.dao.DeviceDAO</value>
    </property>
    <property name="interceptorNames">
      <list>
        <value>hibernateInterceptor</value>
        <value>deviceDaoTarget</value>
      </list>
    </property>
  </bean>

DeviceDAO is an interface that contains several methods that allow you to retrieve, save, and delete objects. DeviceDAOHibernateImpl implements this interface, for example.

public List<Device> getAllDevices() {
    return getHibernateTemplate().loadAll(Device.class);
}

@SuppressWarnings ( "unchecked" ).

+3
3

Spring HibernateTemplate, Spring 3. (. Spring : Hibernate)

Spring Hibernate, Spring Framework.

: SessionFactory, @Transactional , Spring :

@Transactional
public class ProductDaoImpl implements ProductDao {

    private SessionFactory sessionFactory;

    public void setSessionFactory(SessionFactory sessionFactory) {
        this.sessionFactory = sessionFactory;
    }

    @SuppressWarnings("unchecked")
    public Collection<Product> loadProductsByCategory(String category) {
        return this.sessionFactory.getCurrentSession()
                .createQuery(
                    "from test.Product product where product.category=?")
                .setParameter(0, category)
                .list();
    }
}

: Spring , , , . . ( , )

+5

sessionfactory

    public List<T> findByCriteria(Class<T> persistentClass, Criterion... criterion) {
    Criteria crit = getSessionFactory().getCurrentSession().createCriteria(persistentClass);
    for (Criterion c : criterion) {
        crit.add(c);
    }
    return crit.list();
}

Criterions, ,

0

, Hibernate generics Hibernate API. , , , 1.4 1.4 1.5. HibernateTemplate , ( ) Hibernate API.

0
source

Source: https://habr.com/ru/post/1775413/


All Articles