-->
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: ClassCastException when using @ManyToOne with lazy loading
PostPosted: Wed Dec 30, 2009 10:11 am 
Newbie

Joined: Wed Dec 30, 2009 9:41 am
Posts: 1
I have the following minimal example of entities that show my problem:
Code:
@Entity
@Inheritance(strategy = InheritanceType.JOINED)
public class AbstractProduct {
    @Id @GeneratedValue
    private Long id;
}


@Entity
public class Product extends AbstractProduct {
     //...
}


@Entity
public class ProductOrder {
    @Id @GeneratedValue
    private Long id;

    @ManyToOne(optional = false, fetch = FetchType.LAZY)
    private AbstractProduct product;
}


The ProductOrder class references an AbstractProduct using @ManyToOne. In this example the concrete implementation will always be Product, bust that's just to keep it simple. If I now load a ProductOrder from the db and cast the AbstractProduct to Product I get a "java.lang.ClassCastException: mypackage.entity.AbstractProduct_$$_javassist_3 cannot be cast to mypackage.entity.Product".
Code:
        ProductOrder orderFromDb = (ProductOrder) session.get(ProductOrder.class, order.id);
        Product product = (Product) orderFromDb.product;


How would I get access to the actual Product implementation when using lazy loading? Is there anything that I am doing wrong? Or do I have to switch to eager loading? I would have thought that the proxy could deal with such situations. But maybe I am wrong.


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.