Guest User

Untitled

a guest
May 5th, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.98 KB | None | 0 0
  1. public interface User
  2. {
  3. int getId();
  4. void setId(int id);
  5. UserRole getUserRole();
  6. void setUserRole(UserRole userRole);
  7. String getName();
  8. void setName(String name);
  9. String getPhone();
  10. void setPhone(String phone);
  11. String getEmail();
  12. void setEmail(String email);
  13. }
  14.  
  15. @Entity
  16. @NamedQuery(name = "GenericUser.findAll", query = "SELECT u FROM GenericUser u")
  17. public class GenericUser implements User {
  18.  
  19. @Id
  20. @GeneratedValue()
  21. private int id;
  22. private UserRole userRole;
  23. private String name;
  24. private String phone;
  25. private String email;
  26.  
  27. public GenericUser(int id, UserRole userRole, String name, String phone, String email) {
  28. this.id = id;
  29. this.userRole = userRole;
  30. this.name = name;
  31. this.phone = phone;
  32. this.email = email;
  33. }
  34.  
  35. public GenericUser(UserRole userRole, String name, String phone, String email){
  36. this.userRole = userRole;
  37. this.name = name;
  38. this.phone = phone;
  39. this.email = email;
  40. }
  41.  
  42. public GenericUser(){
  43. }
  44. ....get/set....
  45.  
  46. public interface DAO<T>
  47. {
  48. T findById(int id);
  49. Collection<T> findAll();
  50. void save(T t);
  51. void update(T t);
  52. void delete(T t);
  53. void delete(Collection <T> tCollection);
  54. }
  55.  
  56. @TransactionManagement(value= TransactionManagementType.CONTAINER)
  57. @TransactionAttribute(value=REQUIRED)
  58. public abstract class AbstractJpaDao<T> implements DAO<T>
  59. {
  60. private static final Logger LOG = Logger.getLogger(AbstractJpaDao.class);
  61.  
  62. @PersistenceContext(unitName = "cwPU")
  63. protected EntityManager entityManager;
  64.  
  65. @Override
  66. public abstract T findById(int id);
  67.  
  68. @Override
  69. public abstract Collection<T> findAll();
  70.  
  71. @Override
  72. public void save(T t){
  73. LOG.debug("saving " + t);
  74. entityManager.persist(t);
  75. }
  76.  
  77. @Override
  78. public void update(T t){
  79. entityManager.refresh(t);
  80. }
  81.  
  82. @Override
  83. public void delete(T t){
  84. delete(Collections.singletonList(t));
  85. }
  86.  
  87. @Override
  88. public void delete(Collection<T> collection){
  89. for (T t : collection){
  90. entityManager.remove(t);
  91. }
  92. }
  93.  
  94. @PreDestroy
  95. private void closeEntityManager(){
  96. entityManager.close();
  97. }
  98. }
  99.  
  100. @Stateless
  101. @Local(cw.data.DAO.class)
  102. public class GenericUserJpaDao extends AbstractJpaDao<User>
  103. {
  104. private static final Logger LOG = Logger.getLogger(GenericUserJpaDao.class);
  105. @Override
  106. public User findById(int id){
  107. LOG.debug("find by id: " + id);
  108. return entityManager.find(GenericUser.class, id);
  109. }
  110.  
  111. @Override
  112. public Collection<User> findAll(){
  113. TypedQuery<GenericUser> genericOrderTypedQuery = entityManager.createNamedQuery("GenericUser.findAll", GenericUser.class);
  114. Collection<User> usersToReturn = new ArrayList<User>();
  115. usersToReturn.addAll(genericOrderTypedQuery.getResultList());
  116. return usersToReturn;
  117. }
  118.  
  119. @Override
  120. public void save(User user) {
  121. LOG.debug("saving user " + user.getId());
  122. super.save(user);
  123. }
  124. }
  125.  
  126. public class CreateUserObject extends HttpServlet {
  127. @EJB
  128. DAO<User> userDAO;
  129. @Override
  130. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  131. GenericUser genericUser = new GenericUser();
  132. genericUser.setId(0);
  133. genericUser.setEmail("sample");
  134. genericUser.setName("name");
  135. genericUser.setPhone("1");
  136. genericUser.setUserRole(UserRole.ADMIN);
  137. userDAO.save(genericUser);
  138. Collection<User> users = userDAO.findAll();
  139. resp.getWriter().write("size - " + users.size());
  140. for (User user : users){
  141. resp.getWriter().write("id - " + user.getId());
  142. }
  143. }
  144. }
  145.  
  146. <persistence-unit name="cwPU" transaction-type="JTA">
  147. <provider>org.hibernate.ejb.HibernatePersistence</provider>
  148. <properties>
  149. <property name="hibernate.connection.driver_class" value="oracle.jdbc.driver.OracleDriver"/>
  150. <property name="hibernate.connection.url" value="jdbc:oracle:thin:@localhost:1521:xe"/>
  151. <property name="hibernate.connection.username" value="cw"/>
  152. <property name="hibernate.connection.password" value="cw"/>
  153. <property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect"/>
  154. <property name="hibernate.hbm2ddl.auto" value="create"/>
  155. </properties>
  156. </persistence-unit>
Add Comment
Please, Sign In to add comment