====== Hibernate ======
**Doc:** http://docs.jboss.org/hibernate/orm/4.2/
* [[http://docs.jboss.org/hibernate/orm/4.2/devguide/en-US/html_single/|DevGuide HTML Single]]
* [[http://docs.jboss.org/hibernate/orm/4.2/javadocs/|JavaDoc]]
* [[http://docs.jboss.org/hibernate/orm/4.2/manual/en-US/html_single/|Manual HTML Single]] ([[https://docs.jboss.org/hibernate/orm/4.2/manual/en-US/html_single/#queryhql|HQL]])
* [[http://docs.jboss.org/hibernate/orm/4.2/quickstart/en-US/html_single/|Quick Start]]
===== DAO =====
package cz.isn.example.dao;
import cz.isn.example.model.User;
import java.util.List;
/**
*
* @author Jiří Blažek
*/
public interface UserDao {
public List findAll();
public User findById(long id);
public void saveUser(User user);
public void updateUser(User user);
publid void deleteUser(User user);
}
package cz.isn.example.dao;
import cz.isn.example.model.User;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
*
* @author Jiří Blažek
*/
@Component("userDao")
public class UserDaoImpl implements UserDao {
@Autowired
private SessionFactory sessionFactory;
@Override
public List findAll() {
List users = sessionFactory.getCurrentSession()
.createQuery("from User u")
.list();
if (!users.isEmpty()) {
return users;
} else {
return null;
}
}
@Override
public User findById(long id) {
List users = sessionFactory.getCurrentSession()
.createQuery("from User u where u.id = :id")
.setLong("id", id)
.list();
if (!user.isEmpty()) {
return users.get(0);
} else {
return null;
}
}
@Override
public void saveUser(User user) {
sessionFactory.getCurrentSession().save(user);
}
@Override
public void updateUser(User user) {
sessionFactory.getCurrentSession().update(user);
}
@Override
public void deleteUser(User user) {
sessionFactory.getCurrentSession().delete(user);
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public SessionFactory getSessionFactory() {
return this.sessionFactory;
}
}