0

I have a following method:

public Book findBookByTitleAndVersion(String title, Float version) {
    TypedQuery<Book>query = em.createQuery("SELECT b FROM Book b WHERE b.title = ?1 AND b.version = ?2" ,Book.class);

    query.setParameter(1, title);
    query.setParameter(2, version);
    return query.getSingleResult();
}

I'm using MySQL and I have some records that should be returned. I store 'version' as float type (1.22 in my case). This query returns:

javax.persistence.NoResultException: No entity found for query

Float has something to do here, as query by title works fine.

jarosik
  • 4,136
  • 10
  • 36
  • 53
  • 3
    It's pretty unsafe to use floating point values for exact comparison. Version *really* shouldn't be a float in the database, so this is mainly a design problem. – Kayaman Mar 28 '17 at 14:47
  • so what would you suggest as an alternative? – jarosik Mar 28 '17 at 14:50
  • More than likely. Read this http://stackoverflow.com/questions/2100490/floating-point-inaccuracy-examples and change version to a string. – Tony Hopkinson Mar 28 '17 at 14:50
  • A string, an int, there are plenty of choices. There's no reason it needs to be a floating point value, and there's no advantages (but plenty of disadvantages) in it. – Kayaman Mar 28 '17 at 14:51

0 Answers0