• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem regarding type conversion between BigInt and Integer/Long in JPA/Hinbernate/MySQL

Marco Ehrentreich
best scout
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello rangers,

I'm using JPA with Hibernate as a persistence provider together with a MySQL 5.x database for a standalone Java project. So far everything works fine except for one small problem which obviously has to do with type conversion. I'm experimenting for hours now trying to figure out what is wrong here but I'm completely stuck with this. For this I can't even tell you if it's a problem with JPA, Hibernate, MySQL or my application in general.

I have an entity class "Script" which uses an @IdClass composed of two attributes - an Integer "scriptId" and a Long value "activeFrom". Hibernate/JPA is configured to auto-generate the database schema just to be sure to that it's not a problem with a handmade schema. If I make some tests everything looks good at first. The database schema is generated correctly as far as I can tell, that means the problematic Long value is mapped to a BigInt in MySQL which should work from my understanding. It corretly identifies primary key attributes etc. I can create some sample data composing an entire object graph which is persisted correctly to the database.

The only thing that obviously doesn't work is to read the data again from the database. I tried everything from native queries, to EJBQL queries and of course the find() and getReference() methods of EntityManager. Nothing has worked yet. I always get an exception like the following:

The value shown is the right one already written to the database by the same application but it looks like Hibernate/JPA is trying to read the long value as an INTEGER which certainly will not work, at least not with such big values.

Has anyone ever had similar problems? How can it create a correct database schema and write data to it and then fail when reading the data? Doesn't Hibernate use the same data type mapping for read and write operations? Or is this a bug? I can't even tell if it could be a bug with Hibernate or MySQL or whatsoever.

I'd really appreciate any help! Thanks in advance!

Gravity is a harsh mistress. But this tiny ad is pretty easy to deal with:
the new thread boost feature: great for the advertiser and smooth for the coderanch user
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!