문제

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 = "a@a.com";
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.

도움이 되었습니까?

해결책

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 ;) )

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top