Search by field in jpa play framework
I am using JPA in Play 2.2.1 . How can I find by field (other than id) in my model.
I have a User model
@Entity
public class User{
@Id
public Long id;
@Constraint.Required
@Email
public String email;
@Constraint.Required
public String password
}
And I want to check that the new user will not have the same email
but the find function is only for the primary key, so how can I find ByField (String email) in my model.
Any help would be greatly appreciated. Thanks to
+3
singhakash
source
to share
2 answers
There are many ways to do this. 1) NamedQuery 2) Java persistence query language 3) Criteria 4) Native queries ....
0
Nisanth kumar
source
to share
Well unique constraint will solve your problem here is the code
@Entity
@Table(uniqueConstraints={@UniqueConstraint(columnNames={"email"})})
public class User{
@Id
public Long id;
@Column(name = "email")
@Constraint.Required
@Email
public String email;
@Constraint.Required
public String password
}
0
lazy lord
source
to share