JPA如何保存用户同时设置角色

@Data
@Entity
@Table()
@NoArgsConstructor
public class User extends BaseEntity  {
  private String realName;
  private String account;
  private String password;
  private String avatar;
  private Date birthday;
  //T-MAN  F-WOMAN
  private Boolean sex;
  private String email;
  private String phoneNum;
  private String telephoneNum;
  private Date lastLoginTime;
  private Boolean isAdmin;
  // 状态:T-在职,F-离职
  private Boolean status;

  @ManyToOne()
  @JoinColumn()
  private Role role; 

}
@Data
@Entity
@Table()
public class Role extends BaseEntity  {
  private String roleName;
  private String remark;
}

前端

       <!-- 角色 -->
       <nz-form-item>
        <nz-form-label nzSpan="7">职位</nz-form-label>
        <nz-form-control nzSpan="12" >
          <nz-select formControlName="role" required  >
            <nz-option nzLabel="超级管理员" [nzValue]="1"> </nz-option>
            <nz-option nzLabel="客户经理" [nzValue]="2"> </nz-option>
            <nz-option nzLabel="设计人员" [nzValue]="3"> </nz-option>
          </nz-select>
          <div nz-form-explain *ngIf="userErrorTpl.dirty&&userErrorTpl.invalid">必须填写!</div>
        </nz-form-control>
      </nz-form-item>
import { Role } from './role.type';

export default interface User {
  id: number;
  account: string
  realName: string;
  sex: boolean;
  email: string;
  phoneNum: number;
  telephoneNum: number;
  isAdmin: string;
  status: boolean;
  address: string;
  birthday: Date;
  role: Role
}

提交方法

 submitForm(value: {
    id: number; account: string;
    realName: string;
    sex: boolean;
    email: string;
    phoneNum: number;
    telephoneNum: number;
    isAdmin: string;
    status: boolean;
    address: string;
    birthday: Date;
    role: Role 
  }, ): void {
    // tslint:disable-next-line:forin
    for (const key in this.validateForm.controls) {
      this.validateForm.controls[key].markAsDirty();
      this.validateForm.controls[key].updateValueAndValidity();
    }
    console.log(value);
    this.userService.save(value).subscribe(res => {
      console.log(res)
      this.nzMessageService.create('success', '添加员工成功')
      this.router.navigate(['/home/user'])
    })
  }

报错信息

message: "JSON parse error: Cannot construct instance of `com.toy.start.entity.Role` (although at least one Creator exists): no int/Int-argument constructor/factory method to deserialize from Number value (3);

求助大佬,搞了2天了

你这异常是JSON反序列化异常,而且跟构造函数有关,我没用过 lombok 这种东西。你要是不熟悉也不建议您用。你尝试在 Role 类上添加 @NoArgsConstructor 注解试试看。

你的传参中的role给错值了,给成对象

怎么给。。。