我目前正在构建一个REST API,我希望客户端可以轻松地过滤特定实体的大多数属性.使用QueryDSL与结合春季数据REST(由奥利弗·基尔克一个例子),让我很容易地通过允许客户通过组合是指性质(如查询参数进行过滤得到我想要的东西90% /users?firstName=Dennis&lastName=Laumen).
我甚至可以通过实现QuerydslBinderCustomizer接口来自定义查询参数和实体属性之间的映射(例如,用于不区分大小写的搜索或部分字符串匹配).这一切都很棒,但我也希望客户能够使用范围过滤某些类型.例如关于像出生日期这样的财产,我想做类似下面的事情,/users?dateOfBirthFrom=1981-1-1&dateOfBirthTo=1981-12-31.基于数字的属性也是如此/users?idFrom=100&idTo=200.我觉得这应该可以使用QuerydslBinderCustomizer界面,但这两个库之间的集成没有得到非常广泛的记录.
总结一下,这可能使用Spring Data REST和QueryDSL吗?如果是这样,怎么样?
spring querydsl spring-data spring-data-jpa spring-data-rest
我正在尝试迁移该应用程序.我正在从Hibernate工作到Spring Data Jpa.
虽然spring数据jpa提供了简单的查询构建方法,但我仍然坚持创建使用And和的查询方法Or operator.
MethodName - findByPlan_PlanTypeInAndSetupStepIsNullOrStepupStepIs(...)
当它转换为查询时,前两个表达式被组合并执行为[(exp1 and exp2) or (exp3)].
而要求是](exp1) and (exp2 or exp3)].
任何人都可以告诉我,如果这是可以实现的 Spring data jpa?
我有一个LocalContainerEntityManagerFactoryBean为EntityManager实例.
要快速删除整个表的内容,我想运行以下代码:
@Service
public class DatabaseService {
@Autowired
private EntityManager em;
@Transactional
public void clear() {
em.createNativeQuery("TRUNCATE TABLE MyTable").executeUpdate();
}
}
Run Code Online (Sandbox Code Playgroud)
结果:
ERROR org.springframework.integration.handler.LoggingHandler: javax.persistence.TransactionRequiredException: Executing an update/delete query
at org.hibernate.jpa.spi.AbstractQueryImpl.executeUpdate(AbstractQueryImpl.java:71)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:708)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:98)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:262)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:95)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:644)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:65)
at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471)
at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:304)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:178)
at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:744)
Run Code Online (Sandbox Code Playgroud)
如果我做了这个改变:
public …Run Code Online (Sandbox Code Playgroud) TL; DR:如何使用Spring Data JPA中的规范复制JPQL Join-Fetch操作?
我正在尝试构建一个类,它将使用Spring Data JPA处理JPA实体的动态查询构建.为此,我定义了许多创建Predicate对象的方法(例如在Spring Data JPA文档和其他地方建议的),然后在提交适当的查询参数时将它们链接起来.我的一些实体与帮助描述它们的其他实体具有一对多的关系,这些实体在查询时被急切地获取并且合并到用于DTO创建的集合或地图中.一个简化的例子:
@Entity
public class Gene {
@Id
@Column(name="entrez_gene_id")
privateLong id;
@Column(name="gene_symbol")
private String symbol;
@Column(name="species")
private String species;
@OneToMany(mappedBy="gene", fetch=FetchType.EAGER)
private Set<GeneSymbolAlias> aliases;
@OneToMany(mappedBy="gene", fetch=FetchType.EAGER)
private Set<GeneAttributes> attributes;
// etc...
}
@Entity
public class GeneSymbolAlias {
@Id
@Column(name = "alias_id")
private Long id;
@Column(name="gene_symbol")
private String symbol;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name="entrez_gene_id")
private Gene gene;
// etc...
}
Run Code Online (Sandbox Code Playgroud)
查询字符串参数作为键值对从Controller类传递到Service类,在那里它们被处理并组装成Predicates:
@Service
public class GeneService …Run Code Online (Sandbox Code Playgroud) 我正在编写一个代码生成工具来使用Spring-Data-Jpa为Spring启动应用程序生成后端连接代码,而且我很温和地告诉我CrudRepository中的方法返回Iterable而不是List,因为iterable不能提供足够的功能,但List,所以我正在寻找将iterable转换为列表的最佳方法.
我看到这篇关于将迭代更改为集合的帖子我想知道,为什么不将它转换为List,而不是使用像Guava这样的库或实现我自己的函数来进行转换?这样做我不知道有什么问题吗?
编辑:我问,因为它是一个代码生成工具,使它生成引入第三方库依赖的代码是不合理的,编写我自己的函数来进行转换也不合理,因为它必须住在某个地方而且我宁愿在生成的代码中没有它.一个简单的演员会有用,如果有点难看,但只是想知道是否有我遗失的东西?
我有这两个简单的实体Something和Property.该Something实体与多对一关系Property,因此当我创建一个新Something行时,我分配一个现有的Property.
东西:
@Entity
@Table(name = "something")
public class Something implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(name = "name")
private String name;
@Column(name = "owner")
private String owner;
@ManyToOne
private Property property;
// getters and setters
@Override
public String toString() {
return "Something{" +
"id=" + getId() +
", name='" + getName() + "'" +
", owner='" + …Run Code Online (Sandbox Code Playgroud) 是否可以使用Spring Data创建只读存储库?
我有一些实体链接到视图和一些子实体,我想为其提供一些存储库,其中包含一些方法findAll(),findOne()以及一些带有@Query注释的方法.我想避免提供类似的方法save(…),delete(…)因为它们没有任何意义,可能会产生错误.
public interface ContactRepository extends JpaRepository<ContactModel, Integer>, JpaSpecificationExecutor<ContactModel> {
List<ContactModel> findContactByAddress_CityModel_Id(Integer cityId);
List<ContactModel> findContactByAddress_CityModel_Region_Id(Integer regionId);
// ... methods using @Query
// no need to save/flush/delete
}
Run Code Online (Sandbox Code Playgroud)
谢谢!
我以一个例子提出这个问题.
断言我们有一个Repository,如下所示:
public interface ExampleObjectRepository extends CrudRepository<ExampleObject, Long> {
}
Run Code Online (Sandbox Code Playgroud)
通过扩展JpaRepository接口,ExampleObject存储库继承以下方法:
T findOne(ID id);
Run Code Online (Sandbox Code Playgroud)
现在,我观察到,如果在调用此方法后收到对ExampleObject的引用,我对此方法所做的任何操作都会自动保存到数据库中,例如:
ExampleObject pointInCase = exampleObjectRepository.findOne(1L);
pointInCase.setName("Something else");
Run Code Online (Sandbox Code Playgroud)
阅读这个主题,我明白这个ExampleObject实例就是这个标志not detached.
这违背了我的期望.我原以为我需要使用从中继承的save方法CrudRepository来保存更改:
T save(T entity);
Run Code Online (Sandbox Code Playgroud)
是否有人能够确认从Spring Data JPA Repository返回的对象仍然作为标准附加,并解释如何使用API在存储库中标记方法,使其仅返回分离的引用?
我想,改变实体的状态也可能在与所述save(T entity)方法一起使用时改变其定义,因此我也理解如何处理更新的身份.
我有一个实体类如下:
@Entity
public class UserDemo implements Serializable {
@Id
private Long id;
private String username;
private String createdBy;
@Version
private int version;
/***
*
* Getters and setters
*/
}
Run Code Online (Sandbox Code Playgroud)
使用Spring Data JPA和Querydsl如何获取仅包含和添加属性的UserDemo页面?我需要使用分页和搜索.总之,我希望得到与之相同的结果idusername
Page<UserDemo> findAll(Predicate predicate, Pageable pageable);
Run Code Online (Sandbox Code Playgroud)
但填充了UserDemo的有限字段.
我正在尝试做类似下面的事情。
@Entity
@Table(name="Sample")
public record Sample(Integer id, String name) {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name="user_id")
private Integer id;
@Column(name="username")
private String name;
}
Run Code Online (Sandbox Code Playgroud)
但是,它给我错误“记录中不允许用户声明的非静态字段 id”,名称字段也相同。
有没有办法将新的java功能“记录”与JPA注释一起使用?
spring-data-jpa ×10
java ×7
jpa ×6
spring ×6
spring-data ×4
querydsl ×2
spring-boot ×2
criteria-api ×1
hibernate ×1
java-record ×1
persistence ×1