-
-
Save vicentealencar/dd4a369d60a05460d0c0 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package Dao; | |
import java.util.List; | |
import modelo.User; | |
import org.hibernate.Query; | |
import org.hibernate.Session; | |
import org.hibernate.transform.AliasToBeanResultTransformer; | |
import persistencia.HibernateUtil; | |
public class UserDAO { | |
private Session session; | |
private static final String LAWYER_TYPE = "Advogado"; | |
private static final String REPRESENTATIVE_TYPE = "Preposto"; | |
/* | |
* | |
*/ | |
public UserDAO() | |
{ | |
} | |
/** | |
* | |
* @param id | |
* @return | |
*/ | |
public User getLawyer(int id) { | |
return this.getUser(id, LAWYER_TYPE); | |
} | |
/** | |
* | |
* @param id | |
* @return | |
*/ | |
public User getRepresentative(int id) { | |
return this.getUser(id, REPRESENTATIVE_TYPE); | |
} | |
/** | |
* | |
* @return All laywers from the webApp DB. | |
*/ | |
public List<User> getAllLawyers() | |
{ | |
return this.getAllUsers(LAWYER_TYPE); | |
} | |
/** | |
* | |
* @return Allrepresentatives from the webApp DB. | |
*/ | |
public List<User> getAllRepresentatives() | |
{ | |
return this.getAllUsers(REPRESENTATIVE_TYPE); | |
} | |
private List<User> getAllUsers(String userType) | |
{ | |
List<User> result; | |
session = HibernateUtil.getWilsonsSessionFactory().getCurrentSession(); | |
session.beginTransaction(); | |
Query query = session.createSQLQuery(String.format("select users.id,users.name,users.email,roles.name AS role from (users INNER JOIN roles on users.role_id = roles.id) where roles.name='%s'", userType)); | |
query.setResultTransformer(new AliasToBeanResultTransformer(User.class)); | |
result = (List<User>)query.list(); | |
session.getTransaction().commit(); | |
return result; | |
} | |
private User getUser(int id, String userType) | |
{ | |
User result; | |
session = HibernateUtil.getWilsonsSessionFactory().getCurrentSession(); | |
session.beginTransaction(); | |
Query query = session.createSQLQuery(String.format("select users.id,users.name,users.email,roles.name AS role from (users INNER JOIN roles on users.role_id = roles.id) where users.id=? AND roles.name='%s'", userType)); | |
query.setParameter(0, id); | |
query.setResultTransformer(new AliasToBeanResultTransformer(User.class)); | |
result = (User)query.uniqueResult(); | |
session.getTransaction().commit(); | |
return result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment