veljkost
veljkost

Reputation: 1932

Different behaviour of GenerationType.AUTO + @SequenceGenerator when migrated from Hibernate 3.6 to 5.1

In Hibernate 3.6 we had a working code on both MySql and Oracle:

@Id
@Column(name = "id", nullable = false)
@SequenceGenerator(name = "applicationEventLog", sequenceName = "S_APPLICATION_EVENT_LOG")
@GeneratedValue(strategy = GenerationType.AUTO, generator = "applicationEventLog")
private Integer id;

On MySql using autoincrement, and on Oracle it was using the sequence stated with sequenceName. So it is sequence created on db by us, not a hibernate generated one.

After upgrading hibernate to 5.1, it is no longer using sequence stated in @SequenceGenerator, instead it is using hibernate_sequence and ids conflict with existing ones. If I change GenerationType to SEQUENCE:

@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "applicationEventLog")

it does use the stated sequence, but SEQUENCE is not applicable to MySql.

Is there a way to achieve the old behavior: to have GenerationType.AUTO but use the sequence with the name stated in @SequenceGenerator?

Upvotes: 1

Views: 794

Answers (2)

veljkost
veljkost

Reputation: 1932

We have solved this by changing to hibernate's @GenericGenerator instead of @SequenceGenerator:

@Id
@Column(name = "id", nullable = false)
@GenericGenerator(name = "applicationEventLog", strategy = "native", parameters = {
        @Parameter(name = SequenceStyleGenerator.SEQUENCE_PARAM, value = "S_APPLICATION_EVENT_LOG"),
})
@GeneratedValue(generator = "applicationEventLog")
private Integer id;

Upvotes: 1

Neil Stockton
Neil Stockton

Reputation: 11531

AUTO means leave it up to the JPA provider to choose what type of generator it uses. SEQUENCE forces it to use a SEQUENCE. As per the JPA spec. If you want one specific generator then you should SPECIFY IT, rather than rely on the JPA provider to just happen across the same one as you really wanted

Upvotes: 1

Related Questions