-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: Inheritance parent-child bidirectional association
PostPosted: Mon Apr 10, 2006 10:53 am 
Newbie

Joined: Sat Apr 08, 2006 8:15 am
Posts: 4
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


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.