使用OneToOne映射在春季休眠时反序列化异常

时间:2019-02-06 07:53:18

标签: java mysql hibernate spring-boot one-to-one

我正在使用带有hibernate的Spring boot 2.1.2,并且我有两个简单的模型类,例如用户和用户帐户,并且这两个模型与oneToOne映射连接。当我们致电用户时,我还需要获取用户帐户。我只是使用oneToOne映射将用户帐户模型映射到用户模型。但是当我打电话给用户时,却遇到了一个反序列化异常。

用户模型

@Injectable()
export default class AuthGuard {
  constructor (protected readonly config: ConfigService) {
  }
  /*
  ...
  */
}

用户帐户模型

import java.io.Serializable;
import java.util.Calendar;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;


@Entity
@Table(name = "tbl_user")
public class User implements Serializable{

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Integer id;

    @OneToOne(fetch = FetchType.EAGER)
    @JoinColumn(name = "user_account_id")
    UserAccount userAccount; 

    @Column(name = "first_name",nullable = false)
    private String firstName;


    @Column(name = "active",nullable = false)
    private Integer active;

    @Column(name = "created_date",nullable = false)
    private Calendar createdDate;

    @Column(name = "created_by",nullable = false)
    private Integer created_by;

    @Column(name = "updated_date")
    private Calendar updatedDate;

    @Column(name = "updated_by")
    private Integer updated_by;



    public User() {
        super();
        // TODO Auto-generated constructor stub
    }

    public User(Integer id, UserAccount userAccount, String firstName, Integer active, Calendar createdDate,
            Integer created_by, Calendar updatedDate, Integer updated_by) {
        super();
        this.id = id;
        this.userAccount = userAccount;
        this.firstName = firstName;
        this.active = active;
        this.createdDate = createdDate;
        this.created_by = created_by;
        this.updatedDate = updatedDate;
        this.updated_by = updated_by;
    }



    public Integer getId() {
        return id;
    }

    public UserAccount getUserAccount() {
        return userAccount;
    }



    public void setUserAccount(UserAccount userAccount) {
        this.userAccount = userAccount;
    }


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

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }


    public Integer getActive() {
        return active;
    }

    public void setActive(Integer active) {
        this.active = active;
    }

    public Calendar getCreatedDate() {
        return createdDate;
    }

    public void setCreatedDate(Calendar createdDate) {
        this.createdDate = createdDate;
    }

    public Integer getCreated_by() {
        return created_by;
    }

    public void setCreated_by(Integer created_by) {
        this.created_by = created_by;
    }

    public Calendar getUpdatedDate() {
        return updatedDate;
    }

    public void setUpdatedDate(Calendar updatedDate) {
        this.updatedDate = updatedDate;
    }

    public Integer getUpdated_by() {
        return updated_by;
    }

    public void setUpdated_by(Integer updated_by) {
        this.updated_by = updated_by;
    }


}

属性文件

import java.io.Serializable;
import java.util.Calendar;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;


@Entity
@Table(name = "tbl_user_account")
public class UserAccount implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id")
    private Integer id;

    @Column(name = "usere_name",nullable = false)
    UserAccount usereName;

    @Column(name = "password", nullable = false)
    private String password;

    @Column(name = "active",nullable = false)
    private Integer active;

    @Column(name = "created_date",nullable = false)
    private Calendar createdDate;

    @Column(name = "created_by",nullable = false)
    private Integer created_by;

    @Column(name = "updated_date")
    private Calendar updatedDate;

    @Column(name = "updated_by")
    private Integer updated_by;



    public UserAccount(Integer id, UserAccount usereName, String password,Integer active,
            Calendar createdDate, Integer created_by, Calendar updatedDate, Integer updated_by) {
        super();
        this.id = id;
        this.usereName = usereName;
        this.password = password;
        this.active = active;
        this.createdDate = createdDate;
        this.created_by = created_by;
        this.updatedDate = updatedDate;
        this.updated_by = updated_by;
    }

    public UserAccount() {
        super();
        // TODO Auto-generated constructor stub
    }

    public Integer getId() {
        return id;
    }

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

    public UserAccount getUsereName() {
        return usereName;
    }

    public void setUsereName(UserAccount usereName) {
        this.usereName = usereName;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }


    public Integer getActive() {
        return active;
    }

    public void setActive(Integer active) {
        this.active = active;
    }

    public Calendar getCreatedDate() {
        return createdDate;
    }

    public void setCreatedDate(Calendar createdDate) {
        this.createdDate = createdDate;
    }

    public Integer getCreated_by() {
        return created_by;
    }

    public void setCreated_by(Integer created_by) {
        this.created_by = created_by;
    }

    public Calendar getUpdatedDate() {
        return updatedDate;
    }

    public void setUpdatedDate(Calendar updatedDate) {
        this.updatedDate = updatedDate;
    }

    public Integer getUpdated_by() {
        return updated_by;
    }

    public void setUpdated_by(Integer updated_by) {
        this.updated_by = updated_by;
    }


}

1 个答案:

答案 0 :(得分:1)

问题似乎出在UserAccount类中,您在其中将 usereName 字段定义为UserAccount类型。

我认为应该是String