idailylife
idailylife

Reputation: 184

org.hibernate.QueryException: could not resolve property with column name already defined

This is the first time I use SpringMVC+Hibernate, so forgive me if I asked a noob question.

The relationship of database tables looks like this: Text in red color are my tables

And what I want to implement is searching for user_tasks which belongs to some user. So in the implementation of UserRepository I wrote this:

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

and the related UserTask class are defined as follows:

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

Also, I have classes of User and Task defined.

However, error occurs when I tried to call the function in the UserTaskRepoImpl exception

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)

Could anyone figure out why this happened? Thanks in advance!

Upvotes: 0

Views: 2213

Answers (1)

idailylife
idailylife

Reputation: 184

Okay I figured it out by myself. It's because that I confused the SQL column name (defined in @Column) with the HQL column name. The code

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

should be changed to

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

Upvotes: 3

Related Questions