title: How can I cast from one class to another class in hibernate
body: <p>Suppose there are three classes:</p>
  
  <p>One is:</p>
  
  <pre><code>@Entity
  @Indexed
  @Inheritance(strategy = InheritanceType.SINGLE_TABLE)
  @DiscriminatorColumn(name = "type", discriminatorType = DiscriminatorType.STRING)
  @Table(name = "tbl_a")
  public class A {
      @Id
      @DocumentId
      @GeneratedValue(generator="rwSpecSeq")
      @SequenceGenerator(name="rwSpecSeq", sequenceName="RW_SPEC_SEQ")
      private int id;
      private String sampleAttribute;
  
      //Getters and Setters
  }
  
  @Entity
  @DiscriminatorValue("b")
  public class B extends A {
  }
  
  @Entity
  @DiscriminatorValue("c")
  public class C extends A {
  }
  </code></pre>
  
  <p>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.</p>
  
  <p>My approach was something like this:</p>
  
  <pre><code>//Assuming that the object is C class' object.
  A a = adminService.getA(request.getParameter("id"));
  //before merging I did this
  B b = (B)a;
  adminService.save(b);
  </code></pre>