我知道如何实现 spring 数据存储库,
创建一个像这样的界面:
public interface CountryRepository extends CrudRepository<Country, Long> {}
Now Country
is an AbstractCatalog
我的项目中有(很多)更多目录。
我想知道是否可以只创建一个适用于所有目录的存储库:
public interface AbstractCatalogRepository extends CrudRepository<AbstractCatalog, Long> {}
现在我在保存时没有看到问题,但如果我想搜索AbstractCatalog
我已经确定我会碰壁,因为存储库不知道它必须选择哪个子类。
抽象目录.class
@MappedSuperclass
public abstract class AbstractCatalog extends PersistentEntity {
/**
* The Constant serialVersionUID.
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
/**
* The code.
*/
@Column(unique = true, nullable = false, updatable = false)
private String code;
/**
* The description.
*/
@Column(nullable = false)
private String description;
/**
* The in use.
*/
@Column(name = "IN_USE", nullable = false, columnDefinition = "bit default 1")
private Boolean inUse = Boolean.TRUE;
// getters and setters
}
国家级
@Entity
@Table(name = "tc_country")
@AttributeOverrides({
@AttributeOverride(name = "id", column =
@Column(name = "COUNTRY_SID")),
@AttributeOverride(name = "code", column =
@Column(name = "COUNTRY_CODE")),
@AttributeOverride(name = "description", column =
@Column(name = "COUNTRY_DESCRIPTION"))})
public class Country extends AbstractCatalog {
public static final int MAX_CODE_LENGTH = 11;
@Column(name = "GEONAMEID", nullable = true, unique = false)
private Long geonameid;
// getter and setter
}
有谁知道我如何只使用一个存储库来实现AbstractCatalog
类而不必一遍又一遍地创建相同的接口,并且名称和其他属性的差异最小?