Moritz
Moritz

Reputation: 85

Hibernate Search Query for timestamp returns empty list

I am working on a Spring Boot Project with Hibernate and I'm trying to map timestamps out of an SQL-Database. Other queries against strings or numbers work fine, so the problem should not be the project structure itself.

Snippet of my User.java class:

@Indexed
@Entity

public class User {
  @Id
  @GeneratedValue(strategy=GenerationType.AUTO)
  private Integer id;

  @Field
  @Basic
  private java.sql.Timestamp datum;

  public java.sql.Timestamp getDatum() {
    return datum;
  }
}

Snippet of my search service:

FullTextEntityManager fullTextEntityManager =
    org.hibernate.search.jpa.Search.
    getFullTextEntityManager(entityManager);

QueryBuilder queryBuilder =
    fullTextEntityManager.getSearchFactory()
    .buildQueryBuilder().forEntity(User.class).get();

org.apache.lucene.search.Query query =
        queryBuilder.range().onField("datum")
          .below(java.sql.Timestamp.valueOf("1980-02-01 11:02:20.000")).createQuery();

org.hibernate.search.jpa.FullTextQuery jpaQuery =
    fullTextEntityManager.createFullTextQuery(query, User.class);

@SuppressWarnings("unchecked")
List<User> results = (List<User>) jpaQuery.getResultList();

return results;

Upvotes: 1

Views: 703

Answers (1)

The 0bserver
The 0bserver

Reputation: 986

You could just use SpringJPA no?

So it would then just be an interface

@Repository
public interface YourModelRepository extends JpaRepository<YourModel, Integer>{
        List<YourModel> findByDatumGreaterThan(LocalDatetime datum);
}

Let JPA's interface take care of it.

Here's the spring jpa reference sheet.

Upvotes: 3

Related Questions