annotation in the model for postgresql auto increment

760 views
Skip to first unread message

fan...@gmail.com

unread,
Sep 4, 2013, 11:39:20 AM9/4/13
to play-fr...@googlegroups.com
In my java Users class, I have the following code and I want the id column to be auto incremental.  I am using postgresql 9.1

@Entity
public class Users extends Model {

    @Id
    @SequenceGenerator(name = "Token_generator", sequenceName = "id_seq")
    @GeneratedValue(generator = "Token_generator")
    public long id;

    public String firstName;
    public String lastName;
    public String password;
    public String email;
    .....

}

But the evolution script does not seem to be able to generate the SERIAL column.  The content of the 1.sql is something as the follows.  The id field does not appear to be auto incremental.  Any ideas?

# --- Created by Ebean DDL
# To stop Ebean DDL generation, remove this comment and start using Evolutions

# --- !Ups

create table users (
  id                        bigint not null,
  first_name                varchar(255),
  last_name                 varchar(255),
  password                  varchar(255),
  email                     varchar(255),
  constraint pk_users primary key (id))
;

create sequence users_seq;




# --- !Downs

drop table if exists users cascade;

drop sequence if exists users_seq;

Message has been deleted

fan...@gmail.com

unread,
Sep 4, 2013, 11:41:38 AM9/4/13
to play-fr...@googlegroups.com
Forgot to mention that I am using play 2.1.2

adel alfar

unread,
Sep 10, 2013, 11:48:29 AM9/10/13
to play-fr...@googlegroups.com
Here is how I am doing it (and it works for me in PostgreSQL):

@Entity
@Table(name ="A")
public class A {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id", nullable = false)
    private Long id;

....
}

Here is the 1.sql:

create table a (
  id                        bigint not null,

...
constraint pk_driver primary key (id)
)

...
create sequence a_seq;


Hope that helps

adel alfar

unread,
Sep 10, 2013, 11:50:04 AM9/10/13
to play-fr...@googlegroups.com

Correction:
.....
public class A extends Model {...
....

Fan Dong

unread,
Sep 10, 2013, 11:57:37 AM9/10/13
to play-fr...@googlegroups.com
In your 1.sql, id column is followed by 'bigint not null'. I do not understand how postgresql can understand id column is auto incremental.  How did you insert a row ? did you simply leave the id column empty?

Thanks a lot!
--
You received this message because you are subscribed to a topic in the Google Groups "play-framework" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/play-framework/PS733kvrsIs/unsubscribe.
To unsubscribe from this group and all its topics, send an email to play-framewor...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

adel alfar

unread,
Sep 10, 2013, 12:06:25 PM9/10/13
to play-fr...@googlegroups.com
I am using ebean (hence the the extends Model part)....

All I have to do is execute A.save() and the id will be generated....

Fan Dong

unread,
Sep 10, 2013, 1:09:03 PM9/10/13
to play-fr...@googlegroups.com
cool, I will give a try!

Thanks again.
Reply all
Reply to author
Forward
0 new messages