Search code examples
javaspringhibernatehttp-status-code-500

HTTP Status 500 - Request processing failed; nested exception is org.hibernate.QueryException: could not resolve property


I'm new in Spring MVC and Hibernate and I encountered a problem. I've got Child table creating by Hibernate which is connected with User table (Many to One). When Hibernate creating Child table, there is added automatically 'user_object_id' - by 'user_object' (type User) these two tables are connected. But when I'd like to show to User only his children there is a problem:

HTTP Status 500 - Request processing failed; nested exception is org.hibernate.QueryException:
could not resolve property: user_object_id of: com.websystique.springmvc.model.Child

I get Child object from table in ChildDaoImpl class in findAllUserChilds method, and then map into ChildDTO (POJO object) in ChildController. Error is connected with 'user_object_id' in Child model, because this variable doesn't exist there, because it's creating automatically by Hibernate and returned in findAllUserChilds() by Criteria. How can I solve this problem?

Child:

@Entity
@Table(name= "Child")
public class Child implements Serializable
{ 

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private int id;

@Column (nullable=false)
private String first_name;

@Column (nullable=false)
private String last_name;

@Column (nullable=false)
private String birth_city;

@Column (nullable=false)
private String pesel;

@Column (nullable=false)
private String sex;

@Column (nullable=false)
private String blood_group;

@Column (nullable=false)
private String birth_date;


@ManyToOne
private User user_object;

public User getUser_object() {
    return user_object;
}

public void setUser_object(User user_object) {
    this.user_object = user_object;
}

(... 
some getters and setters
...)

}

ChildController:

@RequestMapping(value = "/mainpanel" , method = RequestMethod.GET, 
        headers = "accept=application/json")
public List<ChildDTO> listChild(HttpServletResponse request)
{
    Authentication auth =     SecurityContextHolder.getContext().getAuthentication();

    String username = auth.getName();

    User user = userService.findUserByUsername(username);

    List<Child> list = service.findAllUserChilds(user.getId());

    List<ChildDTO> result = new ArrayList<>();

    for (Child child : list)
    {
        result.add(ChildMapper.map(child));
    }

    return result;
}

ChildDaoImpl:

@Repository("ChildDao")
public class ChildDaoImpl extends AbstractDao<Integer, Child> implements     ChildDao {

@SuppressWarnings("unchecked")
public List<Child> findAllUserChilds(int user_id)
{
    Criteria criteria = createEntityCriteria();
    criteria.add(Restrictions.eq("user_object_id", user_id));
    return (List<Child>) criteria.list();
}

}

Solution

  • Child doesn't has the propetry user_object_id but user_object. You must use user_object.id (if id is the the name of attribute in User)