0

我对 JPA 继承有疑问。数据库模型也是专门搭建的。它包含几个具有相同属性的表(这些表是故意按国家/地区切割的),并且所有这些表都连接到另一个表(OneToOne)。

以下是数据模型的示例:usa_user、Germany_user、austria_user。所有这些表都具有相同的属性(id、name、address)。现在地址也是根据国家建立的,例如usa_address、Germany_address、austria_address。

现在我不知道或有问题,我已经正确映射了很长时间。我有以下内容:

// All Lombok Getter, Setter Args,...
@MappedSuperclass
public abstract Address {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @JsonIgnore
    private Long id;


    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "user_id", referencedColumnName = "id")
    @JsonIgnore
    private User user;

    private String name;
    private String addr_num;
    
    ...
}

// All Lombok Getter, Setter Args,...
@MappedSuperclass
public abstract User {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @JsonIgnore
    private Long id;


    @OneToOne(mappedBy = "user", cascade = CascadeType.ALL, fetch = FetchType.LAZY, optional = false)
    @JsonIgnore
    private Address address;

    private String name;

}

@Entity
@Table(name = "usa_user")
public class UsaUser extends User {}

@Entity
@Table(name = "austria_user")
public class AustriaUser extends User {}

@Entity
@Table(name = "germany_user")
public class GermanyUser extends User {}


@Entity
@Table(name = "usa_address")
public class UsaAddress extends Address {}

@Entity
@Table(name = "austria_address")
public class AustriaAddress extends Address {}

@Entity
@Table(name = "germany_address")
public class GermanyAddress extends Address {}


But unfortunately this does not work. Every time I start it JPA notices that it can't map the Entities Address - User (which is understandable because they are not entities but abstract classes). What would be the best way to solve this? I want to avoid that I have to list the attributes in all these entities because it would be redundant.

The goal is to find out how I can use a @MappedSuperclass in a @MappedSuperclass.

4

1 回答 1

0

MappedSuperclass is not queryable and thus also not joinable. You need to map this as an abstract entity with the table per class inheritance strategy. Just switch to @Entity on the Address and User and add @Inheritance(TABLE_PER_CLASS).

于 2020-10-19T06:48:00.883 回答