2017-02-06 48 views
1

我正在使用泛型與SpringMVC + Hibernate一起工作,但是,拋出一個錯誤,我試圖解決它,但我不能,任何人都知道問題的原因。 這裏是我的代碼:使用GenericsDAO + SpringMVC +休眠錯誤

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> 
+0

你可以在你設置'SessionFactory' bean的地方顯示你的配置嗎? – Naros

+0

我貼我的背景下... – Charlie

回答

0

您服務爲空,添加bean定義與您服務您的MoviesServiceImpl的完整限定名稱。

.... 
<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" /> 
.... 
+0

我將此代碼添加到MoviesServiceImpl,現在我有另一個錯誤: '@Autowired \t公共MoviesServiceImpl(MoviesDAO moviesDAO){ \t \t超(moviesDAO); \t} – Charlie

+0

錯誤: org.hibernate.HibernateException:無法獲取當前線程的事務同步會話 – Charlie

+0

您可以向您展示電影類嗎? – jonhid