How can I cast from one class to another class in hibernate

Suppose there are three classes:

One is:

@Inheritance(strategy = InheritanceType.SINGLE_TABLE)
@DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.STRING)
@Table(name = "tbl_a")
public class A {
    @SequenceGenerator(name="rwSpecSeq", sequenceName="RW_SPEC_SEQ")
    private int id;
    private String sampleAttribute;

    //Getters and Setters

public class B extends A {

public class C extends A {

If one object is saved as B's object then if again I want to cast or change the object to C then it is giving class cast exception. Please tell me how to cast class in hibernate when merging.

My approach was something like this:

//Assuming that the object is C class' object.
A a = adminService.getA(request.getParameter("id"));
//before merging I did this
B b = (B)a;;


You just can't. Hibernate keeps the objects type when they are persisted to the database. And Java prevents casting objects from one type to another when they are incompatible.

In your case, you can't cast an instance of C in B. It would be like casting a String into an Integer.

Your best shot is to write a method that takes an instance of C and copies all the attributes of C into a new object of type B.

Need Your Help

zxing barcode scanner in a fixed div in phonegap android app

android jquery-mobile cordova zxing

I have added zxing barcode scanner plugin successfully in my android phonegap app here is js code where on page show I am able to see barcode scanner screen.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.