Tag: spring data

Spring Data JPA + JpaSpecificationExecutor + EntityGraph

(使用Spring Data JPA)我有两个实体Parent & Child ,它们之间具有OneToMany / ManyToOne双向关系。 我将@NamedEntityGraph添加到父实体,如下所示: @Entity @NamedEntityGraph(name = “Parent.Offspring”, attributeNodes = @NamedAttributeNodes(“children”)) public class Parent{ //blah blah blah @OneToMany(mappedBy = “parent”, fetch = FetchType.LAZY) Set children; //blah blah blah } 请注意,Parent的子项的获取类型是LAZY。 这是故意的。 当我查询个别父母时,我并不总是希望加载孩子。 通常我可以使用我的命名实体图来按需加载孩子,可以这么说。 但….. 有一种特殊的情况,我想查询一个或多个父母,并急切地加载他们的孩子。 除此之外,我还需要能够以编程方式构建此查询。 Spring Data提供了JpaSpecificationExecutor ,它允许我们构建动态查询,但在这种特定情况下,我无法弄清楚如何将它与实体图一起用于急切加载子项。 这有可能吗? 有没有其他方法可以使用规范急切加载到许多实体?

如何在Spring Data MongoDB中仅返回查询的特定字段?

我们如何在Spring Data Mongo中选择特定字段。 我尝试了以下但是我从Foo到String得到了exception。 使用@Query @Query(value=”{path : ?0}”, fields=”{path : 0}”) String findPathByPath(String path); 非@Query String findPathByPath(String path); 这是文档模型 @Document(collection = “foo”) public class Foo { String name, path; … }

Spring数据CrudRepository和悲观锁

我正在使用 Spring Boot 1.4.2 Spring Data JPA 1.10.5 PostgreSQL 9.5数据库 我希望在我的Spring Data存储库中有一个带悲观锁的findOne方法,该方法与已经提供的findOne方法分开。 根据这个答案,我写道: public interface RegistrationRepository extends CrudRepository { @Lock(LockModeType.PESSIMISTIC_WRITE) @Query(“select r from Registration r where r.id = ?1”) Registration findOnePessimistic(Long id); } 这几乎可行。 不幸的是,这不会刷新实体管理器缓存中我的实体的先前实例。 我有两个并发请求更新我的注册状态 第二个等待第一个事务的提交 第二个没有考虑第一个的变化。 因此破坏了行为。 任何线索为什么@Lock没有开箱即用刷新实体管理器? 更新 这是请求的示例代码: public interface RegistrationRepository extends CrudRepository { @Lock(LockModeType.PESSIMISTIC_WRITE) @Query(“select r from registration_table r where […]

Spring Data仅使用Date查询DateTime

我有这个数据模型 public class CustomerModel{ @Column @Type(type=”org.joda.time.contrib.hibernate.PersistentDateTime”) private DateTime membershipDate; //Other properties and getters } 以下回购 public interface CustomerRepo extends Repository{} 我想做的是。 检索给定日期的所有用户,例如(2013年8月1日加入的成员)但问题是在我的数据库中,membershipDate有时间。 如何忽略时间并检索给定日期的所有用户?

如何使用Spring Data Rest和PagingAndSortingRepository处理exception?

假设我有一个类似的存储库: public interface MyRepository extends PagingAndSortingRepository { @Query(“….”) Page findByCustomField(@Param(“customField”) String customField, Pageable pageable); } 这很好用。 但是,如果客户端发送已形成的请求(例如,搜索不存在的字段),则Spring将exception作为JSON返回。 揭示@Query等 // This is OK http://example.com/data-rest/search/findByCustomField?customField=ABC // This is also OK because “secondField” is a valid column and is mapped via the Query http://example.com/data-rest/search/findByCustomField?customField=ABC&sort=secondField // This throws an exception and sends the exception to the client http://example.com/data-rest/search/findByCustomField?customField=ABC&sort=blahblah 抛出exception并发送给客户端的示例: […]

Spring MongoRepository正在更新或插入而不是插入

我用的是: org.springframework.data.mongodb.repository.MongoRepository 我从一个空DB开始,例如用_id = 1234创建一个对象,然后将一些其他String字段设置为hello ,然后执行: repository.save(object); 一切都很好,它将文档保存在MondoDB中。 我创建一个NEW对象,设置相同的_id = 1234但将另一个String字段设置为world ,然后设置为另一个save: repository.save(newObject); 结果:保存工作但更新原始对象。 预期结果:这应该因DuplicateKeyException而失败,因为_id是唯一的,并且我在执行每次保存时使用2个单独的对象。 spring的缺陷还是我做错了什么?

使用SpringData创建只读存储库

是否可以使用Spring Data创建只读存储库? 我有一些实体链接到视图和一些子实体,我想为其提供一些存储库,其中包含一些方法,如findAll() , findOne()和一些带有@Query注释的方法。 我想避免提供像save(…)和delete(…)因为它们毫无意义并且可能会产生错误。 public interface ContactRepository extends JpaRepository, JpaSpecificationExecutor { List findContactByAddress_CityModel_Id(Integer cityId); List findContactByAddress_CityModel_Region_Id(Integer regionId); // … methods using @Query // no need to save/flush/delete } 谢谢!

在mongo集合上使用时,Spring数据版本注释不会递增

我正在使用带有mongodb的spring数据来存储二进制数据,例如图像等。我想维护一个版本字段,以附加到url以欺骗浏览器缓存图像。 请参阅下面的文档基类: import org.springframework.data.annotation.Id; import org.springframework.data.annotation.Version; import org.springframework.data.mongodb.core.index.Indexed; public abstract class BaseDocument { @Id @Indexed(unique=true) protected long id; protected byte[] data; protected String mimeType; protected String filename; protected String extension; @Version private Long version; 我还有一个包装MongoOperations的存储库来保存我的文档。 import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; @Repository public class DocumentRepository implements IDocumentRepository { @Autowired private MongoOperations mongoTemplate; @Override public void saveDocument(D document) […]

按日期排序ASC与Spring数据

我尝试在ASC上按顺序在表上创建一个Spring-Data-JPA应用程序,但它给了我一个错误: Invalid derived query! No property asc found for type java.util.Calendar 为什么? List findAllOrderByDateAsc(); 要么 @Query(“SELECT * FROM foo ORDER BY date ASC”) List findAllOrderByDateAsc();

将@EmbeddedId与JpaRepository一起使用

我有一个简单的Entitly类,带有@EmbeddedId (单独的类中的Integer和String字段)。 我使用Spring Data( org.springframework.data.jpa.repository.JpaRepository )访问数据库(MySql),正常的Id查询工作正常,由Spring生成和我自己编写的。 使用EmbeddedId我没有设法创建正确的查询。 我想要做的是选择所有的id(其中某个条件发生的embeddedId的一个字段)在这里你有一些代码示例,也许有人会知道如何解决它。 实体类: @Entity @Table(name=”table_name”) public class EntityClass { @EmbeddedId private EmbeddedIdClass id; private String someField; //rest of implemetation } EmbeddedId类: @Embeddable public class EmbeddedIdClass implements Serializable { public EmbeddedIdClass(Long id, String language) { super(); this.id = id; this.language = language; } public UserAdTextId() {} @Column(name=”ad_id”, nullable=false) private Integer […]