2013-03-03 111 views
1

我有关于DAO和服务层模式的问题,我正在使用Spring 3和Hibernate 4作为小型应用程序。DAO和服务层的设计模式

关于我的应用程序

我有地方员工和部门的数据都显示在一个JSF形成一个小的应用程序小描述。

部门数据以表格形式使用作为主数据的数据表以及相关的 以表格形式显示部门数据使用数据表以及明细(主 - 明细)情景以表格形式显示。 一旦点击主数据表按钮,会出现一个弹出窗口,可以在其中输入有关部细节和数据库表中的数据仍然存在(相同的情况下进行删除和更新)

我的设计是类似如下

DAO层

public interface GenericDAO<T> { 
    public void create(T entity); 
    public void update(T entity); 
    public void delete(T entity); 
} 

public interface DepartmentDAO extends GenericDAO<Department> 
--methods for getting Department list and others 
    public void findDepartment(DepartmentData data); 
    ----- 

public interface EmployeeDAO extends GenericDAO<Employee> 
--methods for getting Employeelist and others 
--- 

服务层

public interface DepartmentService { 
public void findDepartment(DepartmentData data); 
-- other methods 
} 

@Transactional 
@Named 
public class DepartmentServiceImpl implements DepartmentService { 

@Inject 
DepartmentDAO departmentDAO; 

-- implementation of methods 
} 

public interface EmployeeService { 
public void findEmployees(EmployeeData data); 
-- other methods 
} 

@Transactional 
@Named 
public class EmployeeServiceImpl implements EmployeeService { 

@Inject 
EmployeeDAO employeeDAO; 

-- implementation of methods 
} 

我的问题是我应该为部门和员工使用单一的服务接口或类,因为我为我的JSF表单使用了一个ManagedBean,或者我应该为部门和员工使用单独的界面和类?将所有DAO方法与@Transactional合并到一个服务实现类中,在数据库事务处理时是否会遇到任何性能问题?建议使用类似GenericDAO的通用服务接口?

任何帮助是非常可观的?

更新1

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-3.0.xsd" 
> 
    <context:component-scan base-package="net.test" /> 
    <!-- Data Source Declaration -->  
    <bean id="DataSource" class="org.springframework.jndi.JndiObjectFactoryBean"> 
    <property name="jndiName" value="jdbc/myDS"/>  
</bean> 
    <bean 
     class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor" /> 
    <bean class="org.springframework.orm.hibernate4.HibernateExceptionTranslator" /> 
    <!-- JPA Entity Manager Factory --> 
    <bean id="entityManagerFactory" 
     class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
     <property name="dataSource" ref="DataSource" /> 
     <property name="packagesToScan" value="net.test.entity" /> 
     <property name="jpaVendorAdapter"> 
      <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> 
       <property name="showSql" value="true" /> 
       <property name="generateDdl" value="false" /> 
       <property name="databasePlatform" value="${jdbc.dialectClass}" /> 
      </bean> 
     </property> 
    </bean> 
    <bean id="defaultLobHandler" class="org.springframework.jdbc.support.lob.DefaultLobHandler" /> 
    <!-- Session Factory Declaration --> 
    <bean id="SessionFactory" 
     class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"> 
     <property name="dataSource" ref="DataSource" /> 
     <property name="annotatedClasses"> 
      <list> 
       <value>net.test.entity.Employee</value> 
       <value>net.test.entity.Department</value> 

      </list> 
     </property> 
     <property name="hibernateProperties"> 
      <props> 
       <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop> 
       <prop key="hibernate.show_sql">true</prop> 
       <prop key="hibernate.query.factory_class">org.hibernate.hql.internal.classic.ClassicQueryTranslatorFactory 
       </prop> 
      </props> 
     </property> 
    </bean> 
    <tx:annotation-driven transaction-manager="txManager" /> 
    <tx:annotation-driven transaction-manager="transactionManager" /> 
    <bean id="txManager" 
     class="org.springframework.orm.hibernate4.HibernateTransactionManager"> 
     <property name="sessionFactory" ref="SessionFactory" /> 
    </bean> 

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
     <property name="entityManagerFactory" ref="entityManagerFactory" /> 
    </bean> 
    <!-- <tx:annotation-driven transaction-manager="txManager"/> --> 
    <context:annotation-config /> 
    <bean id="hibernateStatisticsMBean" class="org.hibernate.jmx.StatisticsService"> 
     <property name="statisticsEnabled" value="true" /> 
     <property name="sessionFactory" value="#{entityManagerFactory.sessionFactory}" /> 
    </bean> 
    <bean name="ehCacheManagerMBean" 
     class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" /> 
    <bean id="mbeanServer" class="org.springframework.jmx.support.MBeanServerFactoryBean"> 
     <property name="locateExistingServerIfPossible" value="true" /> 
    </bean> 
    <bean id="jmxExporter" class="org.springframework.jmx.export.MBeanExporter" 
     lazy-init="false"> 
     <property name="server" ref="mbeanServer" /> 
     <property name="registrationBehaviorName" value="REGISTRATION_REPLACE_EXISTING" /> 
     <property name="beans"> 
      <map> 
       <entry key="SpringBeans:name=hibernateStatisticsMBean" 
        value-ref="hibernateStatisticsMBean" /> 
       <entry key="SpringBeans:name=ehCacheManagerMBean" value-ref="ehCacheManagerMBean" /> 
      </map> 
     </property> 
    </bean> 
</beans> 

回答

1

要真正回答这个问题,我们需要了解如何为@事务语义已经配置的详细信息。例如,每个DAO最终都会在单独的事务中运行,或者会在Java级别上懒散地连接在一起,并在数据库级别上懒洋洋地连接等。

假设“common”配置只有一个数据库和连接当服务加入一个共享的请求级别事务时被懒惰地重用;不管你是否使用一两个服务,我都不会有任何大的担忧。

从性能的角度来看,它是数据库的往返次数,数据库请求的工作负载以及在数据库级别保存事务多长时间,这将产生重大影响。有一个或两个Java对象加入当时可能或可能不会被数据库连接支持的Java级别事务(更不用说事务),这不会使性能数字变得更差。您可能会发现序列化多个独立请求的延迟,在这种情况下,可能需要组合,批量或缓存一些查询。哪一个最好从单个DAO完成;但是只有在测量性能问题后才能做到这一点。

团队约定和沟通你的设计意图/域模型将在这里变得更加重要。因此,我认为你是最适合回答自己的问题的人,是否应该选择一个或两个服务。

+0

+1用于约定并明确地与名称进行通信。与复杂的hibernate查询相比,您会更早地遇到性能问题,而不是您所描述的事务拆分。 – vertti 2013-03-03 13:10:46

+0

Chris,你的意思是关于@Tranasctional语义的细节?你的意思是DAO植入类? – user75ponic 2013-03-03 13:47:35

+0

@Polppan。事务可以声明为在web请求开始时打开或者懒惰地创建,在请求之间保持打开状态,与持有hibernate会话打开的底层数据库分离,但在不需要时关闭数据库连接,或者将每个请求置于其自己的事务中等等。还有关于如何合并跨不同会话对象的事务的选项,每次都要启动一个新事务,是可选事务,必须事务已经在运行等等。这是在提及分布式交易之前。这是一个很大的话题。 – 2013-03-03 13:54:47