Hi guys
I have an object say Basket that has a collection of Fruit which is a super class of Apple and Orange. What I'm trying to do is fetching a Basket with all of its Fruit objects initialized so I don't get class cast exception with polymorphism and proxy obj when doing something like
(Apple)(basket.getBasketFruits().getIterator().next()) ;
And consider the following criteria :
Criteria criteria = getSession().createCriteria(Basket.class); criteria.add(Restrictions.eq("id", basketId)); criteria.setFetchMode("basketFruits.fruit", FetchMode.JOIN); return (Basket) criteria.uniqueResult();
with the above criteria, the Fruit objects in the collection of Basket is still not initialized, could someone please point me to the right criteria query ? Below is the brief description of my mapping. and Note that I have some restriction that I need to have the Basket object graph intact
public class Basket { @OneToMany(fetch = FetchType.LAZY, mappedBy = "basket") private Set<BasketFruit> basketFruits= new HashSet<BasketFruit>(0); ... }
public class BasketFruit { ... @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "BASKET_ID") @ForeignKey(name = "FK_BASKET_FRUIT_BASKET") private Basket basket;
@ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "FRUIT_ID") @ForeignKey(name = "......") private Fruit fruit; ... }
@Entity @Inheritance(strategy = InheritanceType.JOINED) @Table(name = "FRUIT") public class Fruit { @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "BASKET_FRUIT_ID") @ForeignKey(name = "FK_FRUIT_BASKET_FRUIT") private BasketFruit BasketFruit ; .... }
@Entity @Table(name = "APPLE") @PrimaryKeyJoinColumn(name = "FRUIT_ID") public class Apple extends Fruit{ .... }|
|