Package com.softserve.academy.food.dao.impl

Source Code of com.softserve.academy.food.dao.impl.UserDao

package com.softserve.academy.food.dao.impl;

import com.softserve.academy.food.dao.IUserDao;
import com.softserve.academy.food.entity.User;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("userDao")
public class UserDao extends Dao implements IUserDao {
    public User get(int id) {
        return (User) sessionFactory.getCurrentSession().get(User.class, id);
    }

    @SuppressWarnings("unchecked")
    public List<User> getAll() {
        return (List<User>) sessionFactory.getCurrentSession().createQuery("from User ").list();
    }

    @SuppressWarnings("unchecked")
    public User get(String name) {
        List<User> list = sessionFactory.getCurrentSession().createQuery("from User where USER_NAME = '" + name + "'").list();

        if (list.isEmpty()) {
            return null;
        }
        return list.get(0);
    }

    public User add(User user) {
        user.setId((Integer) sessionFactory.getCurrentSession().save(user));

        return user;
    }

    public void delete(int id) {
        User object = (User) sessionFactory.getCurrentSession().load(
                User.class, id);
        if (null != object) {
            sessionFactory.getCurrentSession().delete(object);
        }
    }

}
TOP

Related Classes of com.softserve.academy.food.dao.impl.UserDao

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.