gpt4 book ai didi

java - 使用GenericsDAO + SpringMVC + Hibernate报错

转载 作者:太空宇宙 更新时间:2023-11-04 11:48:25 24 4
gpt4 key购买 nike

我正在使用泛型与 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)

上下文.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>

最佳答案

您的服务为空,请使用 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" />
....

关于java - 使用GenericsDAO + SpringMVC + Hibernate报错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42079297/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com