我正在与此tutorial一起处理实体继承。我有扩展用户实体的个人和公司实体。
@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()来获取所有人时,结果我也得到了公司。
答案 0 :(得分:4)
您的问题是JPA要求的“ Discriminator”列。您正在使用@Inheritance
注释,默认情况下将使用InheritanceType.SINGLE_TABLE
策略。这意味着:
Person
和Company
将进入一个表。为了使它适用于您的用例,我做了以下工作:
实体:
@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相关理论,请参见此link。