我使用继承类型“TABLE_PER_CLASS”在我的项目中定义了 2 个实体。之后,我定义了 2 个存储库来访问数据,但是,当我使用它们查找 B 实体的记录时,生成的查询不包含 JOIN 语句,而仅包含“SELECT id, name, alternate_name from b”,它失败了,因为“名称”字段不存在。
我错过了什么我看不到的东西吗?
实体 A:
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@Table(name = "a")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public abstract class A implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
@SequenceGenerator(name = "sequenceGenerator")
private Long id;
@Column(name = "name")
private String name;
...
...
}
实体 B:
@Entity
@Table(name = "b")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class B extends A implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "sequenceGenerator")
@SequenceGenerator(name = "sequenceGenerator")
private Long id;
@Column(name = "alternate_name")
private String alternateName;
...
...
}
基础存储库:
@SuppressWarnings("unused")
@NoRepositoryBean
public interface BaseRepository<T extends A> extends JpaRepository<T, Long> {
}
存储库:
@SuppressWarnings("unused")
@Repository
public interface ARepository extends BaseRepository<A> {
}
B存储库:
@SuppressWarnings("unused")
@Repository
public interface BRepository extends BaseRepository<B> {
}