Advertisement
Guest User

Untitled

a guest
Dec 16th, 2016
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. package com.emmaobo.expensetracker.model;
  2.  
  3. import java.util.List;
  4.  
  5. import javax.persistence.Column;
  6. import javax.persistence.Entity;
  7. import javax.persistence.EnumType;
  8. import javax.persistence.Enumerated;
  9. import javax.persistence.GeneratedValue;
  10. import javax.persistence.Id;
  11. import javax.persistence.JoinTable;
  12. import javax.persistence.OneToMany;
  13. import javax.persistence.Table;
  14.  
  15. import com.emmaobo.expensetracker.enumeration.AccountType;
  16.  
  17. @Entity
  18. @Table(name="USERS")
  19. public class User
  20. {
  21.  
  22. @Id @GeneratedValue
  23. private Long id;
  24.  
  25. @Column(name="USERNAME")
  26. private String username;
  27.  
  28. @Column(name="PASSWORD")
  29. private String password;
  30.  
  31. @Column(name="EMAIL")
  32. private String email;
  33.  
  34. @Enumerated(EnumType.STRING)
  35. private AccountType account;
  36.  
  37. @OneToMany(targetEntity=ExpenseList.class)
  38. @JoinTable(name = "USER_LISTS")
  39. private List<ExpenseList> list;
  40.  
  41.  
  42. public User(){}
  43.  
  44. public User(String username, String password, String email)
  45. {
  46. this.username = username;
  47. this.password = password;
  48. this.email = email;
  49. }
  50.  
  51.  
  52. public Long getId() {
  53. return id;
  54. }
  55.  
  56. public void setId(Long id) {
  57. this.id = id;
  58. }
  59.  
  60. public String getUsername() {
  61. return username;
  62. }
  63.  
  64. public void setUsername(String username) {
  65. this.username = username;
  66. }
  67.  
  68. public String getPassword() {
  69. return password;
  70. }
  71.  
  72. public void setPassword(String password) {
  73. this.password = password;
  74. }
  75.  
  76. public String getEmail() {
  77. return email;
  78. }
  79.  
  80. public void setEmail(String email) {
  81. this.email = email;
  82. }
  83.  
  84. public AccountType getAccount() {
  85. return account;
  86. }
  87.  
  88. public void setAccount(AccountType account) {
  89. this.account = account;
  90. }
  91.  
  92. public List<ExpenseList> getList() {
  93. return list;
  94. }
  95.  
  96. public void setList(List<ExpenseList> list) {
  97. this.list = list;
  98. }
  99.  
  100. public void addList(ExpenseList newList)
  101. {
  102. this.list.add(newList);
  103. }
  104.  
  105. }
  106.  
  107. package com.emmaobo.expensetracker.model;
  108.  
  109. import java.math.BigDecimal;
  110. import java.util.List;
  111.  
  112. import javax.persistence.Column;
  113. import javax.persistence.Entity;
  114. import javax.persistence.GeneratedValue;
  115. import javax.persistence.Id;
  116. import javax.persistence.OneToMany;
  117. import javax.persistence.Table;
  118.  
  119. @Entity
  120. @Table(name="EXPENSE_LIST")
  121. public class ExpenseList
  122. {
  123. @Id @GeneratedValue
  124. private Long id;
  125.  
  126. private String title;
  127.  
  128. @Column(name = "TOTAL")
  129. private BigDecimal total;
  130.  
  131. @OneToMany(targetEntity = Item.class)
  132. private List<Item> items;
  133.  
  134. public ExpenseList(){}
  135.  
  136. public ExpenseList(String title)
  137. {
  138. this.title = title;
  139. }
  140.  
  141. public Long getId() {
  142. return id;
  143. }
  144.  
  145. public void setId(Long id) {
  146. this.id = id;
  147. }
  148.  
  149. public BigDecimal getTotal() {
  150. return total;
  151. }
  152.  
  153. public void setTotal(BigDecimal total) {
  154. this.total = total;
  155. }
  156.  
  157. public List<Item> getItems() {
  158. return items;
  159. }
  160.  
  161. public void setItems(List<Item> items) {
  162. this.items = items;
  163. }
  164.  
  165. public String getTitle() {
  166. return title;
  167. }
  168.  
  169. public void setTitle(String title) {
  170. this.title = title;
  171. }
  172. }
  173.  
  174. public List<ExpenseList> viewUsersLists(Long id)
  175. {
  176. //TODO fix this (study how to retrieve data from Joined Tables)
  177. em = emf.createEntityManager();
  178. et = em.getTransaction();
  179. List<ExpenseList>usersLists = new ArrayList<ExpenseList>();
  180.  
  181. @SuppressWarnings("unchecked")
  182. TypedQuery<Long> query = (TypedQuery<Long>) em.createQuery("SELECT LIST_ID FROM USERS_EXPENSE_LIST Where USER_ID = "+id);
  183. List<Long> usersListIDs = query.getResultList();
  184.  
  185. for(Long expid : usersListIDs)
  186. {
  187. usersLists.add((ExpenseList)em.createQuery("SELECT id FROM ExpenseList Where id ="+expid, ExpenseList.class));
  188. }
  189.  
  190. return usersLists;
  191.  
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement