问题
I am using hibernate entity - and sequence generator with it.
My database is Oracle 12c.
In my sequence generator - it fetches value which is already present in table.
I tried looking out for issue - found one simmilar thread Hibernate sequence nextVal resolved but not used (Oracle)
But still it did not help . The issue am facing is - it works some times and it does not work some times
Below is my code snippet -
@Entity
@Table(name="TABLE_NAME", schema = "SCHEMA")
@SequenceGenerator(name="TABLE_ID_SEQ_GEN", sequenceName="SCHEMA.TABLE_ID_SEQ",allocationSize=1)
public class ImportTransactionDataEntity {
@Id
@Column(name="TABLE_ID",unique=true,nullable=false)
@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="TABLE_ID_SEQ_GEN" )
private int IDColumnPk;
ANy help is appreciated , thank you :)
回答1:
According to Oracle documentation, the @SequenceGenerator should be added to the field, not to the class:
https://docs.oracle.com/cd/E16439_01/doc.1013/e13981/cmp30cfg001.htm#BCGGHADG
so something like:
@Entity
@Table(name="TABLE_NAME", schema = "SCHEMA")
public class ImportTransactionDataEntity {
@Id
@Column(name="TABLE_ID",unique=true,nullable=false)
@GeneratedValue(strategy=GenerationType.SEQUENCE,generator="TABLE_ID_SEQ_GEN" )
@SequenceGenerator(name="TABLE_ID_SEQ_GEN", sequenceName="SCHEMA.TABLE_ID_SEQ",allocationSize=1)
private int IDColumnPk;
This works for me.
回答2:
try this
@Entity
@Table(name="TABLE_NAME")
@SequenceGenerator(name="TABLE_ID_SEQ_GEN",sequenceName="TABLE_ID_SEQ",allocationSize=1)
public class ImportTransactionDataEntity {
@Id
@Column(name="TABLE_ID",unique=true,nullable=false)
@GeneratedValue(strategy=GenerationType.AUTO,generator="TABLE_ID_SEQ_GEN" )
private int IDColumnPk;
it is stored in you database like this the last nummber is the last id used so dont worry about loosing count cause the database does that for you
回答3:
well finally - i had come to a solution that i was already using oracle 12 C - decide to go with Identity column on the table it self .
NOTE - but the identity column is not available before Oracle 12c .
I ended up getting better performance also with some fraction , so this work around was sigh of relief for me
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "TABLE_ID", updatable = false, nullable = false)
private int IDColumnPk;
来源:https://stackoverflow.com/questions/52055666/hibernate-having-issues-with-sequence-object-unable-to-call-next-val