Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.emmaobo.expensetracker.model;
- import java.util.List;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.EnumType;
- import javax.persistence.Enumerated;
- import javax.persistence.GeneratedValue;
- import javax.persistence.Id;
- import javax.persistence.JoinTable;
- import javax.persistence.OneToMany;
- import javax.persistence.Table;
- import com.emmaobo.expensetracker.enumeration.AccountType;
- @Entity
- @Table(name="USERS")
- public class User
- {
- @Id @GeneratedValue
- private Long id;
- @Column(name="USERNAME")
- private String username;
- @Column(name="PASSWORD")
- private String password;
- @Column(name="EMAIL")
- private String email;
- @Enumerated(EnumType.STRING)
- private AccountType account;
- @OneToMany(targetEntity=ExpenseList.class)
- @JoinTable(name = "USER_LISTS")
- private List<ExpenseList> list;
- public User(){}
- public User(String username, String password, String email)
- {
- this.username = username;
- this.password = password;
- this.email = email;
- }
- public Long getId() {
- return id;
- }
- public void setId(Long id) {
- this.id = id;
- }
- public String getUsername() {
- return username;
- }
- public void setUsername(String username) {
- this.username = username;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public String getEmail() {
- return email;
- }
- public void setEmail(String email) {
- this.email = email;
- }
- public AccountType getAccount() {
- return account;
- }
- public void setAccount(AccountType account) {
- this.account = account;
- }
- public List<ExpenseList> getList() {
- return list;
- }
- public void setList(List<ExpenseList> list) {
- this.list = list;
- }
- public void addList(ExpenseList newList)
- {
- this.list.add(newList);
- }
- }
- package com.emmaobo.expensetracker.model;
- import java.math.BigDecimal;
- import java.util.List;
- import javax.persistence.Column;
- import javax.persistence.Entity;
- import javax.persistence.GeneratedValue;
- import javax.persistence.Id;
- import javax.persistence.OneToMany;
- import javax.persistence.Table;
- @Entity
- @Table(name="EXPENSE_LIST")
- public class ExpenseList
- {
- @Id @GeneratedValue
- private Long id;
- private String title;
- @Column(name = "TOTAL")
- private BigDecimal total;
- @OneToMany(targetEntity = Item.class)
- private List<Item> items;
- public ExpenseList(){}
- public ExpenseList(String title)
- {
- this.title = title;
- }
- public Long getId() {
- return id;
- }
- public void setId(Long id) {
- this.id = id;
- }
- public BigDecimal getTotal() {
- return total;
- }
- public void setTotal(BigDecimal total) {
- this.total = total;
- }
- public List<Item> getItems() {
- return items;
- }
- public void setItems(List<Item> items) {
- this.items = items;
- }
- public String getTitle() {
- return title;
- }
- public void setTitle(String title) {
- this.title = title;
- }
- }
- public List<ExpenseList> viewUsersLists(Long id)
- {
- //TODO fix this (study how to retrieve data from Joined Tables)
- em = emf.createEntityManager();
- et = em.getTransaction();
- List<ExpenseList>usersLists = new ArrayList<ExpenseList>();
- @SuppressWarnings("unchecked")
- TypedQuery<Long> query = (TypedQuery<Long>) em.createQuery("SELECT LIST_ID FROM USERS_EXPENSE_LIST Where USER_ID = "+id);
- List<Long> usersListIDs = query.getResultList();
- for(Long expid : usersListIDs)
- {
- usersLists.add((ExpenseList)em.createQuery("SELECT id FROM ExpenseList Where id ="+expid, ExpenseList.class));
- }
- return usersLists;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement