1 package info.ejava.examples.ejb.ejbjpa.bo; 2 3 import java.io.Serializable; 4 5 import javax.persistence.Column; 6 import javax.persistence.Entity; 7 import javax.persistence.FetchType; 8 import javax.persistence.Id; 9 import javax.persistence.JoinColumn; 10 import javax.persistence.ManyToOne; 11 import javax.persistence.NamedQueries; 12 import javax.persistence.NamedQuery; 13 import javax.persistence.OneToOne; 14 import javax.persistence.Table; 15 16 @Entity 17 @Table(name="EJBJPA_ROOM") 18 @NamedQueries({ 19 @NamedQuery(name="Room.findRoomByGuest", 20 query="select r from Room r where r.occupant=:guest") 21 }) 22 public class Room implements Serializable { 23 private static final long serialVersionUID = 1L; 24 25 @Id 26 @Column(name="ROOM_NUMBER") 27 private int number; 28 29 @ManyToOne(optional=false, fetch=FetchType.LAZY) 30 @JoinColumn(name="FLOOR_ID") 31 private Floor floor; 32 33 @OneToOne(optional=true, fetch=FetchType.LAZY) 34 @JoinColumn(name="OCCUPANT_ID") 35 private Guest occupant; 36 37 public Room() {} 38 public Room(Floor floor, int number) { 39 this.floor = floor; 40 this.number = number; 41 } 42 43 public int getNumber() { return number; } 44 public Floor getFloor() { return floor; } 45 46 public Guest getOccupant() { return occupant; } 47 public void setOccupant(Guest occupant) { 48 this.occupant = occupant; 49 } 50 51 @Override 52 public String toString() { 53 StringBuilder builder = new StringBuilder(); 54 builder.append("Room [number=").append(number) 55 .append(", occupant=").append(occupant).append("]"); 56 return builder.toString(); 57 } 58 }