View Javadoc
1   package info.ejava.examples.jaxrs.todos.bo;
2   
3   import java.util.LinkedList;
4   import java.util.List;
5   
6   import javax.persistence.CascadeType;
7   import javax.persistence.Column;
8   import javax.persistence.Entity;
9   import javax.persistence.FetchType;
10  import javax.persistence.GeneratedValue;
11  import javax.persistence.GenerationType;
12  import javax.persistence.Id;
13  import javax.persistence.NamedQuery;
14  import javax.persistence.OneToMany;
15  import javax.persistence.OrderBy;
16  import javax.persistence.Table;
17  
18  @Entity
19  @Table(name="ETODO_TODO_LIST")
20  @NamedQuery(name="TodoList.getTodoLists",
21              query="select tl from TodoList tl order by tl.name asc"
22  )
23  @NamedQuery(name="TodoList.getListByName",
24              query="select tl from TodoList tl where tl.name=:name"
25  )
26  public class TodoList {
27      @Id @GeneratedValue(strategy=GenerationType.SEQUENCE)
28      private int id;
29      
30      @Column(name="NAME", length=32, nullable=false, unique=true)
31      private String name;
32      
33      @OneToMany(mappedBy="todoList", 
34              fetch=FetchType.EAGER, 
35              cascade= {CascadeType.PERSIST,
36                        CascadeType.REFRESH,
37                        CascadeType.DETACH,
38                        CascadeType.REMOVE}, 
39              orphanRemoval=true)
40      @OrderBy("priority ASC")
41      private List<TodoItem> todoItems;
42  
43      public int getId() {
44          return id;
45      }
46  
47      public void setId(int id) {
48          this.id = id;
49      }
50  
51      public String getName() {
52          return name;
53      }
54  
55      public void setName(String name) {
56          this.name = name;
57      }
58  
59      public List<TodoItem> getTodoItems() {
60          if (todoItems==null) {
61              todoItems=new LinkedList<>();
62          }
63          return todoItems;
64      }
65  
66      public void setTodoItems(List<TodoItem> todoItems) {
67          this.todoItems = todoItems;
68      }
69  
70      @Override
71      public String toString() {
72          StringBuilder builder = new StringBuilder();
73          builder.append("TodoList [id=").append(id)
74                 .append(", name=").append(name)
75                 .append(", todoItems=");
76          boolean first=true;
77          for (TodoItem item: getTodoItems()) {
78              if (!first) { builder.append(",").append(System.lineSeparator()); }
79              builder.append(item);
80              first=false;
81          }
82          builder.append("]");
83          return builder.toString();
84      }
85      
86      
87  }