2

At the moment I'm trying to combine a pretty old messy monolithic application with a nice up to date Spring micro-service architecture to get rid of the monolith some time in the future. The problem I struggle, is to generate unique ids when inserting new entities on both sides (hibernate and eclipselink).

At the moment, the monolith application uses Eclipse Link and unfortunately, I can't really touch the old domain model, due to heavy dependencies through different application layers. But now it's the point, where the application is simply not maintainable anymore, and implementing new features is like walking through a minefield with one leg only.

So we decided to migrate to a nice and clear micro-service architecture and to replace more and more parts of the monolithic application, till we fully replace it.

The first step was to generate a new independent domain model from the database directly to use it in the spring/hibernate application. But now we struggle with id generation.

Did anyone use Eclipse Link and Hibernate together on the same postgres database?

We already tried to use GenerationType.AUTO and GenerationType.TABLE on both sides, but it seems like Eclipse Link and Hibernate calculate new IDs completely different. We also tried to set the hibernate property "new_generator_mappings:true" to avoid the multiplication by 50 which hibernate is always using. But unfortunately this didn't work either.

Kind regards, Florian

Mirza Sisic
  • 2,401
  • 4
  • 24
  • 38
FlorianG.
  • 21
  • 2
  • Using AUTO gives away control of what is used for generation strategy, so ignore that if you want predictable behaviour across JPA providers and databases. You can set JPA standard "allocationSize" to control how many ids are allocated. Beyond that you give inadequate details to comment – Neil Stockton Sep 13 '16 at 09:30
  • Hey Neil, thanks for answering. The allocationSize is set to 100 on both sides. Hibernate and Eclipse Link. But the problem is: Lets say the actual id for a given entity is 50. So EclipseLink looks at the corresponding sequence table and sees there's 50 and will automatically set the sequence to 151 due to the allocation size of 100. On this side everything is fine. But now comes the other side, the hibernate side. Hibernate sees, that there's 50 on the sequence table. – FlorianG. Sep 13 '16 at 09:46
  • AllocationSize is also 100. So hibernate writes 51 to the sequence table and internally multiplies (50+1) by 100 internally and keeps the id pool in memory. So we have a completely different id range and also the problem with collisions between the ranges... – FlorianG. Sep 13 '16 at 09:46
  • EclipseLink will use the next 100 numbers in a sequence; when it sees 50 it should set to 150 and use 51-150. What exactly is Hibernate using? Either ORM should be configurable/changeable so that you can use a custom sequence object that mirrors the other's generation strategy. Or you can switch both to use a database strategy - but sounds like you are stuck with modifying the Hibernate application. – Chris Sep 13 '16 at 16:51
  • Hibernate sees that there's 50, sets the value to 51 and uses internally (50+1)*100 ids, so the result id range is 5000-5100 – FlorianG. Sep 14 '16 at 09:24

0 Answers0