We tell Hibernate how to generate identifiers using the <generator> tag. For example:
<generator class="sequence">
<param name="sequence">person_id_sequence</param>
</generator>
I wish to use <generator class="assigned">. As per the documentation:
assignedlets the application assign an identifier to the object before save() is called.
Is there a way to also configure a sequence generator that I can use to generate the identifiers that I will assign to my objects?
This is roughly what I wish to do:
Session session = ...;
// This will use the "sequence" generator:
Integer id = session.fictionalMethodThatGeneratesIdentifiers(Person.class);
Person person = new Person();
person.setId(id);
person.setName("Adam");
// This will use the "assigned" generator:
session.save(person);
Yes (see below), but the obvious question is “why”?
That’s a lot of work just to arrive at the same result.
However, if you must…
fictionalMethodThatGeneratesIdentifiers() would need to do what the hibernate sequence generator does, which is presumably whatever is required of your particular database to get the next sequence number.
If you do this, note that the documentation says:
So, I’d suggest explicitly mapping unsaved-value to, say -1, and having your object initialize the id value to that, otherwise you are going to see a lot of extra overhead every time you try to save an instance.