所以我有这样的课
基地道
public interface BaseDao<T> {
T save(T entity);
}
用户道
public interface UserDao extends BaseDao<User> {
User getUserByUserName(String name);
}
GenericDao 公共抽象类 GenericDao 实现 BaseDao {
@Autowired
protected SessionFactory sessionFactory;
private Class<?> getEntityClass() {
return ((Class) ((ParameterizedType) getClass().getGenericSuperclass())
.getActualTypeArguments()[0]);
}
public T save(T entity) {
Integer id = (Integer) sessionFactory.getCurrentSession().save(entity);
return (T) sessionFactory.getCurrentSession().get(getEntityClass(), id);
}
}
UserDaoImpl
@Repository
public class PgUserDaoImpl extends GenericDao<User> implements UserDao {
public User getUserByUserName(String name) {
Criteria criteria = sessionFactory.getCurrentSession().createCriteria(
User.class);
criteria.add(Restrictions.eq("name", name));
return (User) criteria.uniqueResult();
}
}
通用服务
public class GenericService<T extends GenericDao<E>, E> {
protected T dao;
public GenericService(T dao) {
setDao(dao);
}
@Transactional
public E save(E entity) {
return dao.save(entity);
}
}
用户服务实现
@Service("userServiceImpl")
public class UserServiceImpl extends GenericService<PgUserDaoImpl, User>
implements UserService {
@Autowired
public UserServiceImpl(PgUserDaoImpl dao) {
super(dao);
}
@Autowired
private UserAssebmler assebmler;
@Transactional
@Override
public UserDetails loadUserByUsername(String name)
throws UsernameNotFoundException {
.....
}
}
所以我写了测试控制器来保存用户,但我总是得到 Exception org.hibernate.HibernateException: No Hibernate Session bound to thread,并且配置不允许在这里创建非事务性的
控制器@
Controller
@RequestMapping(value = "/test")
public class TestController {
@Autowired
private UserServiceImpl userService;
@RequestMapping(value = "save", method = RequestMethod.GET)
public String test() {
User user = new User();
user.setName("admin");
user.setPassword("21232f297a57a5a743894a0e4a801fc3");
user.setRole(UserRole.ADMIN);
userService.save(user);
return "home";
}
}
有人知道热修复吗?谢谢 :)