Spring 调用Repository的sava方法为什么会调用Entity的set方法?

Spring MVC + Spring Data Jpa + Spring Security + Tomcat,底层封装了hibernate
在我的Entity中有一个User类,User类里有一个密码的字段。
在 密码 的构造函数和set方法里面都对明文密码进行了加密操作。

问题出在我存入的时候。在Controller里面调用构造函数new了一个UserEntity,
然后调用一个Repository类的save方法。经过调试后我发现save方法会再调用我的
set方法,这就相当于在构造函数里加密了一次,又在set里加密了一次。

但是问题是前一阵用Spring-Boot学习也是在构造函数和set里面都对密码进行了加密。
但是并没有出现这样的问题。因此很好奇,求教各位大佬了。

代码贴在下面:

这是UserEntity的代码:

 package com.carpool.domain;

import com.carpool.website.service.EncryptionService;

import javax.persistence.*;
import java.io.Serializable;
import java.util.Collection;


/**
 * Created by qi on 2016/11/26.
 */
@Entity
@Table
public class UserEntity implements Serializable{
    private String id;
    private String username;


    private String password;
    private byte gender;
    private double credit;
    private String alipay;
    private int coins;
    private Integer receivedComments;



    private Integer carpoolingCount;
    private String qqAccount;
    private String wechatAccount;
    private Collection<ChatRecordEntity> sendedChatRecord;
    private Collection<CommentEntity> commentsRecieved;
    private Collection<CommentEntity> commentsSended;
    private Collection<PaymentRecordEntity> paymentRecievedRecords;
    private Collection<PaymentRecordEntity> paymentSendRecords;
    private Collection<RoomEntity> ownRoom;
    private Collection<RoomEntity> hasPaysRoom;
    private Collection<RoomEntity> userParticipateRooms;

    public UserEntity() {
    }

    public UserEntity(String id, String username, String password, byte gender, double credit, String alipay,
                      int coins, String qqAccount, String wechatAccount) {
        this.id = id;
        this.username = username;
        this.gender = gender;
        this.credit = credit;
        this.alipay = alipay;
        this.coins = coins;
        this.qqAccount = qqAccount;
        this.wechatAccount = wechatAccount;
        //pw encryption

        EncryptionService enp = new EncryptionService();
        try{
            this.password = enp.encipher(password) + enp.encipher(id);
        }catch(Exception e){
            e.printStackTrace();
        }


    }

