Custom Table Generator : public IntegralDataTypeHolder getNextValue()


#1

Upgrade from hibernate 3 to hibernate 5.1.15
We had custom table generator which i need to replace now.
Old code:
CustomTablegenerator

@SuppressWarnings("sync-override")
    @Override
    public Serializable generate(final SessionImplementor session, Object obj) {
        return getOptimizer().generate(
            new AccessCallback() {
                @Override
                public IntegralDataTypeHolder getNextValue() {
                    return (IntegralDataTypeHolder) doWorkInNewTransaction(session);
                }
            }
        );
    }

New code change:

@Override
    public Serializable generate(final SessionImplementor session, Object obj) {
        return getOptimizer().generate(
            new AccessCallback() {


                @Override
                public String getTenantIdentifier() {
                    return session.getTenantIdentifier();
                }

                @Override
                public IntegralDataTypeHolder getNextValue() {
                    //What to write here ?
                    // doWorkInNewTransaction is deprecated
                }

            }
        );
    }

Found some link but not very clear on what to write.

Migration from Hibernate 3.2 to 4.3.9 TransactionHelper class not found


Remove TransactionHelper in preference of IsolationDelegate
https://hibernate.atlassian.net/browse/HHH-5985

Came across link which says it is not advisible to use table identifier generator

Is it the same what we are doing ?(table-identifier-generator is same as TableGenerator ?)
If yes, we have to take it as cleanup task after hibernate upgrade.

How can i replace doWorkInNewTransaction(session) with
delegateWork(org.hibernate.jdbc.WorkExecutorVisitable work, boolean transacted) from JdbcIsolationDelegate


#2

Why would you write a table generator? You could use the one provided by Hibernate.

Anyway, the table generator is a bad idea from a performance perspective.


#3

To avoid the performance impact we have some customization done.
As per your advise we can take it(removing custom table generator) up later.

As i told you, this is old project and i am just doing hibernate upgrade.
But as first step i need to get this working.
I want to know how to replace doWorkInNewTransaction(session) by using IsolationDelegate.


#4

You can inspect the new codebase and see how it’s done in this new release.


#5

When i search in google i find only hibernate 3 ways.
Is there any blog or any example on how to implement this in hibernate 5 ?

As per your advise, “table generator is a bad idea” we will take it up later.
But as of now, we need to use existing mechanism “table generator”.
Any pointers will be really helpful.


#6

I’m not aware of any such blog post since the vast majority of developers use the generator as provided by Hibernate. Anyone who needs a custom implementation needs to study the source code. It’s just code after all.


#7

As per your blog, it is recommended not to use table generators itself.



The above blog of yours gave a great insight on performance issues due to table generator

I want to analyze the effort that might be needed to remove Table generator(we have also customized it).

  • Which is the preferred way of id generator ? IDENTITY or SEQUENCE ?
  • As part of application upgrade, how will the existing id’s be handled ?(when we use different id generation mechanism)
  • An insight on how hiberate internally manages ID generation would be greatly appreciated.
  • Is there any systematic way of removing table generator ? (Any document or blog which i can have as reference)
  • Any known issue or scenarios which needs more effort ?

#8
  • Which is the preferred way of id generator ? IDENTITY or SEQUENCE ?

SEQUENCE if the DB supports it, otherwise IDENTITY.

  • As part of application upgrade, how will the existing id’s be handled ?(when we use different id generation mechanism)

All entity identifiers are handled by Hibernate in the same way.

  • An insight on how hiberate internally manages ID generation would be greatly appreciated.

You can find all the source code on GitHub.

  • Is there any systematic way of removing table generator ? (Any document or blog which i can have as reference)

You need to apply the changes both at the JPA mapping level and do a DB migration.

  • Any known issue or scenarios which needs more effort ?

No idea.


#9

I think i did not explain it properly, so providing more detail.
We have a product which is used by many customers.
Ex. Events will be raised incase application is not behaving as expected.


eventid is auto generated currently with the help of CustomTableGenerator by extending TableGenerator.
say the following events are generated

  1. volume ‘vol1’ is full(id = 10000)
  2. volume is not writable(id = 20000)
  3. volume has breached threshold(id = 30000)
  4. No permission to write into the volume(id = 40000)

Now with this release we upgrade hibernate and we replace TableGenerator with either SEQUENCE or IDENTITY.
Suppose a new event is generated "volume ‘vol2’ is full(id = ?) ?
I want id to continue from where it was left 40000, 50000, 60000, 70000 and so on.
How can i ensure id continues from where it was left(Maximum number).


#10

How can I ensure id continues from where it was left(Maximum number).

I suppose you need to extend the SequenceStyleGenerator and apply the same logic.


#11

Is there any document explaining internal working of table generator? like

  1. What configuration needs to be done.
  2. How id’s are maintained? does it maintain a table hibernate_sequence ?
  3. How the values are fetched ?
    I tried to find your blogs which are really informative and easy to understand but couldn’t find it.

Continuing this discussion
https://docs.jboss.org/hibernate/orm/5.1/userguide/html_single/Hibernate_User_Guide.html#identifiers-generators-identity
In our case, we use mysql(mariadb) which does not support sequence.
We can’t use identity as we do a lot of batch update(polling - every 10 min create/modify lot of objects).

SequenceStyleGenerator is capable of working against databases that do not support sequences by switching to a table as the underlying backing.
Doesn’t that specify for our needs, we need to use either sequence or table, since mysql does not support sequence, we are forced to use table as SequenceStyleGenerator supports sequence and table ?

I feel backward compatibility for Custom tablegenerator is removed.
I am unable to understand how to get nextValue which was working in earlier release.


#12

You can upgrade to MariaDB 10.3 which supports sequences..


#13

Cool, that might help me. I will go through that.
Thank you.


#14

I checked our code and we are using mysql community edition with mariadb connector in our product. Unfortunately i cannot use mariadb 10.3 in our product.
Even after knowing, tablegenerator has performance issues, i am forced to continue with TableGenerator.
I have to retain custom table generator.

I am little curious as to why backward compatibility was removed for table generator.


#15

Most likely because it was stuck to using hilo instead of the new configurable optimizers.