列“column”不能为null

zbwhf8kr  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(535)

我正在制作一个小的网络应用程序。在这个应用程序中,我可以注册新用户。一个用户有一个rol,一个rol可以有多个用户。
用户表

角色表

如果我做了一个insert,我希望rol\u id列是“1”(rol:users)。在我使用dbms的时候,一切都很好,但是在我使用spring的时候就不行了,因为我得到了以下错误

2018-05-01 11:00:43.432 ERROR 9448 --- [nio-8080-exec-7] o.h.engine.jdbc.spi.SqlExceptionHelper   : Column 'rol_id' cannot be null

我的代码是:
rol表格

@Entity
@Table(name="roles")
public class Rol implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    @Column(name="id_rol", updatable = false, nullable = false)
    private Long id;

    @Column(name="rol")
    @NotEmpty
    private String rolType;

    @OneToMany(mappedBy= "rol")
    private List<User> users = new ArrayList<>();

    public Long getId() {
        return id;
    }

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

    public String getRol() {
        return rolType;
    }

    public void setRol(String rol) {
        this.rolType = rol;
    }

    public List<User> getUsers() {
        return users;
    }

    public void setUsers(List<User> users) {
        this.users = users;
    }

}

用户表

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

    private static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy= GenerationType.IDENTITY)
    @Column(name="id_user", updatable = false, nullable = false)
    private Long id;

    @Column(name="name")
    @NotEmpty
    @Length(min= 3, max= 25)
    private String name;

    @Column(name="surname")
    @NotEmpty
    @Length(min= 2, max=30)
    private String surname;

    @Column(name="email")
    @NotEmpty
    @Email
    @Length(min=5, max=30)
    private String email;

    @Column(name="birthdate", nullable = true)
    private String birthdate;

    @Column(name="created_at")
    @Temporal(TemporalType.DATE)
    @DateTimeFormat(pattern="yyyy-MM-dd")
    private Date createdAt = new Date();

    @Column(name="gender")
    @NotEmpty
    @Length(min=4, max=6)
    private String gender;

    @Column(name="username")
    @NotEmpty
    @Length(min=3, max=20)
    private String username;

    @Column(name="pass")
    @NotEmpty
    @Length(min=8, max=30)
    private String password;

    @ManyToOne
    @JoinColumn(name="rol_id")
    private Rol rol;

    public Long getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public String getSurname() {
        return surname;
    }

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

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getBirthdate() {
        return birthdate;
    }

    public void setBirthdate(String birthdate) {
        this.birthdate = birthdate;
    }

    public Date getCreatedAt() {
        return createdAt;
    }

    public void setCreatedAt(Date createdAt) {
        this.createdAt = createdAt;
    }

    public String getGender() {
        return gender;
    }

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

    public String getUsername() {
        return username;
    }

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

    public String getPassword() {
        return password;
    }

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

    /* MODOFIQUE ESTO ACA, NO SE SI ESTA BIEN */
    public String getRol() {
        return rol.getRol();
    }

    public void setRol(Rol rol) {
        this.rol = rol;
    }

}

道类

public interface IUserDAO extends CrudRepository<User, Long>{
}

用户服务接口

public interface IUserService {

    public List<User> findAll();
    public void save(User user);
    public User findOne(Long id);

}

实现userservice的类

@Service
public class UserServiceImpl implements IUserService{

    @Autowired
    IUserDAO userDAO;

    @Override
    @Transactional(readOnly = true)
    public List<User> findAll() {
        return userDAO.findAll();
    }

    @Override
    @Transactional
    public void save(User user) {
        userDAO.save(user);

    }

    @Override
    @Transactional(readOnly = true)
    public User findOne(Long id) {
        return userDAO.findOne(id);
    }

}

这是我的控制器

@Controller
@SessionAttributes("user")
public class UserController {

    Constants c = new Constants();

    @Autowired
    IUserService userService;

    @RequestMapping(value= "user/add", method = RequestMethod.GET)
    public String addUser(Model model) {
        User user = new User();
        model.addAttribute("user", user);
        model.addAttribute(c.TITLE, "Add User");
        model.addAttribute(c.ADD_USER);
        return "user-add";
    }

    @RequestMapping(value= "user/add", method = RequestMethod.POST)
    public String addUser(@Valid User user, BindingResult result, Model model, RedirectAttributes ra, SessionStatus status) {
        if(result.hasErrors()) {
            return "redirect:/user/add";
        }

        try {
            userService.save(user);
            status.setComplete();
        } catch(Exception e) {
            return "redirect:/user/list";
        }
        return "user-add";
    }

    @RequestMapping(value= "user/list", method = RequestMethod.GET)
    public String listUsers(Model model) {
        model.addAttribute("users", userService.findAll());
        model.addAttribute(c.TITLE, "Users List");
        model.addAttribute(c.LIST_USER);
        return "user-list";
    }

}

我对百里香的看法呢

<form th:action="@{/user/add}" method="POST" th:object="${user}" style="width: 70%;">

    <div class="form-group">
        <label for="inputName" class="text-uppercase font-weight-bold">Name</label>
        <input type="text" class="form-control" id="inputName" th:field="*{name}" autocomplete="off">
    </div>

    <div class="form-group">
        <label for="inputSurname" class="text-uppercase font-weight-bold">Surname</label>
        <input type="text" class="form-control" id="inputSurname" th:field="*{surname}" autocomplete="off">
    </div>

    <div class="form-group">
        <label for="inputEmail" class="text-uppercase font-weight-bold">Email</label>
        <input type="email" class="form-control" id="inputEmail" th:field="*{email}" autocomplete="off">
    </div>

    <div class="form-group">
        <label for="inputBirthdate" class="text-uppercase font-weight-bold">Birthdate</label>
        <input type="date" class="form-control" id="inputBirthdate" th:field="*{birthdate}" autocomplete="off">
    </div>

    <div class="form-group mb-5">
        <label for="inputGender" class="text-uppercase font-weight-bold">Gender</label>
        <select type="date" class="form-control" id="inputGender" th:field="*{gender}">
            <option th:value="other">Other</option>
            <option th:value="male">Male</option>
            <option th:value="female">Female</option>
        </select>
    </div>

    <div class="form-group">
        <label for="inputUsername" class="text-uppercase font-weight-bold">Username</label>
        <input type="text" class="form-control" id="inputUsername" th:field="*{username}" autocomplete="off">
    </div>

    <div class="form-group">
        <label for="inputPassword" class="text-uppercase font-weight-bold">Password</label>
        <input type="password" class="form-control" id="inputPassword" th:field="*{password}" autocomplete="off">
    </div>

    <button type="submit" class="btn btn-primary w-100">
        Add User
    </button>
</form>

我只想在插入时将rol\u id设置为1。由于某些原因,只在dbms中工作,而在spring中不工作。
提前谢谢。

91zkwejq

91zkwejq1#

我想我不明白。您计划如何填充 rol_id 未设置 user.rol 一个非空值?这是jpa,您不直接处理连接列和表,而是在对象之间建立关联。
如果希望新创建的用户具有 id=1 默认情况下,您应该相应地修改服务:

@Override
@Transactional
public void save(User user) {
    user.setRol(roleDao.getOne(1l));
    userDAO.save(user);
}

相关问题