Вопрос

I'm looking for a solution to check a duplicate key before commit my BeanFieldGroup with JPAContainer. There's some a solution for this without using CriteriaQuery ? For example, when I execute the commit there's some way to check if the key already exists in database and returns a exception ?

I'm trying this

@Entity
@Table(name="curriculum")
public class Curriculum implements Serializable{
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue
    private Long idCurriculum;

    @Email
    @NotEmpty
    @NotNull
    @Size(max=250)
    @Column(unique=true)
    private String email;
}

/** BeanFieldGroup */   
private final BeanFieldGroup<Curriculum> binder = new BeanFieldGroup<Curriculum>(Curriculum.class);
private final Curriculum bean = new Curriculum();


/** datasources */
private final CustomJPAContainer<Curriculum> datasource = new CustomJPAContainer<Curriculum>(Curriculum.class);


private VerticalLayout buildLayout(){
    vLayout = new VerticalLayout();
    vLayout.setMargin(true);
    vLayout.setSpacing(true);
    binder.setItemDataSource(bean);             
    Field<?> field = null;

    //email unique key on database
    field = binder.buildAndBind("Email", "email", TextLower.class);
    email = (TextLower)binder.getField("email");
    email.setWidth("350px");
    email.setMaxLength(250);
    vLayout.addComponent(email);
            return vLayout;

    }  

    @Override
public void buttonClick(ClickEvent event) {
    if((event.getButton()) == btnSalvar){
        try {
            binder.commit();
        } catch (CommitException e) {
            Notification.show(e.getMessage(), 
                              Type.ERROR_MESSAGE);
        }

        try {                                
            datasource.addEntity(binder.getItemDataSource().getBean());
        } catch(Exception e) {              
                Notification.show(e.getMessage(), 
                                  Type.ERROR_MESSAGE);
                return;
        }
    }

After make commit I do: datasource.addEntity(binder.getItemDataSource().getBean()); if datasource.addEntity() doesn't work and does execute a generic Exception.

How do I check if email field already exist in database before commit without using CriteriaQuery, there are a way ?

Any idea ?

Это было полезно?

Решение

How about this (per Svetlin Zarev's suggestion):

binder.addCommitHandler(new CommitHandler()
{
    @Override
    public void preCommit(CommitEvent commitEvent) throws CommitException
    {
        String email = (String) commitEvent.getFieldBinder().getField("email").getValue();

        EntityManager em = /* retrieve your EntityManager */;

        List<?> results = em.createQuery("select idCurriculum from Curriculum where email = ?")
                            .setParameter(1, email)
                            .getResultList();

        if (results.size() > 0)
            throw new CommitException("Email already exists");
    }

    @Override
    public void postCommit(CommitEvent commitEvent) throws CommitException { }
});

Другие советы

Change your DB to have unique indexes. It will throw an exception when you try to insert duplicate data.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top