在 spring 存储库 @Query 中使用 @MappedSuperclass
Using @MappedSuperclass in spring repository @Query
我正在尝试为扩展我的基本存储库的所有存储库定义通用查询:
@NoRepositoryBean
public interface DocumentRepository<T extends BaseDocument> extends JpaRepository<T, Long> {
@Query(value = "FROM BaseDocument bd WHERE (bd.createdAt IS NULL OR :to IS NULL OR bd.createdAt < :to) AND (bd.deletedAt IS NULL OR :from IS NULL OR bd.deletedAt > :from)")
Iterable<T> findAllActiveBetween(OffsetDateTime from, OffsetDateTime to);
}
@MappedSuperclass
@Where(clause="deleted_at IS NULL")
abstract public class BaseDocument {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private OffsetDateTime createdAt;
private OffsetDateTime deletedAt;
}
有问题的部分是在@Query 中使用BaseDocument。
是否可以在所有子存储库中定义这样的方法而不重复?
当你使用@MappedSuperclass时继承类不共享一个table到select,所以他们每个人都需要单独查询。存储库的继承在这里帮不了你。
如果您使用另一种继承策略,例如 joined multiple table inheritance,则可以从超类的 table select,然后您可以使用 java 对象进行导航。如果您需要在查询中使用子类的属性,每个子类当然需要实现自己的查询。
@NoRepositoryBean
public interface DocumentRepository<T extends BaseDocument> extends JpaRepository<T, Long> {
@Query(value = "FROM BaseDocument bd WHERE (bd.createdAt IS NULL OR :to IS NULL OR bd.createdAt < :to) AND (bd.deletedAt IS NULL OR :from IS NULL OR bd.deletedAt > :from)")
Iterable<T> findAllActiveBetween(OffsetDateTime from, OffsetDateTime to);
}
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
@DiscriminatorColumn(name="BaseDocument_TYPE")
@Table(name="BaseDocument")
abstract public class BaseDocument {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private OffsetDateTime createdAt;
private OffsetDateTime deletedAt;
}
@Entity
@DiscriminatorValue("TextDocument")
@Table(name="TextDocument")
public class TextDocument extends BaseDocument {
private String text;
}
@Entity
@DiscriminatorValue("AudioDocument")
@Table(name="AudioDocument")
public class AudioDocument extends BaseDocument {
private byte [] audio;
}
我正在尝试为扩展我的基本存储库的所有存储库定义通用查询:
@NoRepositoryBean
public interface DocumentRepository<T extends BaseDocument> extends JpaRepository<T, Long> {
@Query(value = "FROM BaseDocument bd WHERE (bd.createdAt IS NULL OR :to IS NULL OR bd.createdAt < :to) AND (bd.deletedAt IS NULL OR :from IS NULL OR bd.deletedAt > :from)")
Iterable<T> findAllActiveBetween(OffsetDateTime from, OffsetDateTime to);
}
@MappedSuperclass
@Where(clause="deleted_at IS NULL")
abstract public class BaseDocument {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private OffsetDateTime createdAt;
private OffsetDateTime deletedAt;
}
有问题的部分是在@Query 中使用BaseDocument。 是否可以在所有子存储库中定义这样的方法而不重复?
当你使用@MappedSuperclass时继承类不共享一个table到select,所以他们每个人都需要单独查询。存储库的继承在这里帮不了你。
如果您使用另一种继承策略,例如 joined multiple table inheritance,则可以从超类的 table select,然后您可以使用 java 对象进行导航。如果您需要在查询中使用子类的属性,每个子类当然需要实现自己的查询。
@NoRepositoryBean
public interface DocumentRepository<T extends BaseDocument> extends JpaRepository<T, Long> {
@Query(value = "FROM BaseDocument bd WHERE (bd.createdAt IS NULL OR :to IS NULL OR bd.createdAt < :to) AND (bd.deletedAt IS NULL OR :from IS NULL OR bd.deletedAt > :from)")
Iterable<T> findAllActiveBetween(OffsetDateTime from, OffsetDateTime to);
}
@Entity
@Inheritance(strategy=InheritanceType.JOINED)
@DiscriminatorColumn(name="BaseDocument_TYPE")
@Table(name="BaseDocument")
abstract public class BaseDocument {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private OffsetDateTime createdAt;
private OffsetDateTime deletedAt;
}
@Entity
@DiscriminatorValue("TextDocument")
@Table(name="TextDocument")
public class TextDocument extends BaseDocument {
private String text;
}
@Entity
@DiscriminatorValue("AudioDocument")
@Table(name="AudioDocument")
public class AudioDocument extends BaseDocument {
private byte [] audio;
}