目前我在我的数据库中有这样的关系
我有适当的课程:
基本超类:
@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
}
客户端:
@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
}
师:
@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
}
基本上,我需要的是基于id匹配从单个对象中的两个表中获取数据。 不太可能它对我不起作用。
我一直收到此错误:
ClassCastException:org.hibernate.mapping.JoinedSubclass不能 强制转换为org.hibernate.mapping.RootClass
我知道如何解决它?
答案 0 :(得分:0)
尝试以下更改。
人员类
@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;
培训师班级
@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;
客户等级
@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;
为每个具体类添加了主键。
希望这有帮助。