使用GenericsDAO + SpringMVC + Hibernate Error

时间:2017-02-06 23:42:27

标签: java hibernate spring-mvc generics

我使用泛型来处理SpringMVC + Hibernate然而,抛出一个错误,我试图解决它,但我不能,任何人都知道问题的原因。 这是我的代码:

public interface GenericDAO <E, K> {

public void save(E entity);
public List<E> findAll();
public void update(E entity);
public void delete(E entity);
public E find(K key);
}

@Transactional
@Repository
public abstract class GenericDAOImpl<E, K extends Serializable> implements GenericDAO<E, K>{

@Autowired
private SessionFactory sessionFactory;

protected Class<? extends E> daoType;

public GenericDAOImpl() {
    Type type = getClass().getGenericSuperclass();
    ParameterizedType parameterizedType = (ParameterizedType) type;

}

protected Session currentSession(){
    return sessionFactory.getCurrentSession();
}

@Override
public void save(E entity) {
    currentSession().save(entity);
}

@SuppressWarnings("deprecation")
@Override
public List<E> findAll() {
    return currentSession().createCriteria(daoType).list();
}

@Override
public void update(E entity) {
    currentSession().saveOrUpdate(entity);
}

@Override
public void delete(E entity) {
    currentSession().delete(entity);
}

@Override
public E find(K key) {
    return (E)currentSession().get(daoType, key);
}
}


public interface GenericService <E, K>{

public void save(E entity);
public List<E> findAll();
public void update(E entity);
public void delete(E entity);
public E find(K key);
}

@Service
public abstract class GenericServiceImpl<E, K> implements GenericService<E, K> {

private GenericDAO<E, K> genericDAO;

public GenericServiceImpl() {
}

public GenericServiceImpl(GenericDAO<E, K> genericDAO){
    this.genericDAO = genericDAO;
}

@Override
@Transactional(propagation = Propagation.REQUIRED)
public void save(E entity) {
    genericDAO.save(entity);
}

@Override
@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
public List<E> findAll() {
    return genericDAO.findAll();
}

@Override
@Transactional
public void update(E entity) {
    genericDAO.update(entity);
}

@Override
@Transactional
public void delete(E entity) {
    genericDAO.delete(entity);
}

@Override
@Transactional(propagation = Propagation.REQUIRED, readOnly = true)
public E find(K key){
    return genericDAO.find(key);
}
}

public interface MoviesDAO extends GenericDAO<Movies, Integer> {
}

@Transactional
@Repository
public class MoviesDAOImpl extends GenericDAOImpl<Movies, Integer> implements MoviesDAO{
}

public interface MoviesService extends GenericService<Movies, Integer>{
}

@Service
public class MoviesServiceImpl extends GenericServiceImpl<Movies, Integer> implements MoviesService{

 }

@Controller
public class MoviesController {

@Autowired(required = true)
MoviesService moviesService;

@RequestMapping("/movies")
public String returnMovies(Model model){
    model.addAttribute("movies", new Movies());
    model.addAttribute("listMovies", moviesService.find(1));
    return "movies";
}
}

出现的错误是:

java.lang.NullPointerException
     com.project.movies.service.impl.GenericServiceImpl.find(GenericServiceImpl.java:51)
com.project.movies.controller.MoviesController.returnMovies(MoviesController.java:21)

context.xml中

<tx:annotation-driven transaction-manager="transactionManager"/>
<mvc:annotation-driven/>    
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/DataBaseBD" />
    <property name="username" value="user"/>
    <property name="password" value="password"/>
</bean> 

<bean id="hibernate5AnnotatedSessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
    <property name="dataSource" ref="dataSource"/>
    <property name="annotatedClasses">
        <list>
            <value>com.project.movies.model.Movies</value>
        </list>
    </property>
    <property name="hibernateProperties">
        <props>
            <prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
            <prop key="hibernate.show_sql">true</prop>
        </props>
    </property> 
</bean>

<bean id="transactionManager" class="org.springframework.orm.hibernate5.HibernateTransactionManager">
    <property name="sessionFactory" ref="hibernate5AnnotatedSessionFactory" />
</bean> 

1 个答案:

答案 0 :(得分:0)

您的服务为空,使用您的全部限定名称MoviesServiceImpl为您的服务添加bean定义。

....
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/DataBaseBD" />
    <property name="username" value="user"/>
    <property name="password" value="password"/>
</bean> 

<bean id="moviesService" class="your.class.package.MoviesServiceImpl" parent="GenericService" />
....