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 com.mycompany.springrest.dao; import com.mycompany.springrest.model.Role; import java.util.List; import org.hibernate.Session; import org.hibernate.SessionFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; import org.springframework.transaction.annotation.Transactional; /** * * @author ?? */ @Repository public class RoleDAOImpl implements RoleDAO { private static final Logger logger = LoggerFactory.getLogger(RoleDAOImpl.class); @Autowired private SessionFactory sessionFactory; @Override public void addRole(Role role) { Session session = this.sessionFactory.getCurrentSession(); session.persist(role); logger.info("Role saved successfully, Role Details=" + role); } @Override public void updateRole(Role role) { Session session = this.sessionFactory.getCurrentSession(); session.update(role); logger.info("Role updated successfully, Role Details=" + role); } @SuppressWarnings("unchecked") @Override public List<Role> listRoles() { Session session = this.sessionFactory.getCurrentSession(); List<Role> rolesList = session.createQuery("from Role").list(); for (Role role : rolesList) { logger.info("Role List::" + role); } return rolesList; } @Override public Role getRoleById(int id) { Session session = this.sessionFactory.getCurrentSession(); Role role = (Role) session.load(Role.class, new Integer(id)); logger.info("Role loaded successfully, Role details=" + role); return role; } @Override public void removeRole(int id) { Session session = this.sessionFactory.getCurrentSession(); Role role = (Role) session.load(Role.class, new Integer(id)); if (null != role) session.delete(role); logger.info("Role deleted successfully, Role details=" + role); } @SuppressWarnings("unchecked") @Override @Transactional public List<Role> listRoles(String username) { Session session = this.sessionFactory.getCurrentSession(); List<Role> rolesList = session.createQuery("from Role where username=?").setParameter(0, username).list(); for (Role role : rolesList) { logger.info("Role List::" + role); } return rolesList; } }