如何使用PrincipalExtractor将新用户添加到postgres数据库? (错误500)

时间:2019-06-01 21:55:26

标签: spring postgresql

我们正在制作一个可通过Google(OAuth2)登录的应用程序,登录后将通过电子邮件检查该用户是否已经在数据库中-如果不是,则添加该应用程序。问题是,我可以成功登录并收集我的数据,但是由于出现500个内部服务器错误,我无法将其添加到数据库中,我在以前的版本中使用了它,但是必须在新数据库上实现它。 / p>

我检查了要插入的值,因为错误500出现了ConstraintViolationException消息,并且看来idUser不是用UserEntity的新实例自动生成的,因为它保持为null并且idUser不是null字段。我还尝试通过userRepository.save(user)或UserService来保存用户,如下面的代码所示,将id严格设置为新值,但是我遇到了同样的错误。

PrincipalExtractor方法:

@Bean
    public PrincipalExtractor principalExtractor(UserRepository userRepository){
        return map -> {
            String email2 = (String)map.get("email");
            userService.setUserRepository(userRepository);
            UserEntity user = userRepository.findByEmail(email2);
            if(user == null){
                user = new UserEntity();
                user.setName((String)map.get("given_name"));
                user.setSurname((String)map.get("family_name"));
                user.setEmail((String)map.get("email"));
                user.setAvatar((String)map.get("picture"));
                userService.addUser(user);
            }
            return user;
        };
    }

UserEntity的代码段:

@Entity
@Table(name = "userr", schema = "public", catalog = "d2b6rsc8m7io0b")
public class UserEntity {
    private Integer idUser;
    private String login;
    private String name;
    private String surname;
    private Date birthDate;
    private String avatar;
    private String phone;
    private String email;
    private Timestamp timesstamp;
    private String password;
    private int cityIdCity;
    private String about;
    private int idCity;
    private Collection<MeetingEntity> meetingsByIdUser;
    private Collection<NoticeEntity> noticesByIdUser;
    private Collection<OpinionEntity> opinionsByIdUser;
    private Collection<OpinionEntity> opinionsByIdUser_0;
    private CityEntity cityByCityIdCity;


    public UserEntity() {
    }

    public UserEntity(Integer idUser, String login, String name, String surname, Date birthDate, String avatar, String phone, String email, Timestamp timesstamp, String password, int cityIdCity, String about, int idCity, Collection<MeetingEntity> meetingsByIdUser, Collection<NoticeEntity> noticesByIdUser, Collection<OpinionEntity> opinionsByIdUser, Collection<OpinionEntity> opinionsByIdUser_0, CityEntity cityByCityIdCity) {
        this.idUser = idUser;
        this.login = login;
        this.name = name;
        this.surname = surname;
        this.birthDate = birthDate;
        this.avatar = avatar;
        this.phone = phone;
        this.email = email;
        this.timesstamp = timesstamp;
        this.password = password;
        this.cityIdCity = cityIdCity;
        this.about = about;
        this.idCity = idCity;
        this.meetingsByIdUser = meetingsByIdUser;
        this.noticesByIdUser = noticesByIdUser;
        this.opinionsByIdUser = opinionsByIdUser;
        this.opinionsByIdUser_0 = opinionsByIdUser_0;
        this.cityByCityIdCity = cityByCityIdCity;
    }

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "id_user", nullable = false)
    public Integer getIdUser() {
        return idUser;
    }


    public void setIdUser(Integer idUser) {
        this.idUser = idUser;
    }

    @Basic
    @Column(name = "login", nullable = true, length = 127)
    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    @Basic
    @Column(name = "name", nullable = false, length = 127)
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Basic
    @Column(name = "surname", nullable = false, length = 127)
    public String getSurname() {
        return surname;
    }

    public void setSurname(String surname) {
        this.surname = surname;
    }

UserService:

@Service
public class UserService {


    @Autowired
    private UserRepository userRepository;

    public void setUserRepository(UserRepository userRepository) {
        this.userRepository = userRepository;
    }


    public List<UserEntity> getAllUser() {
        List<UserEntity> user = new ArrayList<>();
        userRepository.findAll().forEach(user::add);
        return user;
    }

    public UserEntity getUser(Integer id) {
        return userRepository.findById(id).get();
    }

    public void addUser(UserEntity user) {
        userRepository.save(user);
    }


    public void updateUser(Integer id, UserEntity user) {
        userRepository.save(user);
    }


    public void deleteUser(Integer id) {
        userRepository.deleteById(id);
    }
}

我没主意,任何帮助将不胜感激,谢谢!

0 个答案:

没有答案