I have two projects-- one a JBoss application, one a standalone Java application-- that both use the same JPA entities (Hibernate 3.3.1GA, Hibernate Entity Manager 3.4.0GA, JBoss 5.0.0.CR2). Both apps use the same orm.xml file (we use xml mapping instead of annotations because we have to support multiple databases). The entities are mapped as follows in the orm.xml file:
Code:
<entity class="werner.opt.logos.config.Profile" metadata-complete="true" access="FIELD">
<table name="profile" schema="config"/>
<attributes>
<id name="id">
<column name="profile_id" />
<generated-value strategy="SEQUENCE"
generator="PROFILE_ID_SEQ_GEN"/>
<sequence-generator name="PROFILE_ID_SEQ_GEN"
sequence-name="config.profile_profile_id_seq"/>
</id>
<basic name="name" />
<basic name="processPath">
<column name="process_path"/>
</basic>
</attributes>
</entity>
The peristence.xml files are similar; the only difference being that the JBoss app uses a datasource, so it does not contain the database connection info. When the JBoss application inserts entities, the primary keys are generated from the sequence associated to the table. However, when the standalone application inserts entities, it inserts primary keys that do not come from the table's sequence (and each subsequent insert seems to increment the primary key by 50). When I ran the standalone app with "show_sql" set to true, here's what I found in the log file:
Code:
DEBUG [main] (SQLStatementLogger.java:111) -
select
nextval ('config.profile_profile_id_seq')
Hibernate:
select
nextval ('config.profile_profile_id_seq')
DEBUG [main] (SequenceGenerator.java:105) - Sequence identifier generated: 10
DEBUG [main] (AbstractBatcher.java:418) - about to close PreparedStatement (open PreparedStatements: 1, globally: 1)
DEBUG [main] (SequenceHiLoGenerator.java:85) - new hi value: 10
DEBUG [main] (AbstractSaveEventListener.java:135) - generated identifier: 500, using strategy: org.hibernate.id.SequenceHiLoGenerator
DEBUG [main] (JDBCTransaction.java:134) - commit
It looks like nextval is being called on the appropriate sequence (and the correct sequence value returned), but then this value is ignored and instead the org.hibernate.id.SequenceHiLoGenerator is called, which returns an identifier in no way associated with the value returned by the sequence; it is this value which is inserted when the entity is persisted.
Why isn't the standalone app using the value returned from the Postgres sequence? Why the subsequent call to the HiLoGenerator? And why does it work as expected in the JBoss application?
My team has spent all day trying to figure out this issue. Since we will be inserting some records manually (not through the ORM layer), we need to make sure that the entities get their primary keys from the sequence associated to their table. Any help would be greatly appreciated!
Thanks,
Tim