Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package si.arnes.dropbookmarks.db; import com.google.common.base.Optional; import io.dropwizard.hibernate.AbstractDAO; import java.util.List; import org.hibernate.SessionFactory; import si.arnes.dropbookmarks.core.Bookmark; /** * * @author klemen <klemen.pravdic@arnes.si> */ public class BookmarkDAO extends AbstractDAO<Bookmark> { public BookmarkDAO(SessionFactory sessionFactory) { super(sessionFactory); } /** * Find all bookmarks for the user with specified id. * * @param id user id * @return list of bookmarks for the user with specified id. */ public List<Bookmark> findForUser(long id) { return list(namedQuery("si.arnes.dropbookmarks.core.Bookmark.findForUser").setParameter("id", id)); } /** * Finds a bookmark by its id. * * @param id id of a bookmark * @return a bookmark with specified id */ public Optional<Bookmark> findById(long id) { return Optional.fromNullable(get(id)); } /** * Create or Update a bookmark. * * @param bookmark a bookmark to be saved * @return the saved bookmark with all auto-generated fields filled. */ public Bookmark save(Bookmark bookmark) { return persist(bookmark); } /** * Removes a bookmark from the database. * * @param bookmark a bookmark to be removed. */ public void delete(Bookmark bookmark) { namedQuery("si.arnes.dropbookmarks.core.Bookmark.remove").setParameter("id", bookmark.getId()) .executeUpdate(); } /** * Removes all the bookmarks that belong to the particular user * * @param bookmark a bookmark to be removed. */ public void delete(long userId) { namedQuery("si.arnes.dropbookmarks.core.Bookmark.removeAllFromUser").setParameter("id", userId) .executeUpdate(); } }