After I switched to annotations from having mapping information in hibernate.cfg.xml, Hibernate isn't using the sequence generated number for the id. I followed the example in Chapter 1 of the Hibernate Reference Documentation, and when the mapping for the Event class was in hibernate.cfg.xml it worked just fine. As soon as I switched to annotations the objects get stored in the database with ids completely different from what are being generated by the sequence, and they increment by 50, as the previous poster experienced. I'm using hibernate-
3.2 and hibernate-annotations-
3.2.0.CR1. Here's the annotated class:
Code:
@Entity
public class Event {
private long id;
private String title;
private Date eventDate;
public Event() {}
@Id
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="eventIdSequence")
@SequenceGenerator(name="eventIdSequence", sequenceName="EVENT_ID")
public long getId() {
return id;
}
... other setters and getters
This is what I notice in the log:
Code:
DEBUG JDBCContext: - after transaction begin
DEBUG ThreadLocalSessionContext: - allowing proxied method [save] to proceed to real session
DEBUG DefaultSaveOrUpdateEventListener: - saving transient instance
DEBUG AbstractBatcher: - about to open PreparedStatement (open PreparedStatements: 0, globally: 0)
DEBUG SQL: - select EVENT_ID.nextval from dual
Hibernate: select EVENT_ID.nextval from dual
DEBUG AbstractBatcher: - preparing statement
DEBUG SequenceGenerator: - Sequence identifier generated: 41
DEBUG AbstractBatcher: - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
DEBUG AbstractBatcher: - closing statement
DEBUG SequenceHiLoGenerator: - new hi value: 41
DEBUG AbstractSaveEventListener: - generated identifier: 2050, using strategy: org.hibernate.id.SequenceHiLoGenerator
DEBUG AbstractSaveEventListener: - saving [event.Event#2050]
Notice that HIbernate is getting the right sequence number (41), but then is using SequenceHiLoGenerator, which makes the id 2050. If I run the example again the sequence will generate 42 and the id will be 2100. Any ideas on what I might be doing incorrectly?