[英]Spring JPA NoRepositoryBean Queries
How I can use @Query
in @NoRepositoryBean
methods with @MappedSuperclass
? 如何使用
@Query
在@NoRepositoryBean
方法中使用@MappedSuperclass
?
I have @MappedSuperclass Temporal (Basis have Long id) 我有@MappedSuperclass Temporal(基本具有长id)
@MappedSuperclass
public abstract class Temporal extends Basis<Long> {
private Long temporalCode;
private Date dateBegin;
private Date dateEnd;
// getters/setters
}
and @NoRepositoryBean TemporalRepository 和@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);
}
Update: 更新:
For example I have temporal entity Worker: 例如,我有临时实体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);
}
Actualy you could use SpEL expressions for it. 实际上,您可以使用SpEL表达式。 Spring Data JPA reference
Spring Data JPA参考
So we can replace name of superclass with #{#entityName}
因此我们可以用
#{#entityName}
替换超类的名称
Example from reference: 参考示例:
@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> { … }
Solution for my example: 我的示例的解决方案:
@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 don't think you can do this because @NoRepositoryBean
is used to tell spring that don't create a repository proxy bean for this interface. KI认为您无法执行此操作,因为
@NoRepositoryBean
用于告诉spring不要为此接口创建存储库代理bean。 Such interface is used to expose some additional methods other than concrete repository interface. 除了具体的存储库接口之外,该接口还用于公开一些其他方法。 In that case you have to
extend
such interface, I will take another example to explain this: 在这种情况下,您必须
extend
这样的接口,我将再举一个例子来说明这一点:
Consider I have Student
and Teacher
entity : 考虑我有
Student
和Teacher
实体:
Now I have interface as: 现在我的界面为:
@NoRepositoryBean
public interface ReadOnlyRepository<T, ID extends Serializable> extends JpaRepository<T, ID> {
T findById(int id);
}
Now we have to write an concrete repo interface like: 现在我们必须编写一个具体的回购接口,例如:
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
}
In this way it's possible to manually define the queries both Student and Teacher by simply declaring JPA named queries Student.findById
and Teacher.findById
. 通过这种方式,可以通过简单地声明名为查询
Student.findById
和Teacher.findById
JPA来手动定义Student和Teacher的查询。 Using this approach you can easily come up with tailor-made base interfaces for your application scenario. 使用这种方法,您可以轻松地为您的应用程序场景量身定制基础接口。
So @NoRepositoryBean is used to avoid creating repository beans. 因此,@NoRepositoryBean用于避免创建存储库bean。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.