[PersistenceException: Error getting sequence nextval]
Asked Answered
C

3

5

i am getting this error while trying to save data into model in db.

@Entity
public class User extends Model {
   @Required
   public String name; 
   @Email
   public String email; 
   @Required @MaxLength(value=10)
   public String username;
   @Required @MinLength(value=4)
   public String password;
   @Id 
   public int id;
}

this is my Class.

this is the error while i am trying to save the model into db.

enter image description here

i will appreciate any effort for help! many thanks.

EDIT: table structure is here enter image description here

C answered 29/6, 2012 at 11:13 Comment(6)
Please show your table structure (sql file which creates tables). And tell us on what db you're working, because, if it is on MySQL, it does not support sequences.Punish
i added my table structure, please see updateC
did you use ebean to generate your tables?Bergquist
yeah, it generated itself. but my suspect is that i have also an ID of my class. should i delete this as Ebean will create an ID automatically?C
Possible duplicate of this : #9999607Punish
thanks, Nico, i solved the problem by hardcoding the ID value and putting the @GeneratedValue annotation... now it is working like anything :D..C
B
10

I think with ebean you have to physically name and annotate your id. You may also have to tell it the name of the backing sequencer as well (I dont remember). This shows how to do it.

Bergquist answered 29/6, 2012 at 11:20 Comment(4)
John, thanks alot! that was the point. Great Stackoverflower! ;)C
This solution also worked for me (Play 2.0.4). The key was adding this annotation to my User model: @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "users_id_seq"). I dunno if Play isn't identifying the sequence correctly or what, but it works now. Frustrating.Selfregard
I also had to define the sequence, otherwise Hibernate would tell me it doesn't know about users_id_seq: @SequenceGenerator(name="seq_gen_name", sequenceName="task_seq") @GeneratedValue(strategy=GenerationType.SEQUENCE, generator="seq_gen_name") @Id public Long id;Weltanschauung
These example worked for me except I had to add my schema: sequenceName="myschema.my_seq" A simple thing, but easy to miss. It is mentioned in the code in the answer below.Diphosgene
P
2

This worked for me:

@Entity
@Table(name = "table", schema = "schema")
public class Bean extends Model{

   @Id
   @Column(name = "idcolumn")
   @SequenceGenerator(name="gen", sequenceName="schema.table_idcolumn_seq",allocationSize=1) 
   @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "gen")
   private int id;
}

When using the SequenceGenerator, please mind this bug in Hibernate: https://hibernate.atlassian.net/browse/HHH-7232

It forces you to write the schema directly into the sequenceName instead of using the schema field in the SequenceGenerator annotation.

Punic answered 10/7, 2014 at 6:53 Comment(0)
P
1

This worked for me on class annotation:

@SequenceGenerator(name = "SEQUENCE_NAME", sequenceName = "PST_BUSINESS.S_BUSINESS_DOMAIN")
@Entity
@Table(name = "TB_BUSINESS_DOMAIN", schema = "PST_BUSINESS")
public class PstBusinessDomain extends PstAbstractBaseMappedEntity {

As Leo said, this strategy works for annotation in the field and also in the class.

Prioress answered 10/7, 2015 at 3:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.