Вопрос

I am new to JPA, Hibernate as well as Spring. Currently I am creating a spring web service which work with a database with a large number of tables. To access those tables I have created separate class annotating @Entity. Then I created a generic DAO class as all my entities need similar operations.

@Transactional
public class GenericJpaDao<T, ID extends Serializable> {

private Class<T> persistentClass;

private EntityManager entityManager;

public GenericJpaDao(Class<T> persistentClass) {
    this.persistentClass = persistentClass;
}

protected EntityManager getEntityManager() {
    return entityManager;
}

@PersistenceContext
public void setEntityManager(EntityManager entityManager) {
    this.entityManager = entityManager;
}

public Class<T> getPersistentClass() {
    return persistentClass;
}

@Transactional(readOnly = true)
public T findById(ID id) {
    T entity = (T) getEntityManager().find(getPersistentClass(), id);
    return entity;
}

@SuppressWarnings("unchecked")
@Transactional(readOnly = true)
public List<T> findAll() {
    return getEntityManager().createQuery("select x from " + getPersistentClass().getSimpleName() + " x").getResultList();
}

public T save(T entity) {
    getEntityManager().persist(entity);
    return entity;
}

public T update(T entity) {
    T mergedEntity = getEntityManager().merge(entity);
    return mergedEntity;
}

public void delete(T entity) {
    entity = getEntityManager().merge(entity);
    getEntityManager().remove(entity);
}

public void flush() {
    getEntityManager().flush();
}

}

Now I tried to instantiate this GenericJpaDao in the code with relevant Entitiy Class as the persistentClass. But then I could not find a way to set the entitiyManager as I am configuring it via datasource-config.xml as

 <bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
    <property name="persistenceXmlLocation" value="classpath*:META-INF/persistence.xml"></property>
    <property name="persistenceUnitName" value="hibernatePersistenceUnit" />
    <property name="dataSource" ref="dataSource" />
    <property name="jpaVendorAdapter">
        <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
            <property name="showSql" value="true" />
        </bean>
    </property>
</bean>

<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
    <property name="dataSource" ref="dataSource" />
    <property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>

So according to what I understood from the samples available I need to create separate DAO classes for all my entity classes and instantiate them in spring-ws-servlet.xml.

   <bean id="testDao" class="com.sample.dao.TestDao" />
   <bean id="service"
    class="com.sample.service.DefaultService">
    <property name="testDao" ref="testDao" />
</bean>

I think this will be a problem in the long run as I need to have two separated classes for each table in the database, instaintiating them in the xml, keep track of all of them at my service class. Is there any method to overcome this or any best practice available?

Это было полезно?

Решение

You wont need to create a specific DAO for each of your classes.

But you will have to remove the constructor and change your method signatures to include the needed persistentClass (or an instance where you call getClass on).

Basically you need to remove the persistentClass property and change the methods to use the class dynamically from the generic parameter of type T or Class.

That way you have ONE spring managed DAO which is able to handle all of your entities.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top