    @Id
    @Column(name = "id", nullable = false, length = 10, unique = true)
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    @Basic
    @Column(name = "username", nullable = false, length = 10)
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }


    @Basic
    @Column(name = "password", nullable = false, length = 225)
    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        EncryptionService enp = new EncryptionService();
        try{
            this.password = enp.encipher(password) + enp.encipher(id);
        }catch(Exception e){
            e.printStackTrace();
        }

    }

    @Basic
    @Column(name = "gender", nullable = false)
    public byte getGender() {
        return gender;
    }

    public void setGender(byte gender) {
        this.gender = gender;
    }

    @Basic
    @Column(name = "credit", nullable = false, precision = 0)
    public double getCredit() {
        return credit;
    }

    public void setCredit(double credit) {
        this.credit = credit;
    }

    @Basic
    @Column(name = "alipay", nullable = true, length = 20)
    public String getAlipay() {
        return alipay;
    }

    public void setAlipay(String alipay) {
        this.alipay = alipay;
    }

    @Basic
    @Column(name = "coins", nullable = false)
    public int getCoins() {
        return coins;
    }

    public void setCoins(int coins) {
        this.coins = coins;
    }

    @Basic
    @Column(name = "QQ_account", nullable = false, length = 20)
    public String getQqAccount() {
        return qqAccount;
    }

    public void setQqAccount(String qqAccount) {
        this.qqAccount = qqAccount;
    }

    @Basic
    @Column(name = "wechat_account", nullable = false, length = 20)
    public String getWechatAccount() {
        return wechatAccount;
    }

    public void setWechatAccount(String wechatAccount) {
        this.wechatAccount = wechatAccount;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        UserEntity that = (UserEntity) o;

        if (gender != that.gender) return false;
        if (Double.compare(that.credit, credit) != 0) return false;
        if (coins != that.coins) return false;
        if (id != null ? !id.equals(that.id) : that.id != null) return false;
        if (username != null ? !username.equals(that.username) : that.username != null) return false;
        if (password != null ? !password.equals(that.password) : that.password != null) return false;
        if (alipay != null ? !alipay.equals(that.alipay) : that.alipay != null) return false;
        if (qqAccount != null ? !qqAccount.equals(that.qqAccount) : that.qqAccount != null) return false;
        if (wechatAccount != null ? !wechatAccount.equals(that.wechatAccount) : that.wechatAccount != null)
            return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result;
        long temp;
        result = id != null ? id.hashCode() : 0;
        result = 31 * result + (username != null ? username.hashCode() : 0);
        result = 31 * result + (password != null ? password.hashCode() : 0);
        result = 31 * result + (int) gender;
        temp = Double.doubleToLongBits(credit);
        result = 31 * result + (int) (temp ^ (temp >>> 32));
        result = 31 * result + (alipay != null ? alipay.hashCode() : 0);
        result = 31 * result + coins;
        result = 31 * result + (qqAccount != null ? qqAccount.hashCode() : 0);
        result = 31 * result + (wechatAccount != null ? wechatAccount.hashCode() : 0);
        return result;
    }

    @OneToMany(mappedBy = "sender")
    public Collection<ChatRecordEntity> getSendedChatRecord() {
        return sendedChatRecord;
    }

    public void setSendedChatRecord(Collection<ChatRecordEntity> chatrecordsById) {
        this.sendedChatRecord = chatrecordsById;
    }


    @OneToMany(mappedBy = "targetUser")
    public Collection<CommentEntity> getCommentsRecieved() {
        return commentsRecieved;
    }

    public void setCommentsRecieved(Collection<CommentEntity> commentsById) {
        this.commentsRecieved = commentsById;
    }

    @OneToMany(mappedBy = "sourceUser")
    public Collection<CommentEntity> getCommentsSended()
    {
        return commentsSended;
    }
    private void setCommentsSended(Collection<CommentEntity> commentsSended)
    {
        this.commentsSended = commentsSended;
    }

    @OneToMany(mappedBy = "targetUser")
    public Collection<PaymentRecordEntity> getPaymentRecievedRecords() {
        return paymentRecievedRecords;
    }

    public void setPaymentRecievedRecords(Collection<PaymentRecordEntity> paymentrecordsById) {
        this.paymentRecievedRecords = paymentrecordsById;
    }

    @OneToMany(mappedBy = "sourceUser")
    public Collection<PaymentRecordEntity> getPaymentSendRecords() {
        return paymentSendRecords;
    }

    public void setPaymentSendRecords(Collection<PaymentRecordEntity> paymentrecordsById_0) {
        this.paymentSendRecords = paymentrecordsById_0;
    }

    @OneToMany(mappedBy = "host")
    public Collection<RoomEntity> getOwnRoom() {
        return ownRoom;
    }

    public void setOwnRoom(Collection<RoomEntity> roomsById) {
        this.ownRoom = roomsById;
    }

    @OneToMany(mappedBy = "payer")
    public Collection<RoomEntity> getHasPaysRoom() {
        return hasPaysRoom;
    }

    public void setHasPaysRoom(Collection<RoomEntity> hasPaysRoom) {
        this.hasPaysRoom = hasPaysRoom;

    }

    @ManyToMany
    @JoinTable(name = "user_rooms",
    joinColumns = {@JoinColumn(name = "uid")},
    inverseJoinColumns = {@JoinColumn(name = "rid")})
    public Collection<RoomEntity> getUserParticipateRooms() {
        return userParticipateRooms;
    }

    public void setUserParticipateRooms(Collection<RoomEntity> userParticipateRooms) {
        this.userParticipateRooms = userParticipateRooms;
    }

    @Column(nullable = false)
    public Integer getReceivedComments() {
        return receivedComments;
    }

    public void setReceivedComments(Integer receivedComments) {
        this.receivedComments = receivedComments;
    }

    @Column
    public Integer getCarpoolingCount() {
        return carpoolingCount;
    }

    public void setCarpoolingCount(Integer carpoolingCount) {
        this.carpoolingCount = carpoolingCount;
    }
}

这是一个测试的TestContrller,主要看insertData方法:

 package com.carpool.website.controller;

import com.carpool.domain.UserEntity;
import com.carpool.website.dao.UserEntityRepository;
import com.carpool.website.service.ChatRecordService;
import com.carpool.website.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * Created by qi on 2016/11/26.
 */
@RestController
@RequestMapping("/Test")
public class TestController {


    @Autowired
    ChatRecordService chatRecordService;
    @Autowired
    UserService userService;


    @RequestMapping("/deleteChatRecord")
    String deleteChatRecordTest(){
        try{
            this.chatRecordService.deleteChatRecordById(10);
            return "True";
        }catch(Exception e){
            return "False";
        }
    }

    @RequestMapping("/insert")
    boolean insertData(){
        UserEntity userEntity = new UserEntity("12223","asssw","123456", (byte)2793314121856183",12,"123456","123456");
        userEntity.setReceivedComments(0);
        try{
            this.userService.saveStudent(userEntity);
        }catch(Exception e){

        }

        return true;
    }

}

实体搞这么复杂不太好吧!

我看了你代码,有些问题,你insertData方法实例化UserEntity对象,然后这个对象里面已经加密过一次密码了,如图所示:
图片说明

然后你set方法那边又加密了一次密码,如图所示:
图片说明

所以相当于加密了一次后又再加密了一次。

那个UserEntity的有参构造方法就直接写this.password=password;就行了,还做一次加密操作干嘛?

不知道说得对不对,还是你自己本地断点调试一下吧!说错了,请见谅,还是靠自己!