Hi,
I have some trouble with id generation in my project. The IDs should be generated by the database (Postgresql) and Hibernate should use these IDs.
Therefore I setup the sequence generator of my entity as follows:
Code:
@SequenceGenerator(name = "SizeCallingSettingSequence", sequenceName = "seq_sizecallingsetting", schema = LIMSModule.SCHEMA_NAME)
...
public class SizeCallingSettingEntity extends AbstractEntity<Long> {
...
@Override
@Id
@Column(name = "id")
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "SizeCallingSettingSequence")
public Long getId() {
return this.id;
}
...
}
Sometimes persisting entities of type SizeCallingSettingEntity results in an error because of duplicate IDs in the table:
Code:
10:41:02,827 INFO [stdout] (EJB default - 10) Hibernate: select nextval ('lims.seq_sizecallingsetting')
10:41:02,827 DEBUG [org.hibernate.id.enhanced.SequenceStructure] (EJB default - 10) Sequence value obtained: 15
10:41:02,827 DEBUG [org.hibernate.engine.jdbc.internal.LogicalConnectionImpl] (EJB default - 10) Releasing JDBC connection
10:41:02,827 DEBUG [org.hibernate.engine.jdbc.internal.LogicalConnectionImpl] (EJB default - 10) Released JDBC connection
10:41:02,827 DEBUG [org.hibernate.event.internal.AbstractSaveEventListener] (EJB default - 10) Generated identifier: -35, using strategy: org.hibernate.id.enhanced.SequenceStyleGenerator
...
10:41:03,447 INFO [stdout] (EJB default - 10) Hibernate: insert into lims.sizecallingsetting (lockversion, abi310matrix, algorithmType, baseliningwindowsize, calloffscale, calloverlap, callshoulder, callstutter, cutoff, description, endpoint, fsaprocessingtype, heightofnotlabeledpeaks, initHomozygousAlleles, laddercutoffpercentage, manualcutoff, minpeakheightblue, minpeakheightgreen, minpeakheightorange, minpeakheightred, minpeakheightyellow, minpeakwidth, name, noiselimit, offsetvalue, peakheightratiopercentage, peakwindowsize, polynomialdegree, sizecallingassignedsizespercentage, sizecallingqualitylimit, sizedpeakratiofactorforwarning, smoothinggranularity, startingpoint, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
10:41:03,457 DEBUG [org.hibernate.engine.jdbc.spi.SqlExceptionHelper] (EJB default - 10) FEHLER: doppelter Schlüsselwert verletzt Unique-Constraint »sizecallingsetting_pkey« [n/a]: org.postgresql.util.PSQLException: FEHLER: doppelter Schlüsselwert verletzt Unique-Constraint »sizecallingsetting_pkey«
(N.B. Error: duplicate key violates unique-constraint »sizecallingsetting_pkey«)
It seems that the correct sequence value returned by the database, in this case 15, is somehow regenerated by SequenceStyleGenerator. The generated identifier, in this case -35, already existed in the db and the error is thrown.
To solve the problem I replaced the sequence generator by this one:
Code:
@GenericGenerator(name="SizeCallingSettingSequence", strategy="sequence", parameters = {@Parameter(name="sequence", value="seq_sizecallingsetting"), @Parameter(name="schema", value=LIMSModule.SCHEMA_NAME)})
This helps but does not explain the error before.
I also read something about hibernate.id.new_generator_mappings. In my jboss 7 debug logs I saw that this value seems to be set to "true" by default (hibernate ref manual says its false by default). I tried setting it to false but then all sequences are missing:
Code:
org.hibernate.exception.SQLGrammarException: FEHLER: Relation »rmberlin.seq_patient« existiert nicht (n.b. does not exist)
I'm using Hibernate 4.0.1-Final, JBoss 7.1.1-Final, Postgres 8.0.4