java 如何使用 jpa spring 找出电子邮件是否已经存在并向前端发送一些错误消息
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32801008/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to find out if an email already exist with jpa spring and sending some error messag to the front end
提问by Greg
So i have a simple UsersDao
所以我有一个简单的 UsersDao
public interface UserDao extends JpaRepository<User, Long> {
}
And inside my user controller i want to do something like this :
在我的用户控制器中,我想做这样的事情:
@RequestMapping(value = "/register",method = RequestMethod.POST)
public void addUser(@RequestBody User user) {
//How do i check if user already exist with email instead of id
// i managed to do this but can i search on something else than the id
User user1 = userDao.findOne(1);
if (user.getEmail().equals(user1.getEmail()))
{
// And how should i give one error to the front end if the email
//already exist I'm using angular js
}
else {
userDao.save(user);
}
}
I also have some extra questions on this topic:
我还有一些关于这个主题的额外问题:
Somethings that are not clear are following. I have done a small tutorial on jpa but there they use:
不清楚的东西还在后面。我在 jpa 上做了一个小教程,但他们在那里使用:
EntityManager, EntityTransaction
实体管理器、实体交易
Note : when using EntityManagerFactory it goes as follow :
注意:使用 EntityManagerFactory 时,如下所示:
EntityManagerFactory emf = null,
//Then they use EntityManagerFactory
emf = Persistence.createEntityManagerFactory("SomeValue")
//where can i get "someValue" When using application .properties
//because in the example they use xml but can't find the right properties in application.properties
Or do i not need to use these in springboot
或者我不需要在springboot中使用这些
Sorry for all these question. I really want to get into spring but somethings are still a bit unclear at this point ;)
很抱歉所有这些问题。我真的很想进入春天,但在这一点上还有点不清楚;)
回答by ufuoma
You can do the following:
您可以执行以下操作:
Assuming Userhas an attribute email, define a method in the interface like this to generate a dynamic query:
假设User有一个属性email,在接口中定义一个方法是这样生成动态查询的:
public interface UserDao extends JpaRepository<User, Long> {
public User findByEmail(String email);
}
Then you can find a user by email. If nullis returned, no user with the given email exists. Also, within the Userentity class, you can define an annotation to ensure that emailis unique like this:
然后您可以通过电子邮件找到用户。如果null返回,则不存在具有给定电子邮件的用户。此外,在User实体类中,您可以定义一个注释以确保它email是唯一的,如下所示:
public class User {
....
@Column(unique=true)
String email;
}
回答by asm0dey
You have 2 options:
您有 2 个选择:
- Use method
User findByEmail(String email);in repository interface. - Use method like
@Query("SELECT COUNT(u.id) FROM User u WHERE u.email=:email) Long countUsersWithEmail(String email);Than it's obvious how to use rusults of these queries. I would use 2nd choice because of smaller overhead.
User findByEmail(String email);在存储库界面中使用方法。- 使用像
@Query("SELECT COUNT(u.id) FROM User u WHERE u.email=:email) Long countUsersWithEmail(String email);这样的方法 很明显如何使用这些查询的结果。由于开销较小,我会使用第二个选择。

