Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring JPA Hibernate处理大型数据库_Java_Spring_Hibernate_Jpa_Spring Ws - Fatal编程技术网

Java Spring JPA Hibernate处理大型数据库

Java Spring JPA Hibernate处理大型数据库,java,spring,hibernate,jpa,spring-ws,Java,Spring,Hibernate,Jpa,Spring Ws,我对JPA、Hibernate和Spring都是新手。目前,我正在创建一个SpringWeb服务,它与一个包含大量表的数据库一起工作。为了访问这些表,我创建了单独的类注释@Entity。然后我创建了一个泛型DAO类,因为我的所有实体都需要类似的操作 @Transactional public class GenericJpaDao<T, ID extends Serializable> { private Class<T> persistentClass; priva

我对JPA、Hibernate和Spring都是新手。目前,我正在创建一个SpringWeb服务,它与一个包含大量表的数据库一起工作。为了访问这些表,我创建了单独的类注释
@Entity
。然后我创建了一个泛型DAO类,因为我的所有实体都需要类似的操作

@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();
}

}
因此,根据我从可用示例中了解到的情况,我需要为所有实体类创建单独的DAO类,并在
springWSservlet.xml
中实例化它们

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


我认为从长远来看,这将是一个问题,因为我需要为数据库中的每个表使用两个独立的类,将它们安装在xml中,并在我的服务类中跟踪它们。是否有任何方法可以克服这一问题,或者有任何最佳实践

您不需要为每个类创建特定的DAO

但您必须删除构造函数并更改方法签名,以包含所需的persistentClass(或调用getClass的实例)

基本上,您需要删除persistentClass属性并更改方法,以便从T或class类型的泛型参数中动态使用该类


这样,您就有了一个spring管理的DAO,它能够处理您的所有实体

小贴士:扔掉你的解决方案并使用它。这样,您只需声明一些接口(无需实现!)添加一行xml配置即可获得所需的所有dao。
   <bean id="testDao" class="com.sample.dao.TestDao" />
   <bean id="service"
    class="com.sample.service.DefaultService">
    <property name="testDao" ref="testDao" />
</bean>