繁体   English   中英

Spring JPA NoRepositoryBean查询

[英]Spring JPA NoRepositoryBean Queries

如何使用@Query@NoRepositoryBean方法中使用@MappedSuperclass

我有@MappedSuperclass Temporal(基本具有长id)

@MappedSuperclass
public abstract class Temporal extends Basis<Long> {

    private Long temporalCode;
    private Date dateBegin;
    private Date dateEnd;
    // getters/setters
}

@NoRepositoryBean TemporalRepository

@NoRepositoryBean
public interface TemporalRepository<T extends Temporal> extends JpaRepository<T, Long> {
//    this two are ok
    List<T> findByTemporalCode(Long temporalCode);
    List<T> findByTemporalCodeAndDateBeginBeforeAndDateEndAfter(Long temporalCode, Date dateBegin, Date dateEnd);

//    query not working because Temporal is not an @Entity
//    @Query("select t from Temporal t 
//         where (t.dateBegin < ?1 or t.dateBegin is null) 
//         and (t.dateEnd < ?1 or t.dateEnd is null) ")
//    List<T> findByDate(Date date);
}

更新:
例如,我有临时实体Worker:

@Entity
public class Worker extends Temporal {
//    fields and methods here
}

public interface WorkerRepo extends TemporalRepository<Worker> {
//    Looks like it will work but I don't want to write the same method in each TemporalRepository subclass
//    @Query("select w from Worker w where (w.dateBegin < ?1 or w.dateBegin is null) and (w.dateEnd < ?1 or w.dateEnd is null) ")
//    List<Worker> findByDate(Date date);
}

实际上,您可以使用SpEL表达式。 Spring Data JPA参考
因此我们可以用#{#entityName}替换超类的名称

参考示例:

@MappedSuperclass
public abstract class AbstractMappedType {
  …
  String attribute
}

@Entity
public class ConcreteType extends AbstractMappedType { … }

@NoRepositoryBean
public interface MappedTypeRepository<T extends AbstractMappedType>
  extends Repository<T, Long> {

  @Query("select t from #{#entityName} t where t.attribute = ?1")
  List<T> findAllByAttribute(String attribute);
}

public interface ConcreteRepository
  extends MappedTypeRepository<ConcreteType> { … }

我的示例的解决方案:

@NoRepositoryBean
public interface TemporalRepository<T extends Temporal> extends JpaRepository<T, Long> {
    List<T> findByTemporalCode(Long temporalCode);

    List<T> findByTemporalCodeAndDateBeginBeforeAndDateEndAfter(Long temporalCode, Date dateBegin, Date dateEnd);

    @Query("select t from #{#entityName} t 
        where (t.dateBegin < ?1 or t.dateBegin is null)
        and (t.dateEnd < ?1 or t.dateEnd is null)")
    List<T> findByDate(Date dateBegin);
}

KI认为您无法执行此操作,因为@NoRepositoryBean用于告诉spring不要为此接口创建存储库代理bean。 除了具体的存储库接口之外,该接口还用于公开一些其他方法。 在这种情况下,您必须extend这样的接口,我将再举一个例子来说明这一点:

考虑我有StudentTeacher实体:

现在我的界面为:

@NoRepositoryBean
public interface ReadOnlyRepository<T, ID extends Serializable> extends JpaRepository<T, ID> {
  T findById(int id);
}

现在我们必须编写一个具体的回购接口,例如:

public interface StudentRepository extends ReadOnlyRepository<Student, Long> {
  //override the methods of `ReadOnlyRepository` with @query
}

public interface TeacherRepository extends ReadOnlyRepository<Teacher, Long> { 

//override the methods of `ReadOnlyRepository` with @query
}

通过这种方式,可以通过简单地声明名为查询Student.findByIdTeacher.findById JPA来手动定义Student和Teacher的查询。 使用这种方法,您可以轻松地为您的应用程序场景量身定制基础接口。

因此,@NoRepositoryBean用于避免创建存储库bean。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM