質問

I'm looking for how to use clauses AND or OR to do a login in my application. With SQL I use "SELECT * FROM person WHERE email = ? AND password = ?", but with JPAContainer I don't know how to do this.

//Beans
@Entity
public class Person {
    @Id
    @GeneratedValue
    private Integer id;

    @NotNull        
    @Size(min=5, max=50)
    private String name;

    @Email
    private Email email;

    @Size(min=8,max=8)
    private String password;

    @ManyToOne
    private PersonType personType;
}

@Entity
public class PersonType {
    @Id
    @GeneratedValue
    private Integer id;

    @NotNull        
    @Size(min=5, max=50)
    private String personType;
}

How to I can check Email and Password of Person and return a List using JPAContainer ?

役に立ちましたか?

解決

If I understood you correctly, you'd like to validate a login by checking both the email and the password fields. I'm not sure why you need a JPAContainer instead of a simple CriteriaQuery, but here's the answer to your question (off the top of my head):

JPAContainer<Person> container = 
     JPAContainerFactory.<Person>makeReadOnly(Person.class, entityManager);

container.addContainerFilter(new Compare.Equal("email", emailInput));
container.addContainerFilter(new Compare.Equal("password", passwordInput));

// Query container size, eg
if(container.size()==1) { ... }

If you add multiple container filters in this manner, they will implicitly be understood as the intersection thereof, ie. AND.

If you want the union of filter, ie. OR, you'll have to build it yourself as such (if that makes any sense is at your discretion):

Filter filter = new Compare.OR(new Compare.Equal("email", emailInput), new Compare.Equal("password", passwordInput));
container.addContainerFilter(filter);

However, I'll repeat that using a plain CriteriaQuery may be more appropriate.


Moreover —and don't take this lightly— from your Person entity, I infer that you're storing the passwords in clear in the database which is a big no-no, and

  1. can put your users at risk if the database is compromised, and
  2. could be a rough blow to your own reputation if your code is audited/reviewed.

EDIT:

With CriteriaQuery, you could something like:

CriteriaBuilder cb = entityManager.getCriteriaBuilder();
CriteriaQuery<Long> q = qb.createQuery(Long.class);
Root<Person> root = q.from(Person.class);
Path<String> email = root.get("email");
Path<String> password = root.get("password");

cq.select(cb.count(root)).where(
    cb.and(
        cb.equal(email, emailInput),
        cb.equal(password, passwordInput)
    )
);

return entityManager.createQuery(cq).getSingleResult();

Replace cb.and(...) with cb.or(...) if you so wish. HTH.

他のヒント

I managed to do.

I did this.

EntityManager em = datasource.getEntityProvider().getEntityManager();       
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Person> c = cb.createQuery(Person.class);
Root<Person> person = c.from(Person.class);
c.where(
    cb.and(
cb.equal(person.get("email"), email.getValue()),
cb.equal(person.get("password"), password.getValue()))
);
TypedQuery tq = em.createQuery(c);
List<Person> lista = tq.getResultList();

And now works !

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top