在Spring CrudRepository中,我们是否支持字段的"IN子句"?即类似于以下内容?
findByInventoryIds(List<Long> inventoryIdList)
Run Code Online (Sandbox Code Playgroud)
如果没有这样的支持,可以考虑哪些优雅的选择?针对每个id的触发查询可能不是最佳的.
目前我有一个使用Spring Data REST的Spring Boot应用程序.我有一个与另一个域实体Post
有@OneToMany
关系的域实体Comment
.这些类的结构如下:
Post.java:
@Entity
public class Post {
@Id
@GeneratedValue
private long id;
private String author;
private String content;
private String title;
@OneToMany
private List<Comment> comments;
// Standard getters and setters...
}
Run Code Online (Sandbox Code Playgroud)
Comment.java:
@Entity
public class Comment {
@Id
@GeneratedValue
private long id;
private String author;
private String content;
@ManyToOne
private Post post;
// Standard getters and setters...
}
Run Code Online (Sandbox Code Playgroud)
他们的Spring Data REST JPA存储库是以下基本实现CrudRepository
:
PostRepository.java:
public interface PostRepository extends CrudRepository<Post, Long> …
Run Code Online (Sandbox Code Playgroud) 我已经在我的项目中使用Spring Data JPA存储库一段时间了,我知道以下几点:
findByCustomerNameAndPhone()
(假设customerName
和phone
是域对象中的字段).我感兴趣的是如何对它进行编码,我已经查看了Spring JPA源代码和API,但我找不到以下问题的答案:
您能否帮助解决上述问题并提供任何支持的文档?
我想编写一个Spring Data JPA存储库接口方法签名,它可以让我找到在该实体中具有嵌入对象属性的实体.有谁知道这是否可能,如果是这样的话怎么样?
这是我的代码:
@Entity
@Table(name = "BOOK_UPDATE_QUEUE", indexes = { uniqueConstraints = @UniqueConstraint(columnNames = {
"bookId", "region" }, name = "UK01_BOOK_UPDATE_QUEUE"))
public class QueuedBook implements Serializable {
@Embedded
@NotNull
private BookId bookId;
...
}
@Embeddable
public class BookId implements Serializable {
@NotNull
@Size(min=1, max=40)
private String bookId;
@NotNull
@Enumerated(EnumType.STRING)
private Region region;
...
}
public interface QueuedBookRepo extends JpaRepository<QueuedBook, Long> {
//I'd like to write a method like this, but can't figure out how to search by region,
//when …
Run Code Online (Sandbox Code Playgroud) @NoRepositoryBean
在阅读Spring Data文档时,我多次遇到界面.
引用文档:
如果您正在使用Spring命名空间使用Spring命名空间进行自动存储库接口检测,那将导致Spring尝试创建MyRepository实例.这当然不是所希望的,因为它只是在Repository和您要为每个实体定义的实际存储库接口之间起中间作用.要将扩展存储库的接口排除在实例化为存储库实例之外,请使用
@NoRepositoryBean
.
但是,我仍然不确定何时何地使用它.有人可以建议并给我一个具体的用法示例吗?
如何实现此代码的等价物:
tx.begin();
Widget w = em.find(Widget.class, 1L, LockModeType.PESSIMISTIC_WRITE);
w.decrementBy(4);
em.flush();
tx.commit();
Run Code Online (Sandbox Code Playgroud)
...但是使用Spring和Spring-Data-JPA注释?
我现有代码的基础是:
@Service
@Transactional(readOnly = true)
public class WidgetServiceImpl implements WidgetService
{
/** The spring-data widget repository which extends CrudRepository<Widget, Long>. */
@Autowired
private WidgetRepository repo;
@Transactional(readOnly = false)
public void updateWidgetStock(Long id, int count)
{
Widget w = this.repo.findOne(id);
w.decrementBy(4);
this.repo.save(w);
}
}
Run Code Online (Sandbox Code Playgroud)
但我不知道如何指定updateWidgetStock
方法中的所有内容都应该使用悲观的锁定集来完成.
有一个Spring Data JPA注释org.springframework.data.jpa.repository.Lock
允许你设置一个LockModeType
,但我不知道将它放在updateWidgetStock
方法上是否有效.这听起来更像是一个注释WidgetRepository
,因为Javadoc说:
org.springframework.data.jpa.repository
@Target(value = METHOD)
@Retention(value = RUNTIME)
@Documented
public @interface Lock …
我正在尝试手动连接Spring Data JPA对象,以便我可以生成DAO代理(也称为存储库) - 而无需使用Spring bean容器.
不可避免地,我会被问到为什么要这样做:这是因为我们的项目已经在使用Google Guice(并且在使用Gin和GWT的UI上),我们不想维护另一个IoC容器配置,或者拉入所有产生的依赖关系.我知道我们可以使用Guice SpringIntegration
,但这是最后的选择.
似乎所有东西都可用于手动连接对象,但由于它没有很好地记录,我现在遇到了困难.
根据Spring Data用户指南,可以单独使用存储库工厂.不幸的是,该示例显示了RepositoryFactorySupport
哪个是抽象类.经过一番搜索,我找到了JpaRepositoryFactory
JpaRepositoryFactory
实际上工作得很好,除了它不会自动创建交易.必须手动管理事务,否则任何内容都不会持久保存到数据库:
entityManager.getTransaction().begin();
repositoryInstance.save(someJpaObject);
entityManager.getTransaction().commit();
Run Code Online (Sandbox Code Playgroud)
问题原来是@Transactional
注释没有自动使用,需要一个帮助TransactionInterceptor
值得庆幸的是,JpaRepositoryFactory
在返回之前,可以进行回调以向生成的Repository代理添加更多AOP建议:
final JpaTransactionManager xactManager = new JpaTransactionManager(emf);
final JpaRepositoryFactory factory = new JpaRepositoryFactory(emf.createEntityManager());
factory.addRepositoryProxyPostProcessor(new RepositoryProxyPostProcessor() {
@Override
public void postProcess(ProxyFactory factory) {
factory.addAdvice(new TransactionInterceptor(xactManager, new AnnotationTransactionAttributeSource()));
}
});
Run Code Online (Sandbox Code Playgroud)
这是事情进展不顺利的地方.单步执行代码中的调试器,TransactionInterceptor
确实创建了一个事务 - 但错误EntityManager
.Spring EntityManager
通过查看当前正在执行的线程来管理活动.这样TransactionInterceptor
做并看到EntityManager
线程没有活动绑定,并决定创建一个新线程.
但是,这个new EntityManager
与创建并传递给JpaRepositoryFactory
构造函数的实例不同,后者需要一个EntityManager
.问题是,我如何制作TransactionInterceptor …
我试图从Spring指南运行示例:构建RESTful Web服务.
如果我打开localhost:8080/greeting,它的效果很好.
但是,如果我打开192.168.1.111:8080/greeting或140.112.134.22:8080/greeting,它就无法建立连接,尽管我的计算机实际上在互联网上使用了这两个IP.
有人可以建议我如何在Spring中配置嵌入式Tomcat以接受其他IP地址上的HTTP请求,除了localhost(即127.0.0.1)?
谢谢!:)
问题是,Spring HATEOAS与Spring Data Rest之间有什么区别?
我觉得两者都可以这样做,Spring Data Rest(作为Spring Data的一部分)看起来更有活力.
https://github.com/spring-projects/spring-hateoas https://github.com/spring-projects/spring-data-rest
你什么时候使用其中一个?
我的团队正在使用功能分支来实现新功能,并不断将快照构建部署到远程仓库中供我们的用户使用.因此,"部署"实际上只意味着"分发到远程Maven存储库".我们目前只为主分支而不是功能分支运行持续集成构建,原因如下:我们使用Maven构建我们的项目并在JAR旁边分发JavaDoc和源代码.
我的计划现在是为每个功能分支构建添加一个分类器,并期望在创建和部署这样的工件时使用一个分类器:
文物:foo-${version}
.jar,foo-${version}-sources
.jar,foo-${version}-javadoc.jar
分支:特征-X
foo-${version}-feature.jar
,foo-${version}-sources-feature.jar
,foo-${version}-javadoc-feature.jar
我并不真正关心工件的确切命名,我只需要为功能分支单独的main,source和JavaDoc工件.事实证明,JavaDoc插件和源插件都没有考虑配置的分类器,因此有效地覆盖了为我的主构建创建的工件.
我真的不想改变artifactId,虽然这可能会解决问题.你如何处理功能分支和与Maven的持续集成?
continuous-integration feature-branch maven maven-javadoc-plugin maven-source-plugin
spring ×9
java ×8
spring-data ×6
jpa ×5
spring-boot ×2
hateoas ×1
java-ee ×1
maven ×1
spring-mvc ×1
tomcat ×1