0

so I have custom IdGenerator like that:

@Service
public class IdGenerator extends SequenceStyleGenerator {

    private final SequenceRepository sequenceRepository;

    public IdGenerator(SequenceRepository sequenceRepository) {
        this.sequenceRepository = sequenceRepository;
    }

    @Override
    public Serializable generate(SharedSessionContractImplementor session, Object object) throws HibernateException {
        BaseObject type = ((BaseObject) object);
        return StringUtils.getNumberWithMaxRadix(sequenceRepository.getNext(type.sequenceName()));
    }

}

And on Entity Id field Hibernate annotations:

@Id
@Column(name = "id", nullable = false, length = 18)
@SequenceGenerator(name = "location_road_seq", sequenceName = "location_road_seq", allocationSize = 10)
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "location_road_seq")
@GenericGenerator(
        name = "location_road_seq",
        strategy = "com.wildtigerrr.bestgamebot.bin.service.base.IdGenerator",
        parameters = {
                @org.hibernate.annotations.Parameter(name = IdGenerator.VALUE_PREFIX_PARAMETER, value = "a0lr")
        })
private String id;

When inserting objects from the code with Hibernate - works like a charm. But I need to insert initial data with Liquibase, and here I have issues inserting data from Liquibase changeset:

INSERT INTO location (id, system_name)
VALUES (nextval('location_seq'), 'TestLocation');

Returns simple values from sequence as Id.

Is there an option and how should I configure Liquibase to use my IdGenerator?

And if it's not possible, what's best practices/possible solutions for overcoming that issue? Any feedback would be appreciated!

  • maybe you can write an sql function that will do the trick for you. So you could call it from database (smt. like `select id_generator(seq_name)`) and even from java. So in your `SequenceRepository` you will call function instead of thing you are calling now. – bilak Dec 21 '21 at 21:08
  • @bilak Thanks! Sounds like a possible workaround, but I don't want to put additional workload on a DB side. So currently I've solved it with removing custom id generator at all for records creation, and just use it when I need to pass data with formatted ids, converting real DB ids in a runtime. – WildTigerrr Dec 23 '21 at 13:35

0 Answers0