簡體   English   中英

處理實體繼承Spring Boot

[英]Handling entities inheritance spring boot

我正在使用本教程來處理實體繼承。 我有擴展User實體的個人和公司實體。

@Entity
@Inheritance
public abstract class User { 

@Id
private long id;

@NotNull
private String email;

// getters and settres
}

@Entity
public class Person extends User { 
private int age;
// getters and settres and other attributs
}

@Entity
public class Company extends User { 
private String companyName;
// getters and settres and other attribut
}

然后擴展UserBaseRepository的UserRpository,PersonRepository和Company Repository。

@NoRepositoryBean
public interface UserBaseRepository<T extends User> 
extends CrudRepository<T, Long> {

public T findByEmail(String email);

}

@Transactional
public interface UserRepository extends UserBaseRepository<User> { }

@Transactional
public interface PersonRepository extends UserBaseRepository<Person> { }

@Transactional
public interface CompanyRepository extends UserBaseRepository<Company> { }

問題是當調用personRepository.findAll()來獲取所有人時,結果我也得到了公司。

您的問題出在JPA要求的“ Discriminator”列中。 您正在使用@Inheritance批注,默認情況下將使用InheritanceType.SINGLE_TABLE策略。 這意味着:

  1. 您繼承的實體PersonCompany將進入一個表。
  2. JPA將需要一個鑒別器來區分實體類型。

為了使它適用於您的用例,我做了以下工作:

實體:

@Inheritance
@Entity
@Table(name = "user_table")
public abstract class User {

    @Id
    private long id;

    @NotNull
    @Column
    private String email;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }
}

@Entity
public class Company  extends User {

    @Column(name = "company_name")
    private String companyName;

    public String getCompanyName() {
        return companyName;
    }

    public void setCompanyName(String companyName) {
        this.companyName = companyName;
    }
}

@Entity
public class Person extends User {

    @Column
    private int age;

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

數據庫架構:

-- user table
create table user_table (
  id BIGINT         NOT NULL PRIMARY KEY,
    email             VARCHAR(50) NOT NULL,
    age               INT,
    company_name      VARCHAR(50),
    dtype             VARCHAR(80) -- Discriminator
);

一些測試數據:

insert into user_table(id, dtype, age, email) values
(1,'Person', 25, 'john.doe@email.com'),
(2,'Person',22, 'jane.doe@email.com');

insert into user_table(id, dtype, company_name, email) values
(3,'Company','Acme Consultants', 'acme@company.com'),
(4,'Company', 'Foo Consultants', 'foo@company.com');

倉庫:

@NoRepositoryBean
public interface UserBaseRepository<T extends User> extends CrudRepository<T, Long> {

    T findByEmail(String email);
}

@Transactional
public interface PersonRepository extends UserBaseRepository<Person> {

}

@Transactional
public interface CompanyRepository extends UserBaseRepository<Company> {

}

JUnit測試:

public class MultiRepositoryTest extends BaseWebAppContextTest {

    @Autowired
    private PersonRepository personRepository;

    @Autowired
    private CompanyRepository companyRepository;

    @Test
    public void testGetPersons() {
        List<Person> target = new ArrayList<>();
        personRepository.findAll().forEach(target::add);
        Assert.assertEquals(2, target.size());
    }
    @Test
    public void testGetCompanies() {
        List<Company> target = new ArrayList<>();
        companyRepository.findAll().forEach(target::add);
        Assert.assertEquals(2, target.size());
    }

}

以上測試通過。 這表明JPA現在正確使用了鑒別器來檢索所需的記錄。

有關您的問題的JPA相關理論,請參見此鏈接

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM