gpt4 book ai didi

java - 组织.hibernate.QueryException : could not resolve property with column name already defined

转载 作者:太空宇宙 更新时间:2023-11-03 11:48:12 27 4
gpt4 key购买 nike

我是第一次使用SpringMVC+Hibernate,如果问了菜鸟问题请见谅

数据库表的关系是这样的: Text in red color are my tables

我想要实现的是搜索属于某个用户user_tasks。所以在 UserRepository 的实现中我这样写:

@Repository("userTaskRepo")
public class UserTaskRepoImpl extends AbstractDao<Integer, UserTask> implements UserTaskRepository {

public List<UserTask> getByUserId(int userId, int startTaskId, int size) {
List<UserTask> userTasks = getSession().createCriteria(UserTask.class)
.add(Restrictions.eq("user_id", userId))
.add(Restrictions.ge("task_id", startTaskId))
.setMaxResults(size)
.list();
return userTasks;
}
}

和相关的UserTask类定义如下:

@Entity
@Table(name = "usertask")
public class UserTask {
//public enum TaskState {CLAIMED, FINISHED, EXPIRED};
public static int TYPE_CLAIMED = 0;
public static int TYPE_FINISHED = 1;
public static int TYPE_EXPIRED = 2;

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Integer id;

@Column(name = "user_id", nullable = false)
private Integer userId;

@Column(name = "task_id", nullable = false)
private Integer taskId;

@Column(name = "curr_usermicrotask_id")
private Integer currUserMicrotaskId;

@Column(name = "state", nullable = false)
private Integer state;

//...some getters and setters
}

此外,我还定义了 UserTask 类。

但是,当我尝试调用 UserTaskRepoImpl 中的函数时发生错误异常

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.hibernate.QueryException: could not resolve property: user_id of: edu.inlab.models.UserTask
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:979)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:858)
javax.servlet.http.HttpServlet.service(HttpServlet.java:622)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843)
javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

谁能弄清楚为什么会这样?提前致谢!

最佳答案

好吧,我自己想通了。这是因为我混淆了 SQL 列名(在 @Column 中定义)和 HQL 列名。代码

.add(Restrictions.eq("user_id", userId))
.add(Restrictions.ge("task_id", startTaskId))

应该改为

.add(Restrictions.eq("userId", userId))
.add(Restrictions.ge("userId", startTaskId))

关于java - 组织.hibernate.QueryException : could not resolve property with column name already defined,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37204964/

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