Using Hibernate sequence generators manually

Basically, I want to access the sequence values โ€‹โ€‹in a neutral database. A use case is that I have a field on the entity that I want to set based on the incremental value (except the identifier).

For example, let's say I have an object Shipment. At some point after the shipment is created, it will be shipped. As soon as it is sent, a manifest number is generated for it and assigned. The manifest number looks something like M000009(where the material after "M" is the value to the left of the sequence).

Something similar was asked here in SO , but I am not a fan of this solution, as it needs another table to maintain and seems like a weird attitude towards.

Does anyone know if it is possible to use something like hibernate MultipleHiLoPerTableGeneratoras something other than an ID generator?

If this is not possible, does anyone know of any libraries that can handle this (either using sleep mode, or even just plain JDBC). I would rather not write this myself (and have to deal with pre-fetching values, locking and synchronizing).

Thanks.

+3
source share
3 answers

, , , :

  • , .
  • ( ), , .

, :

  • JDBC , ( ), , .
  • .

, , , , .

+1

, -, . . , .

, sql- .

hibernate Dialect, db.

"" . , . (.. ), . ( )

+1

Here is a sample code. I would like to do this because I have not compiled this and it is requesting spring code. Having said that, he should still provide the bones of what you want to do.

public Long getManifestNumber() {
        final Object result = getHibernateTemplate().execute(new HibernateCallback() {
            public Object doInHibernate(Session sess) throws HibernateException, SQLException { 
                SQLQuery sqlQuery = sess.createSQLQuery("select MY_SEQUENCE.NEXTVAL from dual");
                sqlQuery.uniqueResult();
            }
        });
        Long toReturn;
        if (result instanceof BigDecimal) {
            toReturn = ((BigDecimal)result).longValue();
        }
        return toReturn;
    }
+1
source

Source: https://habr.com/ru/post/1717262/


All Articles