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

用户表

java - 列“column”不能为空-LMLPHP

角色表

java - 列“column”不能为空-LMLPHP

如果我进行插入,我希望rol_id列为“ 1”(rol:用户)。
在使用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


我的代码是这样的:

角色表

@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;
    }

}


DAO类

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_id设置为1。
由于某种原因,仅在DBMS中有效,而在Spring中则无效。

提前致谢。

最佳答案

我不认为我会遵循。您打算如何在不将rol_id设置为非空值的情况下填充user.rol?这是JPA,您不直接处理联接列和表,而是在对象之间建立关联。

如果希望新创建的用户具有默认分配了id=1的角色,则应相应地修改服务:

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

关于java - 列“column”不能为空,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50118477/

10-13 02:53