专栏'列不能为空

时间:2018-05-01 14:38:16

标签: java mysql hibernate spring-boot jpa

即时制作一个小型网络应用。在这个应用程序中,我可以注册新用户。 用户有一个rol,而rol可以有很多用户。

用户表

enter image description here

角色表

enter image description here

如果我进行INSERT,我希望列rol_id为" 1" (rol:用户)。 在使用DBMS时,一切都运行良好,但是当我使用Spring时它没有,因为我收到以下错误

  self.render('page',form_url=blobstore.create_upload_url('/upload'

我的代码是:

Rol表

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

}

DAO课程

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

实施UserService的类

public interface IUserService {

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

}

这是我的控制器

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

}

当我进行插入时,我想要将rol_id设置为1。 出于某种原因,只能在DBMS中使用,而不能在Spring中使用。

提前致谢。

1 个答案:

答案 0 :(得分:1)

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

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

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