Java tutorial
/* Copyright 2013 Mael Le Guvel This work is free. You can redistribute it and/or modify it under the terms of the Do What The Fuck You Want To Public License, Version 2, as published by Sam Hocevar. See the COPYING file for more details. */ package fr.mael.microrss.dao.impl; import java.util.List; import org.hibernate.Query; import org.springframework.stereotype.Repository; import fr.mael.microrss.dao.CategoryDao; import fr.mael.microrss.domain.Category; import fr.mael.microrss.domain.User; @Repository public class CategoryDaoImpl extends GenericDaoImpl<Category> implements CategoryDao { @Override public Category collapse(Category category, boolean collapsed) { StringBuffer query = new StringBuffer("update Category cat set collapsed = :value "); query.append("where cat = :category "); Query q = getSessionFactory().getCurrentSession().createQuery(query.toString()); q.setEntity("category", category); q.setBoolean("value", collapsed); q.executeUpdate(); return category; } @Override public List<Category> forUser(User user) { StringBuffer query = new StringBuffer("select distinct cat from Category cat "); query.append("left join fetch cat.userFeeds uf "); query.append("left join fetch uf.unreadUserFeed "); query.append("left join fetch uf.feed "); query.append("where cat.user = :user "); Query q = getSessionFactory().getCurrentSession().createQuery(query.toString()); q.setEntity("user", user); return (List<Category>) q.list(); } }