I see this is an old topic, but having recently been bitten in the ass by this unexpected behavior when we recently "upgraded" our Hibernate XML schema (using v3.3.1) to annotations (using v3.5.2) I'd like to respond to this question:
Quote:
So were do we stand? Is SeqHiLo the right way to go? What are people choosing when faced with this?
For our purposes, relying solely on Hibernate to generate sequence values is astoundingly dangerous. Like, string-concatination vs. named parameters dangerous. So dangerous I would almost say
stupid, unless you have some truly magnificent reason, and even then I'd try and talk you out of it.
The problem we ran into is that the default for the annotation is to use the brain-dead HiLo generator, which as noted before can quickly grow your indexes geometrically. In my mind, if you are going through the trouble of specifying a particular sequence for Hibernate to use, that would tend to indicate that you
don't want Hibernate to use its own methods.
Code:
@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "my_table_seq")
@SequenceGenerator(name = "my_table_seq", sequenceName = "my_table_seq")
@Column(name = "my_table_id", nullable = false)
public Integer getId() {
return id;
}
Looking at this code, where we've gone through and identified the name of the DB sequence generator in
three different places, you might be tempted to think this would actually
use that sequence. Nope. HiLo.
Furthermore, relying on the allocationSize is another recipe for disaster. The only way you can fully trust Hibernate is if you can guarantee that you will never, ever touch the DB manually. In my entire professional life I have never encountered such a situation. Thus, as already recommended, if you want to use your database's sequence generator, you have to specify GenerationType.AUTO.