【问题标题】:JPA. Map 2 classes based on one single superclassJPA。基于一个超类映射 2 个类
【发布时间】:2015-11-28 21:26:00
【问题描述】:

目前我的数据库中有这样的关系

我有合适的课程:

基本超类:

@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

知道如何解决吗?

【问题讨论】:

标签: java hibernate jpa


【解决方案1】:

尝试以下更改。

人员类

@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;

为每个具体类添加了主键。

希望这会有所帮助。

【讨论】:

  • 不太可能行不通:This class [class org.garage48.models.Person] does not define an IdClass
  • @PetrShypila:好的。然后请尝试使用 Person 类中未注释的 id。为什么要在抽象类和具体类中使用 ID?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多