Handling entities inheritance spring boot

匆匆过客 提交于 2020-01-24 21:10:30

问题


I'm working with this tutorial to handle entities inheritance. I have person and company entities that extends the User entity.

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

then UserRpository ,PersonRepository and Company Repository that extends the UserBaseRepository.

@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> { }

the probleme is when calling personRepository.findAll() to get all persons , in result i got also companies.


回答1:


Your issue is with the "Discriminator" column that JPA requires. You are using the @Inheritance annotation and by default that will use the InheritanceType.SINGLE_TABLE strategy. That means the following:

  1. Your inherited entities Person and Company will go into a single table.
  2. JPA will require a Discriminator to differentiate between entity types.

I did the following to make it work for your use case:

Entities:

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

DB Schema:

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

Some test data:

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');

Repositories:

@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 Tests:

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());
    }

}

The above tests pass. That indicates JPA now utilizes the discriminator correctly to retrieve the required records.

For JPA related theory for your question, see this link.



来源:https://stackoverflow.com/questions/53115122/handling-entities-inheritance-spring-boot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!