-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 
Author Message
 Post subject: To catch the reason of the unique constraint on JPA
PostPosted: Mon Aug 02, 2010 3:55 am 
Newbie

Joined: Mon Aug 02, 2010 3:53 am
Posts: 1
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 :

Code:
@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.

Code:
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 1 post ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.