0

I have: (MySQL 5.1)

CREATE TABLE `poh_faktury` (
  `cislo` int(10) unsigned NOT NULL PRIMARY KEY,
... 
) ENGINE=MyISAM DEFAULT CHARSET=utf8$$

@Entity
@Table(name="poh_faktury")
@NamedQueries({
  @NamedQuery(name="dalsiCisloFaktury", query="SELECT MAX(fakt.cislo) + 1 FROM Faktura AS fakt")
})

The generated queries (yes, two) are:

select MAX(faktura0_.cislo)+1 as col_0_0_ from poh_faktury faktura0_
select MAX(faktura0_.cislo)+1 as col_0_0_ from poh_faktury faktura0_ limit 2

But this

    Integer res = ((Integer) this.getJpaTemplate().execute( new JpaCallback() {
        public Object doInJpa( EntityManager em ) throws PersistenceException {
            Query q = em.createNamedQuery( "dalsiCisloFaktury" );
            return q.getSingleResult();
        }
    }));

puts null into res.

When I invoke SQL, it returns 1000 as it should:

SELECT MAX(fakt.cislo) + 1 FROM poh_faktury AS fakt;

How should I make Hibernate return correct scalar value?

Ondra Žižka
  • 43,948
  • 41
  • 217
  • 277
  • 1
    Which SQL query does it generate? How is the cislo field defined an mapped? – JB Nizet Jul 21 '12 at 15:32
  • Hmm, interesting. After restarting MySQL (to enable logs), the value is correct. I am about to delete the question. Thanks. – Ondra Žižka Jul 21 '12 at 15:46
  • You could log the queries in hibernate rather than doing it in MySQL. It would be easier to diagnose problems this way. – JB Nizet Jul 21 '12 at 15:53

0 Answers0