我需要对存储在面向文档的数据库(MongoDB)中的(简单)Java对象图进行版本控制.对于关系数据库和Hibernate,我发现了Envers并且对这些可能性感到非常惊讶.是否有类似的东西可以用于Spring Data Documents?
我发现这篇文章概述了我对存储对象版本的想法(以及更多......),我当前的实现类似,因为它将对象的副本存储在带有时间戳的单独历史记录集合中,但我想改进这一点以节省存储空间.因此,我认为我需要在对象树上实现"diff"操作,并且需要"merge"操作来重建旧对象.有没有图书馆帮助这个?
编辑:任何MongoDB和版本的体验高度赞赏!我看到很可能没有Spring Data解决方案.
我正在使用Spring-Data for MongoDB:
版本信息 - org.mongodb.mongo-java-driver版本2.10.1,org.springframework.data.spring-data-mongodb版本1.2.1.RELEASE.
我的案例与此处定义的案例类似,(对不起格式化......):
我刚开始使用spring-data-mongodb开发一些Java应用程序,并遇到了一些我无法解决的问题:
我有几个像这样的文档bean:
@Document(collection="myBeanBar")
public class BarImpl implements Bar {
String id;
Foo foo;
// More fields and methods ...
}
@Document
public class FooImpl implements Foo {
String id;
String someField;
// some more fields and methods ...
}
Run Code Online (Sandbox Code Playgroud)
我有一个存储库类,其方法只是调用类似于此的查找:
public List<? extends Bar> findByFooField(final String fieldValue) {
Query query = Query.query(Criteria.where("foo.someField").is(fieldValue));
return getMongoOperations().find(query, BarImpl.class);
}
Run Code Online (Sandbox Code Playgroud)
保存一个Bar可以正常工作,它会将它保存在mongo中,同时保存为Foo和Bar的"_class"属性.但是,通过Foo中的某些属性查找会抛出这样的异常:
Exception in thread "main" java.lang.IllegalArgumentException: No
property someField found on test.Foo!
at org.springframework.data.mapping.context.AbstractMappingContext.getPersistentPropertyPath(AbstractMappingContext.java:225)
at …Run Code Online (Sandbox Code Playgroud) 这是我当前的PageableResolver:
@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
PageableArgumentResolver resolver = new PageableArgumentResolver();
resolver.setFallbackPageable(new PageRequest(1, 5));
argumentResolvers.add(new ServletWebArgumentResolverAdapter(resolver));
}
Run Code Online (Sandbox Code Playgroud)
但是不推荐使用PageableArgumentResolver,并链接到PageableHandlerMethodArgumentResolver,我想我们可以在没有适配器的情况下注册,如下所示:
@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
PageableHandlerMethodArgumentResolver resolver = new PageableHandlerMethodArgumentResolver();
resolver.setFallbackPageable(new PageRequest(0, 5));
argumentResolvers.add(resolver);
}
Run Code Online (Sandbox Code Playgroud)
首先,新类PageableHandlerMethodArgumentResolver的实现有点不同,因为new PageRequest(1, 5)引用2.页面所以我必须使用new PageRequest(0, 5)它才能显示第一页.
但我仍然有这个新对象的问题.当我调用没有参数的URL(page.page和page.size)时,首次加载分页栏是完美的.然后,当我想移动分页栏时,我们使用2个参数调用相同的URL(例如,page.page = 3&page.size = 5),仍然显示第一页.我认为这个新的解析器需要的不仅仅是这两个参数,因此它不会激活回退条件.您是否知道如何使用此PageableHandlerMethodArgumentResolver?
我有一个标准的Spring数据JPA和Spring数据Rest设置,它正确地返回关联作为正确资源的链接.
{
"id": 1,
"version": 2,
"date": "2011-11-22",
"description": "XPTO",
"_links": {
"self": {
"href": "http://localhost:8000/api/domain/1"
},
"otherDomain": {
"href": "http://localhost:8000/api/domain/1/otherDomain"
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是在某些请求中,我希望扩展与"otherDomain"的关联(因此客户端不必执行N + 1个请求来获取完整数据).
是否可以配置Spring Data Rest以这种方式处理响应?
JpaSpecificationExecutor附带的方法是不够的,它们都没有给我我想要的东西:
Page<T> findAll(Specification<T> spec, Pageable pageable)
List<T> findAll(Specification<T> spec)
List<T> findAll(Specification<T> spec, Sort sort)
Run Code Online (Sandbox Code Playgroud)
第一种方法执行分页查询和计数查询.接下来的2个根本不执行分页.我需要的是以下之一:
Slice<T> findAll(Specification<T> spec, Pageable pageable)
List<T> findAll(Specification<T> spec, Pageable pageable)
Run Code Online (Sandbox Code Playgroud)
通过不扩展JpaSpecificationExecutor,我能够执行两个查询,但计数查询也是如此.在我的情况下,必须避免计数查询,因为它非常昂贵.问题是如何?
我在pom.xml中有一个带有Spring Data Elasticsearch插件的Spring Boot应用程序.我创建了一个文档类,我想索引:
@Document(indexName = "operations", type = "operation")
public class OperationDocument {
@Id
private Long id;
@Field(
type = FieldType.String,
index = FieldIndex.analyzed,
searchAnalyzer = "standard",
indexAnalyzer = "standard",
store = true
)
private String operationName;
@Field(
type = FieldType.Date,
index = FieldIndex.not_analyzed,
store = true,
format = DateFormat.custom, pattern = "dd.MM.yyyy hh:mm"
)
private Date dateUp;
@Field(
type = FieldType.String,
index = FieldIndex.not_analyzed,
store = false
)
private String someTransientData;
@Field(type = FieldType.Nested)
private List<Sector> sectors;
//Getter …Run Code Online (Sandbox Code Playgroud) 我有一个简单的REST服务,可以使用Spring启动访问数据CrudRepository.
这个存储库已经实现了这样的分页和排序功能:
public interface FlightRepository extends CrudRepository<Flight, Long> {
List<Flight> findAll(Pageable pageable);
}
Run Code Online (Sandbox Code Playgroud)
打电话给:
Sort sort = new Sort(direction, ordering);
PageRequest page = new PageRequest(xoffset, xbase, sort);
return flightRepo.findAll(page);
Run Code Online (Sandbox Code Playgroud)
我想添加过滤到这个存储库(例如只返回实体id > 13 AND id < 27).CrudRepository似乎不支持此功能.有没有办法如何实现这一点,还是我需要使用不同的方法?
谢谢你的任何提示!
我使用spring-boot-starter-jdbc(v1.3.0)编写应用程序.
我遇到的问题:BeanPropertyRowMapper失败的实例,因为它无法转换java.sql.Timestamp为java.time.LocalDateTime.
为了复制这个问题,我实现
org.springframework.core.convert.converter.Converter了这些类型.
public class TimeStampToLocalDateTimeConverter implements Converter<Timestamp, LocalDateTime> {
@Override
public LocalDateTime convert(Timestamp s) {
return s.toLocalDateTime();
}
}
Run Code Online (Sandbox Code Playgroud)
我的问题是:如何让我提供TimeStampToLocalDateTimeConverter的BeanPropertyRowMapper.
更一般的问题是,如何注册我的转换器,以使它们在系统范围内可用?
以下代码将我们带到NullPointerException初始化阶段:
private Set<Converter> getConverters() {
Set<Converter> converters = new HashSet<Converter>();
converters.add(new TimeStampToLocalDateTimeConverter());
converters.add(new LocalDateTimeToTimestampConverter());
return converters;
}
@Bean(name="conversionService")
public ConversionService getConversionService() {
ConversionServiceFactoryBean bean = new ConversionServiceFactoryBean();
bean.setConverters(getConverters());
bean.afterPropertiesSet();
return bean.getObject();
}
Run Code Online (Sandbox Code Playgroud)
谢谢.
我正在尝试使用弹簧数据的新功能,投影来获取部分实体(NetworkSimple)的页面
我已经检查了文档,如果我只是请求:
Collection<NetworkSimple> findAllProjectedBy();
Run Code Online (Sandbox Code Playgroud)
它有效,但如果我使用可分页:
Page<NetworkSimple> findAllProjectedBy(Pageable pageable);
Run Code Online (Sandbox Code Playgroud)
它抛出一个错误:
org.hibernate.jpa.criteria.expression.function.AggregationFunction$COUNT cannot be cast to org.hibernate.jpa.criteria.expression.CompoundSelectionImpl
Run Code Online (Sandbox Code Playgroud)
任何人已经使用过这个吗?
我的NetworkSimple类如下:
public interface NetworkSimple {
Long getId();
String getNetworkName();
Boolean getIsActive();
}
Run Code Online (Sandbox Code Playgroud) 我正在使用Spring JPA,为了向我的实体添加一个String列表,我正在使用@ElementCollection,如下所示.
@ElementCollection
private Map<Integer, String> categories;
Run Code Online (Sandbox Code Playgroud)
当我使用它时,它会生成一个名为subscription_categoriesthis 的表,其中包含以下列subscription(varchar), catergories(varchar) and caterogies_key (int)
如果我在桌面上使用我的SQL工具,我可以使用以下内容查询此表
select `subscription_categories`.`subscription` from `subscription_categories` where `subscription_categories`.`categories`='TESTING';
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试在Spring Data中使用它时,它会因"... not mapped"错误而失败
以下是一些尝试:
@Query("select s.subscription from subscription_categories s where s.categories = ?1")
List<Subscription> findUsernameByCategory(String category);
@Query("select s.subscription from categories s where s.categories = ?1")
List<Subscription> findUsernameByCategory(String category);
Run Code Online (Sandbox Code Playgroud)
两者都返回相同的错误.
引起:org.hibernate.hql.internal.ast.QuerySyntaxException:未映射类别
我的问题是:
如何查询@ElementCollection创建的表?
spring-data ×10
java ×7
spring ×6
hibernate ×2
jpa ×2
mongodb ×2
rest ×2
spring-boot ×2
diff ×1
spring-mvc ×1
sql ×1
versioning ×1