View Javadoc
1   package info.ejava.examples.ejb.ejbjpa.bo;
2   
3   import java.io.Serializable;
4   import java.util.LinkedList;
5   import java.util.List;
6   
7   import javax.persistence.CascadeType;
8   import javax.persistence.Column;
9   import javax.persistence.Entity;
10  import javax.persistence.FetchType;
11  import javax.persistence.Id;
12  import javax.persistence.NamedQueries;
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="EJBJPA_FLOOR")
20  @NamedQueries({
21      @NamedQuery(name="Floor.getFloors", 
22              query="select f from Floor f order by level"),
23      @NamedQuery(name="Floor.fetchFloor", 
24              query="select f from Floor f "
25                      + "join fetch f.rooms r "
26                      + "join fetch r.occupant "
27                      + "where f.level=:level")
28  })
29  public class Floor implements Serializable {
30      private static final long serialVersionUID = 1L;
31  
32      @Id
33      @Column(name="LEVEL", nullable=false)
34      int level;
35      
36      @OneToMany(mappedBy="floor", 
37              fetch=FetchType.LAZY,
38              cascade={CascadeType.PERSIST, CascadeType.REMOVE, CascadeType.DETACH},
39              orphanRemoval=true)
40      @OrderBy("number")
41      List<Room> rooms;
42      
43      protected Floor() {}
44      public Floor(int level) {
45          this.level = level;
46      }
47          
48      public int getLevel() { return level; }
49  
50      public List<Room> getRooms() { return rooms; }
51      public void setRooms(List<Room> rooms) {
52          this.rooms = rooms;
53      }
54      public Floor withRoom(Room room) {
55          if (rooms==null) {
56              rooms = new LinkedList<Room>();
57          }
58          if (room!=null) {
59              rooms.add(room);
60          }
61          return this;
62      }
63  
64      @Override
65      public String toString() {
66          StringBuilder builder = new StringBuilder();
67          builder.append("Floor [level=").append(level)
68              .append(", rooms={").append("\n");
69          if (rooms!=null) { for (Room room: rooms) {
70              builder.append(room).append("\n");
71          }}
72          builder.append("}");
73          return builder.toString();
74      }
75  }