在Spring数据中使用Mongo实体中的jodatime

时间:2013-03-14 16:24:12

标签: java jodatime querydsl spring-data-mongodb

我有一个将Spring Data持久保存到Mongo数据库的实体:

@Document
public class MyEntity {

    @Id
    private String id;

    @QueryType(PropertyType.DATETIME)
    private DateTime lastUpdate;

}

这是我的存储库:

public interface MyEntityRepository extends
    MongoRepository<MyEntity, String>,
    QueryDslPredicateExecutor<MyEntity> {}

我的pom.xml中用于QueryDSL生成的插件

            <plugin>
            <groupId>com.mysema.maven</groupId>
            <artifactId>apt-maven-plugin</artifactId>
            <version>1.0.8</version>
            <executions>
                <execution>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>process</goal>
                    </goals>
                    <configuration>
                        <outputDirectory>target/generated-sources</outputDirectory>
                        <processor>org.springframework.data.mongodb.repository.support.MongoAnnotationProcessor</processor>
                    </configuration>
                </execution>
            </executions>
        </plugin>

但是,它将DateTime视为一个常规实体(我尝试使用和不使用QueryType)。我希望它被视为日期,所以我可以进行比较,因为现在我不能:

Predicate predicate = QMyEntity.myentity.lastUpdate... // where are the lessThan or greaterThan methods?

当然,如果可能的话,我想坚持使用JodaTime,而不是回到Java Date,或者将日期存储为millis。

2 个答案:

答案 0 :(得分:3)

如果您使用的是最新版本的Spring Data MongoDB(编写本文时为1.2.0.RELEASE),如果您在类路径中有库,则应注册JodaTime类型的必要Converter实现(见according ticket)。

如果您必须使用它的旧版本,则需要按照reference documentation中的说明手动编写和注册这些转换器。

答案 1 :(得分:1)

org.joda.time.DateTime通常被Querydsl视为DateTime类型,因此一些额外的标志使Querydsl视为实体类型。

额外的标志可能是例如DateTime类型的属性是由@Embedded@Embeddable注释的地方。

如果这不能解决,请在GitHub上为Querydsl打开一张票。