JPA:表关系问题

时间:2011-06-06 15:19:46

标签: java jpa one-to-many table-relationships

我有两张桌子:预订和机器。所以,我有两个班:

预订

@javax.persistence.Entity
@Table(name = "reservation")
@NamedQueries({
@NamedQuery(
        name = "getActiveReservationByStatus",
        query = "select reservation from Reservation reservation where reservation.status=:status")  
       })
public class Reservation implements Serializable {
    @javax.persistence.Id
    @Column(name = "reservation_id")
    @javax.persistence.GeneratedValue(strategy = javax.persistence.GenerationType.AUTO)
    private int id;
    @Column(name = "reservation_status")
    private int status = 1;
    @Column(name = "user_id")
    private int userId;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY, mappedBy="reservation")
    private List<Machine> machineResultList;   
    public Reservation() {
    }

    public List<Machine> getMachineResultList() {
          return machineResultList;
    }
    public void setMachineResultList(List<Machine> machineResultList) {
        this.machineResultList = machineResultList;
    }

    //getters, setters...
}

@javax.persistence.Entity
@Table(name = "testmachine")

@NamedQueries({
@NamedQuery(
        name = "getMachineByState",
        query = "select testmachine from TestMachine testmachine where testmachine.reservation.userId =:status") ,
@NamedQuery(
        name = "getMachineNameByReservation",
        query = "select machine from TestMachine machine where machine.reservation.status =:status")
      })

public class TestMachine implements Serializable {
    @javax.persistence.Id
    @Column(name = "machine_id")
    @javax.persistence.GeneratedValue(strategy = javax.persistence.GenerationType.AUTO)
    private int id;

    @ManyToOne(cascade= {CascadeType.REFRESH}, fetch=FetchType.LAZY)
    @JoinColumn(name="reservation_id")
    private Reservation reservation;

    @Column(name = "machine_state")
    private boolean state = false;
    @Column(name = "machine_ip")
    private String machineIP;

    public void setReservationId(Reservation reservation) {
        this.reservation = reservation;
    }

    public String getReservationId() {
        return reservation.getReservationPurpose();
    }

    //getters, setters... 

如果我使用getActiveReservationByStatus查询,我可以使用Reservation类中定义的getter和setter从预订表中获取值。但我无法从machineResultList获取值。

我应该如何修改getMachineResultList getter以便从该List中获取信息(来自机器表的信息)?

0 个答案:

没有答案