-->
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 query property child
PostPosted: Thu Jul 18, 2013 10:13 am 
Newbie

Joined: Thu Jul 18, 2013 9:42 am
Posts: 1
Hi,

I use the single table strategy (with discriminator) to use inheritance

UML schema
Image

I would like to fetch all the orders of a customer with all the associations related (cars, books and tvs). Do you know how to achieve this without breaking the model classes.

Code:

@Entity
@Table(name = "customers")
public class Customer{

    private Date birthDate;
    private String name;

    @OneToMany(fetch = FetchType.LAZY, mappedBy = "customer", cascade = CascadeType.ALL, orphanRemoval = true)
    @OrderBy("order")
    private List<? extends Order> orders;

}

@Entity
@Table(name = "orders")
@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.STRING)
public abstract class Order{

    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @NotNull
    @JoinColumn(name = "CustomerID")
    private Customer customer;
}


@Entity
@DiscriminatorValue("book")
public class BookOrder {

  @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
  @JoinColumn(name="book_id")
  private Set<Book> books;
}

@Entity
@DiscriminatorValue("car")
public class CarOrder {

  @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
  @JoinColumn(name="car_id")
  private Set<Car> cars;
}


@Entity
@DiscriminatorValue("tv")
public class TvOrder {

  @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, orphanRemoval = true)
  @JoinColumn(name="tv_id")
  private Set<Tv> tvs;
}





if im doing in hql
Code:
select cutomer from customer customer
inner join fetch customer.orders order
left join fetch order.cars
left join fetch order.books
left join fetch order.tvs


Im getting the error
org.hibernate.QueryException: could not resolve property: cars, and it makes sense in the abstract class Order this field doesnt exist.

Do you know how i can achieve this ? what is the recommendation of hibernate in this case ?

My goal is to simple a simple query and to fetch everything


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.