我有这个型号:
public class Event {
private String name;
private Date start;
private Date end;
}
Run Code Online (Sandbox Code Playgroud)
和存储库为
@Repository
public interface EventRepository extends JpaRepository<Event, Long> {
List<Event> findByEventTypeAccount(Account account);
}
Run Code Online (Sandbox Code Playgroud)
我想要做的是,我将传递一个日期,需要检查日期间start
和end
EG(我将通过9月30日期和需要找到具有9月30日他们之间的所有条目start
和end
)
有点像findDateisBetweenStartAndEnd(Date date)
?
我希望在单页中获得所有结果,我已尝试过
Pageable p = new PageRequest(1, Integer.MAX_VALUE);
return customerRepository.findAll(p);
Run Code Online (Sandbox Code Playgroud)
以上是行不通的,有没有办法实现这个?似乎无法通过此处询问的自定义查询实现.
我有两个实体,一个用户和一个注册用户.
注册用户具有user类型的字段.我想在Spring注册用户实体的数据存储库中有一个方法,通过连接到注册用户的用户的用户名搜索所有注册用户.
因此,这是具有关联用户字段的注册用户实体:
@Entity
public class RegisteredUser implements Serializable {
...
@OneToOne
@JoinColumn(name = "USERNAME_FK")
private User user;
...
}
Run Code Online (Sandbox Code Playgroud)
这是一个用户名:
@Entity
public class User implements Serializable {
...
@Id
@Column(nullable = false)
protected String username;
...
}
Run Code Online (Sandbox Code Playgroud) 任务:app:checkDebugAarMetadata 失败
任务“:app:checkDebugAarMetadata”执行失败。
无法解析配置“:app:debugRuntimeClasspath”的所有文件。找不到 com.example.newsapplication:egUsePieRotation:。需要者:项目:应用程序
可能的解决方案:
android repository gradle android-studio spring-repositories
我在 Spring Repo 中有这样的东西:
findTop10ItemsByCategIdInOrderByInsertDateDesc(List ids)
Run Code Online (Sandbox Code Playgroud)
我想要前 10 个项目,其中类别 id 在按插入日期排序的 id 列表中。
另一个类似的查询:
findTop10ItemsByDomainIdAndCategIdInOrderByInsertDateDesc(List ids, @Param Integer domainId)
Run Code Online (Sandbox Code Playgroud)
在这里,我希望域 id 等于给定的参数,并且 categId 位于给定的列表中。
我设法使用 @Query 解决了它,但我想知道上述查询是否有一个单行。
谢谢
编辑
顶部工作正常。最初我有findTop10ItemsByDomainIdAndCategIdOrderByInsertDateDesc
. 现在我想要来自类别 ID 列表的结果。这就是新的要求。
第二次编辑 我的查询用于查找域 id 等于给定参数且类别 id 包含在给定列表中的集合 o 结果。但我发现 HQL 不支持 setMaxResult 类型的东西作为顶部或限制。
@Query("select i from Items i where i.domainId = :domainId and i.categId in :categoryIds order by i.insertDate desc")
Run Code Online (Sandbox Code Playgroud)
这个方法的参数是(@Param("domainid") Integer domainid,List<Integer> categoryIds)
但它接缝我被允许对每个参数使用 @Param 注释或根本不使用 @Param (除了 Pageable 返回;不是我的情况)
我仍然不知道如何实现这个想法:提取前 n 个元素,其中字段 a …
我有很多从关系数据库模式自动创建的JPA实体.
有没有办法生成相应的Spring Data Repositories(Repository接口)?
java spring-data spring-data-jpa spring-boot spring-repositories
在SpringBoot微服务中,我试图为每个mean_of_payment_id选择一个actor的最新记录.为此,请使用mean_of_payment_id上的group by子句为actor_id选择actor内容,其中created_date等于max(created_date)嵌套查询的子集.我正在使用JPQL.下面是表结构和查询.
@Query("select ac from ActorContent ac "
+ "where (ac.actor.uuid=:actorUuid ) and "
+ "ac.createdDate IN ( SELECT MAX(aci.createdDate) "
+ "FROM ActorContent aci WHERE ac.actor.uuid=aci.actor.uuid "
+ "and aci.uuid = ac.uuid group by ac.meanOfPayment.id)"
)
Run Code Online (Sandbox Code Playgroud)
不幸的是,在执行查询后,我得到了所有记录,但我期待的是前三行.MeanOfPayment和Actor是ActorContent的引用表.
我实现这样的分页:
List<Products> products = productRepository.findAllProducts(productsRequest.getInitiatorType(), "ACTIVE",
new PageRequest(page, 100, Sort.Direction.DESC, "insertDate"));
Run Code Online (Sandbox Code Playgroud)
但是如何获得此查询的总大小?只复制这样的查询?
@Query("SELECT t FROM Products t WHERE t.isApproved = true AND t.partnerId = ?1 AND t.categories.status = ?2")
List<OpcClProducts> findAllOpcClProducts(String senderId, String status, Pageable pageable);
@Query("SELECT COUNT(t) FROM Products t WHERE t.isApproved = true AND t.partnerId = ?1 AND t.categories.status = ?2")
long getTotalCount(String senderId, String status);
Run Code Online (Sandbox Code Playgroud)
并调用2个查询:for totalCount和数据?
所以我想要一个“Void-Repository”,通过它可以访问不一定对实体进行操作的存储过程。
@Repository
public interface StoredProceduresRepository extends CrudRepository<Void, Long> {
@Procedure("my_answer_giver")
String getMyAnswer(@Param("input") String input);
}
Run Code Online (Sandbox Code Playgroud)
但这当然行不通,因为CrudRepository
期望Void
成为一个实体。
有没有一种方法可以使用@Procedure
注释而无需创建虚拟实体,或者我是否坚持使用使用EntityManager
通过准备好的语句进行查询的实现类?
因为说实话,这很丑陋:
@Repository
public class StoredProceduresRepository {
@PersistenceContext
EntityManager em;
public String getMyAnswer(String input) {
Query myAnswerGiver = em
.createStoredProcedureQuery("my_answer_giver")
.registerStoredProcedureParameter("input", String.class, ParameterMode.IN)
.setParameter("input", input);
Object result = ((Object[]) myAnswerGiver.getSingleResult())[0];
return (String) result;
}
}
Run Code Online (Sandbox Code Playgroud) java spring stored-procedures spring-data-jpa spring-repositories
我们尝试CrudRepository
在我们的项目中使用 Spring Data为我们的域对象提供持久性。
一开始我选择了 REDIS 作为后端,因为在第一次使用CrudRepository<ExperimentDomainObject, String>
它的实验中,让它运行起来很容易。
当试图将它放入我们的生产代码时,事情变得更加复杂,因为在这里我们的域对象不必使用简单类型作为键,因此存储库是CrudRepository<TestObject, ObjectId>
.
现在我得到了例外:
找不到能够从 [...ObjectId] 类型转换为 [byte[]] 类型的转换器
搜索此异常,此答案导致我对RedisTemplate
配置进行了未受过教育的实验。(对于我的实验,我使用的是 edded-redis)
我的想法是,提供一个RedisTemplate<Object, Object>
而不是RedisTemplate<String, Object>
允许使用Jackson2JsonRedisSerializer
来完成作为 keySerializer 的工作。
仍然,调用testRepository.save(testObject)
失败。
请看我的代码:
为了这个例子的简洁,我有公共字段并省略了导入。如果需要它们(使其成为 MVCE),我会很乐意提供它们。给我留言吧。
依赖:
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-redis'
implementation 'org.springframework.boot:spring-boot-starter-web'
compileOnly 'org.projectlombok:lombok'
annotationProcessor 'org.projectlombok:lombok'
implementation group: 'redis.clients', name: "jedis", version: '2.9.0'
implementation group: 'it.ozimov', name: 'embedded-redis', version: '0.7.2'
}
Run Code Online (Sandbox Code Playgroud)
Redis配置:
@Configuration
@EnableRedisRepositories
public class RedisConfiguration {
@Bean
JedisConnectionFactory jedisConnectionFactory() …
Run Code Online (Sandbox Code Playgroud) java spring spring-data spring-data-redis spring-repositories
java ×7
spring ×6
spring-data ×4
android ×1
gradle ×1
hibernate ×1
jpa ×1
jpql ×1
pagination ×1
repository ×1
spring-boot ×1