gpt4 book ai didi

java - 列 'column' 不能为空

转载 作者:行者123 更新时间:2023-11-29 10:15:57 25 4
gpt4 key购买 nike

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

用户表

enter image description here

角色表

enter image description here

如果我进行插入,我希望列 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>{
}

用户服务接口(interface)

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

@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。

提前致谢。

最佳答案

我想我没听懂。您打算如何在不将 user.rol 设置为非空值的情况下填充 rol_id ?这就是 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com