Tag: spring data mongodb

如何将最终字段添加到现有的spring-data-mongodb文档集合中?

我有一个使用spring-data-mongodb版本1.0.2.RELEASE的现有文档集合。 @Document public class Snapshot { @Id private final long id; private final String description; private final boolean active; @PersistenceConstructor public Snapshot(long id, String description, boolean active) { this.id = id; this.description = description; this.active = active; } } 我正在尝试添加一个新属性private final boolean billable; 。 由于属性是final因此需要在构造函数中设置它们。 如果我将新属性添加到构造函数,则应用程序将无法再读取现有文档。 org.springframework.data.mapping.model.MappingInstantiationException: Could not instantiate bean class [com.some.package.Snapshot]: Illegal arguments […]

使用java.util.Currency的Mongo spring-data问题

获取错误“在实体类java.util.Currency上找不到属性null” 嗨,我有文档类 @Document @JsonInclude(Include.NON_NULL) public class Course { @Id private String id; private String title; private Currency curr; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public Currency getCurr() { return […]

如何在spring-data-mongodb框架中将BigDecimal转换为Double

Spring Data MongoDB映射默认情况下将BigDecimal转换为String。 但是,我希望它们在mongodb中转换为Double。 这是后者在mongodb(比较查询/聚合查询)中对此字段进行查询所必需的。 我如何重新编译自己的转换器(BigDecimalToDouble / DoubleToBigDecimal)来执行此操作?

spring-data-mongodb在对象再水化时如何处理构造函数?

我已阅读http://static.springsource.org/spring-data/data-mongo/docs/1.1.0.RELEASE/reference/html/#mapping-chapter但无法找到以下基本弹簧数据的答案 – mongodb对象映射问题: 如果我从MongoDB加载以下类的实例: public class Test { private String str1; private String str2; private Date date3; public Test(String str1) { this.str1 = str1; this.date3=new Date(); } } 我理解构造函数Test(String str1)将使用MongoDB文档的顶级字段str1中找到的值进行调用。 我假设这个构造函数相当于显式声明@PersistenceConstructor 。 但是在这种情况下str2, date3字段str2, date3会发生什么? 是否仍会初始化所有不属于构造函数的字段,或者是否会丢失str2, date3值,因为只找到了使用str1的PeristenceConstructor? 最后,这会以什么顺序发生? date3是由构造函数设置的,然后被先前持久化的字段覆盖,反之亦然?

如何使用Spring Data MongoDB进行乐观锁定?

我正在阅读Spring Data MongoDB – 参考文档 ,我发现这些示例有点过于简单化了。 特别是我试图了解如何处理并发环境中的陈旧数据。 例如,假设我有以下实体: public class Person { private final String username; private final String firstname; private final String lastname; […] } 现在,如果我使用CrudRepository来保存/更新/删除我的实体,那么想象一下这样一个场景,其中两个线程检索同一个实体,其中一个删除它,另一个更新其lastname字段。 如果delete调用在save调用之前完成,那么当预期行为导致save操作失败时,我的实体将被删除然后重新创建。 到目前为止,我已经看到两种解决此问题的方法: 使用@Version注释。 找不到任何文档说Spring Data支持使用版本化文档在MongoDB中进行乐观锁定。 如果有人能指出我的链接,我们将不胜感激。 使用MongoOperations.findAndModify ,如果返回null失败。 这种方法的缺点是我不能再实现我的存储库API以获得“获取实体”和“保存更新的实体”类型的语义。 例如: User user = userRepository.findByUsername(“uniqueUsername”); user.setLastName(“Archer”); userRepository.update(user); 我想将其中的一部分推送到存储库我猜: userRepository.updateLastname(“uniqueUsername”, “Archer”); 但是我有点担心如果我想要做的每种类型的更新都需要一个新方法,我的存储库接口将无法控制地增长。 我意识到我还没有提出过一个问题,所以这就是:使用Spring Data for MongoDB设计应用程序的最佳实践是什么?

spring数据mongodb映射动态场

我在我的java类中有这个模型 @Document public class Template { private String type; private String code; @Version Long version; } 我需要添加一个名为template的新字段,并将此字段映射为动态,换句话说,我会像这样建模一个文档 { _id: ‘id’ type:’myType’, code:’myCode’ template:{ someFiled:[ { subField1:’value1′, subField2:’value2′ }, { sub1Field1:’1value1′, sub1Field2:’1value2′ } ………………….. ], otherField:[ { otherField1:’value1′, otherField2:’value2′ } ], ……… }, version:1000L } 有没有办法将字段注释为动态? 解 @Document public class Template { private String type; private […]

如何从外部实用程序jar正确加载和配置Spring bean

目前我有一个包含许多数据存储区服务的实用程序jar。 在幕后,这些数据存储区服务使用Spring Data MongoDB,并且所有内容都使用实用程序jar中的app-context.xml文件进行配置。 我希望这个实用程序jar能够更改后备存储,而无需更改使用此实用程序jar的任何内容。 现在,我想创建一个使用此实用程序jar中的数据存储区服务的spring mvc Web应用程序。 我如何设置它,以便spring mvc web应用程序(或任何其他jar)可以轻松使用数据存储区服务,而无需了解实用程序jar,但仍然可以正确加载实用程序jar中的bean? 我正在考虑向实用程序jar中添加一个新的java bean类,它将app-context加载到它自己的jar中,然后为服务设置一些属性。 然后spring mvc将在我的实用程序jar中使用这个新类创建一个bean,并通过这个bean引用服务。 /** * This bean would exist in the utility jar, and other jars/webapps would * create a new instance of this bean. */ public class Services { private MyAService myAService; private MyBService myBService; public Services() { ClassPathXmlApplicationContext ctx = new […]

Spring Data MongoDB:当Document嵌入另一个时,如何忽略唯一索引字段?

我有一个像这样定义的Contract类: @Document public class Contract { @Id private String id; @Indexed(unique = true) private String ref; private String status = “pending”; // getter & setter & hashcode & equals & tostring… } 我希望随着时间的推移保存合同状态,所以我创建了一个这样的Version类: @Document public class Version { @Id private String id; private Contract contract; private Instant createdAt; // getter & setter & hashcode & […]

Text Search无法使用Spring Boot MongoDB

我正在开发Spring Boot + MongoDB + spring data mongo + Text search示例。 通过链接的参考: https : //spring.io/blog/2014/07/17/text-search-your-documents-with-spring-data-mongodb ,我开发了我的代码,但当我执行时,它给出了空结果集。 请帮助这个场景。 我期待得到两个results OrderId = 10248 and 10249 ,但我得空了。 我开发的代码:OrderDetails.java @Document(collection=”order-details”) public class OrderDetails { @Id private ObjectId id; //@TextIndexed(weight=2) @Field(“OrderID”) private Integer orderID; @Field(“ProductID”) private Integer productID; @Field(“UnitPrice”) private Double unitPrice; @Field(“Quantity”) private Integer quantity; @Field(“Discount”) private Integer discount; […]

如何在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; … }