Hi,
I am trying to understand how to implement inheritance and bidirectional association and make it work.
I am using annotations but hte same goes for xml mapping
I have a base class with a one o many list of Reservation objects
Code:
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
Reservable {
private Long id;
@Id @GeneratedValue(strategy=GenerationType.AUTO)
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
private List<Reservation> reservations;
@OneToMany(mappedBy="reservable",cascade = {CascadeType.ALL}, fetch = FetchType.EAGER) @org.hibernate.annotations.Cascade(org.hibernate.annotations.CascadeType.DELETE_ORPHAN)
@OrderBy("startDate")
public List<Reservation> getReservations() {
if (reservations == null){
reservations = new ArrayList<Reservation>();
}
return reservations;
}
public void setReservations(List<Reservation> reservations) {
this.reservations = reservations;
}
}
you can notice I use
join inheritance strategy
I have to subclasses of Reservable
Code:
@Entity
@Table(name="room")
public class Room extends Reservable{
}
@Entity
@Table(name="locker")
public class Locker extends Reservable{
}
and Reservation class has a many to one (typical bidirectional parent-child) to Reservable
Code:
@Entity
@Table(name="reservation")
public class Reservation{
...
@ManyToOne
@Column(name="reservable")
public Reservable getReservable() {
return reservable;
}
public void setReservable(Reservable reservable) {
this.reservable = reservable;
}
}
This works fine, when I create and save a Room or a Locker with some reservations, all gets neatly saved into db.
And I can then call a
Code:
session.get(Locker, id)
or
Code:
session.get(Room, id)
and I get the object with its reservations.
Fine.
Now I need to query all reservations of a particular type of object (either Room or Locker)
Question is: how can I do it? how can I specify in a Criteria (best) or HQL to retrieve a list of all reservations but only for rooms and not for lockers?
I looked in the forums and wiki but haven't found an answer yrt.
Thanks in advance for help
Francesco