gpt4 book ai didi

java - 尝试连接用户时出现 Hibernate 异常 - 字段 'username' 没有默认值

转载 作者:行者123 更新时间:2023-11-30 22:25:21 25 4
gpt4 key购买 nike

我试图让一个用户关注另一个用户,但每次都出现错误。当我通过 SQL 将它插入我的数据库时,它可以正常工作,所以我在 Java 端遇到错误。这是我的代码:

@Id
@GeneratedValue
private int id;

@Column(name = "roomie_username")
private String roomie_username;

@ManyToOne(optional=false)
@JoinColumn(name = "username", referencedColumnName="username", insertable=false, updatable=false)
private User user;

public Roomie() {
this.user = new User();
}

public Roomie(int id, String roomie_username, User user) {
super();
this.id = id;
this.roomie_username = roomie_username;
this.user = user;
}

我的 Controller :

@RequestMapping("/user/{username}")
public String showUser(@PathVariable String username, Model model,
Principal principal) {
User user = usersService.getUser(username);

model.addAttribute("user", user);

Roomie roomie = new Roomie();
roomie.setRoomie_username(username);

model.addAttribute("roomie", roomie);

List<Roomie> roomies = roomieService.getRoomies(username);

model.addAttribute("roomies", roomies);


return "user";

}

@RequestMapping(value = "/addRoomie", method = RequestMethod.POST)
public String addRoomie(Principal principal,
@Validated(value = FormValidationGroup.class) Roomie roomie)
throws IOException {

String currentUsername = principal.getName();
if (currentUsername.equals(roomie.getRoomie_username())) {
return "cannotadd";
}
else {
roomie.getUser().setUsername(currentUsername);

if(roomieService.exists(roomie.getRoomie_username(), currentUsername)) {

return "cannotadd";
} else {
roomieService.saveOrUpdate(roomie);

return "roomieadded";
}

}

在我实际尝试添加室友的 JSP 中:

<sf:form id="details" method="post"
action="${pageContext.request.contextPath}/addRoomie"
commandName="roomie">
<sf:input type="hidden" name="roomie_username"
path="roomie_username" />
<input class="btn btn-default" class="control"
value="Connect to user" type="submit" />
</sf:form>

我的数据库结构:

DB

这是我的错误:

org.springframework.orm.hibernate3.HibernateJdbcException: JDBC exception on Hibernate data access: SQLException for SQL [insert into roomie (roomie_username) values (?)]; SQL state [HY000]; error code [1364]; could not insert: [com.spring.web.dao.Roomie]; nested exception is org.hibernate.exception.GenericJDBCException: could not insert: [com.spring.web.dao.Roomie]

如果有人能提供帮助,我将不胜感激,因为我已经坚持了很长时间。

最佳答案

我们可以启用 hibernate 层中的日志记录以查看传递给查询的实际输入。请引用网址https://docs.jboss.org/hibernate/orm/3.5/reference/en/html/session-configuration.html .您可以通过 log4j 属性或 hibernate XML 进行配置。它将帮助您调试任何问题

关于java - 尝试连接用户时出现 Hibernate 异常 - 字段 'username' 没有默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35414964/

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