Question

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 column. 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.

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top