2

Currently I have such relations in my DB

enter image description here

And I have appropriate classes:

Basic superclass:

@Entity
@Table(name = "Person")
@Inheritance(strategy = InheritanceType.JOINED)
public abstract class Person {
    @Id
    @Column(name = "Id")
    @GeneratedValue(strategy = GenerationType.AUTO)
    protected Integer id;

    @Column(name = "Name")
    protected String name;

    @Column(name = "Birthday")
    protected Date birthDate;

    @Column(name = "Avatar")
    protected String avatarUrl;

    @Column(name = "Sex")
    protected Integer gender;
    //... Getters and Setters
}

Client:

@Entity
@Table(name = "Client")
@PrimaryKeyJoinColumn(name = "Id")
public class Client extends Person {
    @Id
    @Column(name = "PersonId")
    private Integer personId;

    @Column(name = "Weight")
    private Float weight;

    @Column(name = "WeightUnit")
    private Integer weightUnit;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name="PersonId")
    private Trainer trainer;
    //... Getters and Setters
}

Trainer:

@Entity
@Table(name = "Trainer")
@PrimaryKeyJoinColumn(name = "Id")
public class Trainer extends Person {

    @Id
    @Column(name = "PersonId")
    private Integer personId;

    @Column(name = "Description")
    private String description;

    @Column(name = "CurrencyCode")
    private Integer currencyCode;

    @Column(name = "Price")
    private Float price;

    @Column(name = "Rating")
    private Float rating;

    @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "trainer")
    private List<Client> clients;
    //Getters and Setters
}

So basically, what I need, is to grab data from both tables in a single object, based on id matching. Unlikely it doesn't work for me.

All the time I receive this error:

ClassCastException: org.hibernate.mapping.JoinedSubclass cannot be cast to org.hibernate.mapping.RootClass

Any idea how can I fix it?

Community
  • 1
  • 1
Petr Shypila
  • 1,449
  • 9
  • 27
  • 47
  • 1
    Remove `@Id` from the subclasses: http://stackoverflow.com/questions/12087011/spring-3-1-hibernate-4-exception-for-inheritance-cannot-be-cast-to-org-hibernat – Robert Niestroj Nov 28 '15 at 22:07

1 Answers1

0

Try with the following changes.

Person Class

@Table(name = "Person")
@Inheritance(strategy = InheritanceType.JOINED)
@MappedSuperclass
public abstract class Person {

//  @Column(name = "Id")
//  @GeneratedValue(strategy = GenerationType.AUTO)
//  protected Integer id;

    @Column(name = "Name")
    protected String name;

    @Column(name = "Birthday")
    protected Date birthDate;

    @Column(name = "Avatar")
    protected String avatarUrl;

    @Column(name = "Sex")
    protected Integer gender;

Trainer Class

@Entity
@Table(name = "Trainer")
public class Trainer extends Person {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "TrainerId")
    private Integer personId;

    @Column(name = "Description")
    private String description;

    @Column(name = "CurrencyCode")
    private Integer currencyCode;

    @Column(name = "Price")
    private Float price;

    @Column(name = "Rating")
    private Float rating;

    @OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "trainer")
    private List<Client> clients;

Client Class

@Entity
@Table(name = "Client")
public class Client extends Person {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "ClientId")
    private Integer personId;

    @Column(name = "Weight")
    private Float weight;

    @Column(name = "WeightUnit")
    private Integer weightUnit;

    @ManyToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "PersonId" , insertable = false, updatable= false)
    private Trainer trainer;

Added the primary key to each of the concrete classes.

Hope this helps.

tharindu_DG
  • 8,900
  • 6
  • 52
  • 64
  • Unlikely it won't work: `This class [class org.garage48.models.Person] does not define an IdClass` – Petr Shypila Nov 29 '15 at 09:22
  • @PetrShypila : ok. Then please try with the id uncommented in the Person class. Why r u using IDs in abstract class & concrete class? – tharindu_DG Nov 29 '15 at 09:25