Hello everyone. I am trying to learn JPA and I have a problem that I stucked in since 2 days.
I have a table named "User" includes id,email,password,username and status.
As you guess email and username columns are unique.
I also have a Class called User something like that :
@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;
Rest of this class is getters and setters.
I am trying to insert a value by 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 is the problem with the username constraint or the email unique constraint? While you can check my Attention start and end block, I am sure that you get my point : )
Thanks in advance.