Reputation: 198318
I'm using Ebean, and define such a model:
@Entity
@Table(name = "users")
public class User extends Model {
@Id
public String id;
public String email;
public String name;
}
You can see the field id
is String
, and has a @Id
annotation.
Now I save it:
User user = new User();
user.id = "abc";
user.email = "[email protected]";
Ebean.save(user);
But when I saved it, I found the value of it's id
is: 1
, not abc
I specified.
I checked the sql, found the table generate:
create table users (
id varchar(255) not null,
email varchar(255),
name varchar(255),
constraint pk_users primary key (id))
;
create sequence users_seq;
You can see there is a seq users_seq
which has been used when inserting a user.
How to define the model or how to configure Ebean to let it not do anything to the @Id field? Just let it use my specified value?
===========
UPDATE
Sorry, guys, I found this strange behavior is because of my mistake!
Yes, I use it with playframework 1, and I tried to create a play-ebean module for myself. But there is something wrong: When I save a model, I cleared the id value by mistake!
So the assigned value abc
is missing, then Ebean will try to use a seq to get a new value for it.
Thanks for all you help, and sorry again, I will be more careful when I ask question next time.
Upvotes: 1
Views: 3007
Reputation: 14373
You need to extend GenericModel
instead of Model
if you want to operate on your own @Id
field. I am also talking in PlayFramework context.
Upvotes: 1
Reputation: 55798
Isn't it better idea to create another unique field and use it optionally ie. String userCode
or something?
Play with Ebean uses auto incrementation of Id's to make sure the Id is unique so I'd personally didn't change that as all docs assumes that id of model is some numeric kind.
You can thought use Play's routes to find user by its string-id and still use Long id to perform basic operations.
(I'm writing in Play's context as I assume that you ask in it ;) )
Upvotes: 3