我正在使用Spring Data的Querydsl集成来使用谓词执行我的查询.
findAll(predicate, pageable)
Run Code Online (Sandbox Code Playgroud)
有没有办法转储执行的实际原始查询/命令?
我也查看了这个问题的答案,它对我不起作用. 配置MongoDB Java驱动程序的日志记录
- 更新 - 我设法通过在application.properties(而不是log4j.properties)中添加logging.level.org.mongodb.driver = DEBUG来使日志工作正常工作
但是,我仍然无法看到正在执行的原始查询:
2016-03-23 21:50:56 DEBUG查询:56 - 查询完成2016-03-23 21:50:56 DEBUG查询:56 - 在连接上发送命名空间testdb.reservation的查询[connectionId {localValue:4,serverValue: 42631}]到服务器ds046785.mongolab.com:39186
我试图"比较"2个集合之间的所有文档,这将仅返回true,如果只有2个集合中的所有文档完全相同.
我一直在搜索集合上的方法,但找不到可以做到这一点的方法.
我在mongo shell中尝试了类似这样的东西,但没有像我预期的那样工作:
db.test1 == db.test2
Run Code Online (Sandbox Code Playgroud)
要么
db.test1.to_json() == db.test2.to_json()
Run Code Online (Sandbox Code Playgroud)
无论如何,我还在java中使用spring-data mongodb.
请分享你的想法!谢谢.
所有Spring配置都已正确编写.非Multi-Exec Redis操作完美运行.
@Autowired
@Qualifier("stringRedisTemplate")
StringRedisTemplate template;
void test(){
template.multi();
template.boundValueOps("somevkey").increment(1);
template.boundZSetOps("somezkey").add("zvalue", timestamp);
template.exec();
}
Run Code Online (Sandbox Code Playgroud)
通过Junit Test运行上面的代码后,抛出了异常.
org.springframework.data.redis.RedisSystemException: Unknown exception; nested exception is org.springframework.data.redis.RedisSystemException: Unknown jedis exception; nested exception is java.lang.NullPointerException
at org.springframework.data.redis.connection.jedis.JedisUtils.convertJedisAccessException(JedisUtils.java:93)
at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.translateExceptionIfPossible(JedisConnectionFactory.java:155)
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:58)
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:163)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)
at $Proxy66.appendUserStream(Unknown Source)
at com.uniu.test.repository.StreamCacheRepositoryTest.testAppendUserStream(StreamCacheRepositoryTest.java:23)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193) …Run Code Online (Sandbox Code Playgroud) 根据其JavaDoc,PersistenceAnnotationBeanPostProcessor似乎负责使用注释@PersistenceContext注入EntityManager.它似乎暗示如果没有在Spring应用程序上下文xml中声明这个bean,@ PerersContext注释将不起作用.
但是,根据我的实验,这不是事实.
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd"
version="1.0">
<persistence-unit name="default" transaction-type="RESOURCE_LOCAL" />
</persistence>
Run Code Online (Sandbox Code Playgroud)
<context:component-scan base-package="com.test.dao" />
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="persistenceUnitName" value="default"/>
<property name="jpaVendorAdapter">
<bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true"/>
<property name="generateDdl" value="true"/>
<property name="databasePlatform" value="org.hibernate.dialect.DerbyDialect"/>
</bean>
</property>
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.apache.derby.jdbc.ClientDriver"/>
<property name="url" value="jdbc:derby://localhost:1527/c:\derbydb\mydb"/>
<property name="username" value="APP"/>
<property name="password" value="APP"/>
</bean>
<tx:annotation-driven/>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory" />
</bean>
<!--
<bean id="persistenceAnnotation" class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor" />
-->
Run Code Online (Sandbox Code Playgroud)
@Repository("userDao")
public class UserDaoImpl …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用注释和spring-security为我的开源项目添加方法级安全性.我现在面临的问题是找到所有方法,特别是用于分页的方法(例如,返回页面).
使用@PostFilter可以在列表中使用(但我个人认为在应用程序而不是数据库中进行过滤不是一个好主意)但是在分页查询中完全失败.
这是有问题的,因为我有一个Entity包含List<Compound>.复合有不同的实现,用户可能只有权读取其中一个化合物.Compound使用TABLE_PER_CLASS继承.存储库实现QueryDslPredicateExecutor.
我的想法是为每个查询添加一个谓词,根据当前用户限制返回结果.然而,我有点迷失在a)用户和角色的数据模型应该如何看,以及b)如何创建谓词(一旦定义了模型,这可能很容易).或者querydsl是否已经提供基于类型的过滤(在查询类中包含的元素)?
我正在使用spring-data-elasticsearch和elasticsearch来查询文档.我想对嵌套文档进行嵌套查询.
我在java中有这个:
@Document(indexName = "as", type = "a", indexStoreType = "memory", shards = 1, replicas = 0, refreshInterval = "-1")
class A {
@Id
private String Id;
@Field(type = String, index = analyzed, store = true)
private String field1;
// ... Many more Fields.
@NestedField(type = FieldType.Object, index = analyzed, store = true, dotSuffix = "accounts")
private List<B> bs;
// ... getters and setters
}
Run Code Online (Sandbox Code Playgroud)
和
class B { // some normal pojo }
Run Code Online (Sandbox Code Playgroud)
当我让spring-data进行映射时,我得到:
"a": {
"properties": {
"bs": …Run Code Online (Sandbox Code Playgroud) 我正在使用Spring Data JpaRepository,我发现它非常易于使用.我实际上需要所有这些功能 - 分页,排序,过滤.不幸的是,有一件令人讨厌的事情似乎迫使我回归使用普通的JPA.
我需要按相关集合的大小排序.比如我有:
@Entity
public class A{
@Id
private long id;
@OneToMany
private List<B> bes;
//boilerplate
}
Run Code Online (Sandbox Code Playgroud)
我不得不排序 bes.size()
有没有办法以某种方式定制订单仍然利用分页,过滤和其他Spring Data的强大功能?
我正在尝试实现细粒度访问控制,同时仍然利用Spring数据休息.
我正在努力确保安全,CrudRepository因此用户只能修改或插入属于他们的数据.我正在使用@PreAuthorize/ @PostAuthorize和@PreFilter/ @PostFilter将访问锁定到当前主体.
到目前为止我的存储库看起来像这样
public interface MyRepository extends CrudRepository<MyObject, Integer> {
@PreAuthorize("#entity.userId == principal.id")
@Override
<S extends MyObject> S save(S entity);
@PreFilter("filterObject.userId === principal.id")
@Override
<S extends MyObject> Iterable<S> save(Iterable<S> entities);
@PostAuthorize("returnObject.userId == principal.id")
@Override
MyObject findOne(Integer integer);
@PostFilter("filterObject.userId == principal.id")
@Override
Iterable<MyObject> findAll();
}
Run Code Online (Sandbox Code Playgroud)
虽然这有点单调乏味,但它似乎完成了我所追求的目标.(如果有人知道更好的方式,请随时告诉我!)
我遇到问题的地方是delete(),count()和exists()
@Override
long count();
@Override
void delete(Integer integer);
@Override
void delete(MyObject entity);
@Override
void deleteAll();
@Override
boolean exists(Integer integer);
Run Code Online (Sandbox Code Playgroud)
这些方法要么采用 …
我在MongoDB中插入了一个init文件:
db.User.insert({ "_id" : ObjectId("5589929b887dc1fdb501cdba"), "_class" : "com.smartinnotec.aposoft.dao.domain.User", "title" : "DI.", ... "address" : { "_id" : null, ... "country" : "Österreich" }})
Run Code Online (Sandbox Code Playgroud)
如果我用db.User.find()调用此条目,那么我得到以下内容:
{ "_id" : ObjectId("5589929b887dc1fdb501cdba"), "_class" : "com.smartinnotec.aposoft.dao.domain.User", "title" : "DI.", ... "address" : { "_id" : null, ... "country" : "�sterreich" } }
Run Code Online (Sandbox Code Playgroud)
带有特殊字符的单词"�sterreich不正确.
有没有人知道我在mongodb可以做些什么才能解决这个问题?
string-formatting utf special-characters mongodb spring-data
我正在使用JPARespository我的所有CRUD操作.最近我想实现排序,所以我继续Pagable.
问题是,我希望存储库方法返回List对象,我在服务层中使用它们.
我怎样才能实现这一点,有没有办法将这些Page对象转换为List?
spring-data ×10
java ×6
spring ×4
mongodb ×3
querydsl ×2
java-ee ×1
jpa ×1
pagination ×1
redis ×1
utf ×1