I am trying to learn JPA and I have a problem that I am stuck with from two days.
I have a table called "User" that includes an identifier, email address, password, username and status.
Do you think the columns of letters and username are unique.
I also have a class called User, something like this:
@Entity @Table(name = "user", uniqueConstraints = @UniqueConstraint(columnNames = {"username", "email"})) public class User { @Id @GeneratedValue(strategy=GenerationType.AUTO) private int id; @Column(name="email", nullable=false) private String email; @Column(name="password", nullable=false) private String password; @Column(name="username", nullable=false) private String username; @Column(name="status", nullable=false) private String status;
The rest of this class is getters and seters.
I am trying to insert a value using JPA with Hibernate.
try { em = jpaResourceBean.getEMF().createEntityManager(); em.getTransaction().begin(); user.setStatus(UserStatus.PENDING.toString()); em.persist(user); em.getTransaction().commit(); logger.info("User " + user.getUsername() + " has been registered"); // Attention starts } catch (XXXXXX) { if (XXXXX.getYYY().equals("ZZZZZ")) logger.info("User name is already exist"); if (XXXXX.getMMM().equals("LLLLL")) logger.info("Email is already exist"); } // Attention end
All I want to know: how can I understand the problem with the username restriction or the unique email restriction? While you can check the start and end block of Attention, I'm sure you will get my point :)
Thanks in advance.
source share