Hibernate creating Entity Primary Key Id Negative - spring-boot

After upgrading to Spring Boot 2.7.7, which is dependent on Hibernate 5.6.14, Entities that are configured with Sequence Generator, new records started with negative ids?
For tables with records already in the database, sequence continues to work fine. The tables are using a postgres sequence to increase by 50, but first id is generating as -47, then -46 and so on. This must be a divide by zero issue, because it doesn't happen on records id with a pre-existing value greater than zero.
Very interesting issue to find, I only noticed because fresh db with unit testing.. Seems Hibernate had issue with this in the past but many years ago.

I'ved determined that Spring Boot dependence on Hibernate 5.6.14 must be downgraded to 5.6.7.Final to resolve the issue. I added a implementation in gradle dependence section.
implementation("org.hibernate:hibernate-core:5.6.7.Final")

Related

Old value in _aud table in envers

I have integrated hibernate envers in spring boot. Now my requirement is to have old value also for the particular column when value changed in *_AUD tables. However I cant see any feature available in Hibernate Envers plugin.
Please suggest.
Thanks
Unfortunately what you are looking to do just isn't someting supported.
It's one thing to think of an entity and needing to store basic type values such as strings or numeric data and have its old/new value represented by two columns in the audit table; however when you move beyond basic entity mappings to ones where you have relationships between entity types or collections; you begin to see that trying to store old/new data in the same row just isn't efficient and in some cases feasible.
That said, you can still read the audit history and deduce these old/new values using a variety of ways that include the Envers Query API, Debezium, or even basic database triggers.

Why is the axon framework not able to insert in its own table?

I just started to use the Axon-Framework and I like it a lot. So I wanted to integrate it in an existing project. But when I try to start my application I get the following errors:
Fetch Segments for Processor 'my.package.name' failed: org.hibernate.exception.ConstraintViolationException: could not execute statement. Preparing for retry in 4s
ERROR: null value in column "_identifier_mapper_processor_name" violates not-null constraint
Detail: Failing row contains (my.package.name, 0, null, null, null, 2020-01-21T09:32:28.189Z, null, null).
I do understand that an unique constraint is violated but the table is managed by the axon framework. I tested it an new project and looked at the database. And the table token_entry did not contain the _identifier_mapper_processor_name column. Then I looked at the database of my old project and the table contained the two additional columns: _identifier_mapper_processor_name, _identifier_mapper_segment.
Why does an table of the axon framework sometimes contain these addional columns and sometimes not?
The problem seems to be hibernate. The ImplicitNamingStrategyComponentPathImpl naming strategy (which is used by my project) has a problem with #IdClass: Ticket.
The axon framework uses #IdClass for the token_entry table. This leads to the error described above.
Neither _identifier_mapper_processor_name nor _identifier_mapper_segment occur in any search you'd perform on the Axon Framework GitHub page. Thus, not in current framework code nor any commits over the last 10 years of Axon's existence.
Hence, I am very hard pressed to understand how you did end up with both columns to begin with. Would you mind sharing what version of Axon you're using and if you are adding any Axon Framework Extensions to the project too?
As far as resolving the problem, I'd assume something is tagging along when creating the token_entry table in your existing project.
Any specifics you are doing in your existing project as far as Axon and/or database modification would be beneficial to deduce where this problem stems from.

Need thoughts on where to implement unique number generation logic in our distributed environment

We have a unique requirement where we need to create fixed 12 digit unique number for every transaction we process successfully in our current application. The application is set of restful services and has Oracle DB as a data store.
We do have the logic as to how to come up with unique 12 digit number but we are trying to understand where we can fit this logic so that the transactions which are getting executed in this environment gets reference to this unique id.
We figured out that keeping some part of that 12 digit in DB sequence could be an option but that will not work in near future as we would be having multiple databases.
How about if you have a Sequencer service which is responsible for generating these unique numbers? When a new transaction is created, the entity which manages the transaction can request a unique number from this service and associate this with the transaction.

performance issue in find() method after migration to Hibernate 4.0 from OpenJPA 1.2

I migrate from OpenJPA 1.2 to Hiberante 4.0
I'm using TimesTen DB
I'm doing a native query to get id's of object's that I need , and then perform find on each on of them.
In OpenJPA instead of find I used findCache() method and if it return null I use the find() method , In hibernate I used only the find() method.
I performed this operation on the same DB.
after running couple of test I saw that the performance of OpenJPA is far better.
I printed the statistics of hibernate session ( after querying and finding the same object's) and saw that the hit\miss count to the first level cache is always 0.
while the OpenJPA is clearly reaching it's cache by fetching object's with the findCache method.
How can I improve the performance of find in Hibernate ?
I suspect it referred to the difference in the first level cache implementation of this tools.
another fact: I use the same EntityManager for the application run time ( I need to minimize the cost of creating of an EntityManager - my app is soft real time )
thanks.
Firstly, why don't you just retrieve the full objects instead of the id. One select statement to retrieve a number of objects is many magnitude times faster than retrieving each item individually.
Secondly, you likely need a second level cache for hibernate. The first level cache is mostly applicable within each session.
The first level cache in Hibernate corresponds to the session. So if the session has not yet loaded a given object, it will be a miss.
You need to enable second level cache to be able to cache an object by id across sessions.
Check out the reference documentation for more info http://docs.jboss.org/hibernate/orm/4.1/manual/en-US/html_single/#performance-cache

managing/implementing auto-increment primary key in oracle without triggers

We have many tables in our database with autoincrement primary key ids setup the way they are in MySQL since we are in the process of migrating to Oracle from MySQL.
Now in oracle I recently learned that implementing this requires creating a sequence and a trigger on the id field for each such table. We have like 30 -40 tables in our schema and we want to avoid using database triggers in our product, since management of database is out of scope for our software appliance.
What are my options in implementing the auto increment id feature in oracle... apart from manually specifying the id in the code and managing it in the code which would change a lot of existing insert statements.
... I wonder if there is a way to do this from grails code itself? (by the way the method of specifying id as increment in domain class mapping doesnt work - only works for mysql)
Some info about our application environement: grails-groovy, hibernate, oracle,mysql support
This answer will have Grails/Hibernate handle the sequence generation by itself. It'll create a sequence per table for the primary key generation and won't cache any numbers, so you won't lose any identifiers if and when the cache times out. Grails/Hibernate calls the sequence directly, so it doesn't make use of any triggers either.
If you are using Grails hibernate will handle this for you automatically.
You can specify which sequence to use by putting the following in your domain object:
static mapping = {
id generator:'sequence', params:[sequence:'MY_SEQ']
}

Resources