开发者

generated id to be copied in anothre column as string upon creation

This is a JavaEE6 application using Hibernate and PostgreSQL. Users are created in the database with @GeneratedValue(strategy = GenerationType.AUTO) that generates Long ids. I am trying to use the user_id column of the database as the name column for the login realm ( e.g request.login(id.toString(), password ) ) because I can not ensure uniqueness of the names in the user_name 开发者_如何学Ccolumn. However, using the built-in FORM authentication with Glassfishv3, it fails as it seems that the mechanism expect the column in the database to be a var char.

I am therefore looking for a way to populate at the same time that the user is created in the database a column string_id which value would be the id automatically generated translated into a String. I could do it in a subsequent operation but I was wondering if this could be done in one shot. I have tried @PrePersist but it seems that the id has not been generated at this stage. Or maybe there are smarter ways to perform the login ?

Thanks.


You can do it in one shot -

@Entity
public class A {
  @Id @GeneratedValue(strategy=SEQUENCE)
  public Integer getId() { return id; }
  public void setId(Integer id) { this.id = id; }
  private Integer id;

  public String getName() { return name; }
  private void setName(String name) { this.name = name; }
  private String name;
}

Serializable id = em.unwrap(Session.class).save(a);
a.setName("Why oh why redundancy? " + id);
em.flush();

Code copied from https://gist.github.com/786238

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