Я знаю, что эту проблему часто задают, но я застрял. Я основываю свой проект на основе этого руководства: http://www.cavalr.com/blog/Spring_3_and ... _4_Example
Это мой root-context.xml
classpath:db.properties
org.hibernate.dialect.MySQL5InnoDBDialect
true
Это мой servlet-context.xml
Это мой AbstractDaoImpl
public abstract class AbstractDaoImpl implements AbstractDao {
private Class entityClass;
protected AbstractDaoImpl(Class entityClass) {
this.entityClass = entityClass;
}
@Autowired
private SessionFactory sessionFactory;
public Session getCurrentSession() {
return sessionFactory.getCurrentSession();
}
@SuppressWarnings("unchecked")
@Override
public E findById(I id) {
return (E) getCurrentSession().get(entityClass, id);
}
@Override
public void saveOrUpdate(E e) {
getCurrentSession().saveOrUpdate(e);
}
@Override
public void delete(E e) {
getCurrentSession().delete(e);
}
@Override
public List findByCriteria(Criterion criterion) {
Criteria criteria = getCurrentSession().createCriteria(entityClass);
criteria.add(criterion);
return criteria.list();
}
}
Это мой класс RecipeDaoImpl
@Repository
public class RecipeDaoImpl extends AbstractDaoImpl implements RecipeDao {
protected RecipeDaoImpl() {
super(Recipe.class);
}
@Override
public boolean saveRecipe(Recipe r) {
return saveRecipe(r);
}
@Override
public Recipe getRecipe(String recipeId) {
return findById(recipeId);
}
@SuppressWarnings("unchecked")
@Override
public List findRecipes(String keyword) {
return findByCriteria( Restrictions.and( Restrictions.like("name", keyword, MatchMode.ANYWHERE),
Restrictions.like("keywords", keyword, MatchMode.ANYWHERE) ) );
}
}
Это мой класс RecipeServiceImpl
@Service("recipeService")
@Transactional(readOnly = true)
public class RecipeServiceImpl implements RecipeService {
@Autowired
private RecipeDao recipeDao;
@Override
@Transactional(readOnly = false)
public boolean saveRecipe(Recipe r) {
return recipeDao.saveRecipe(r);
}
@Override
public Recipe getRecipe(String recipeId) {
return recipeDao.getRecipe(recipeId);
}
@Override
public List findRecipes(String keyword) {
return recipeDao.findRecipes(keyword);
}
}
Это мой контроллер рецептов
/**
* Handles requests for the application home page.
*/
@Controller
public class RecipeController {
private static final Logger logger = LoggerFactory.getLogger(RecipeController.class);
@Autowired
private RecipeService recipeService;
/**
* Adds recipes to the DB
*/
@RequestMapping(value = "/add", method = RequestMethod.GET)
public String add(Locale locale, Model model) {
return "add";
}
/**
* Searches for recipes
*/
@RequestMapping(value = "/search", method = RequestMethod.POST)
public String search(@RequestParam(value="keyword", required=true) String keyword, Model model) {
List recipes = recipeService.findRecipes(keyword);
System.out.println( "Results:"+ recipes.size() );
return "results";
}
/**
* Logs in the user
*/
@RequestMapping(value = "/login", method = RequestMethod.POST)
public String login(Locale locale, Model model) {
return "login";
}
}
Я пробовал поместить @Transactional в класс RecipeDaoImpl и класс AbstractDaoImpl, но ни один из них не сработал.
EDIT:
Я исправил это, перехватив исключение и открыв новое:
public Session getCurrentSession() {
Session session = null;
try {
session = sessionFactory.getCurrentSession();
} catch ( HibernateException he ) {
session = sessionFactory.openSession();
}
return session;
}