HTTP 状态 500 - 请求处理失败;嵌套异常是 org.hibernate.QueryException:无法解析 属性
HTTP Status 500 - Request processing failed; nested exception is org.hibernate.QueryException: could not resolve property
我是 Spring MVC 和 Hibernate 的新手,我遇到了一个问题。我有 Child table 由 Hibernate 创建,它与用户 table (多对一)连接。当 Hibernate 创建 Child table 时,会自动添加 'user_object_id' - 通过 'user_object' (键入用户)这两个 table 已连接。但是,当我只想向用户显示他的 children 时,出现了问题:
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
我在ChildDaoImpl class的findAllUserChilds方法中从table得到Childobject,然后映射到Child控制器中的 DTO (POJO object)。错误与 Child 模型中的 'user_object_id' 相关,因为该变量在那里不存在,因为它由 Hibernate 自动创建并由 Criteria 在 findAllUserChilds() 中返回。我该如何解决这个问题?
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
...)
}
Child控制器:
@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();
}
}
Child 没有属性 user_object_id 但 user_object。
您必须使用 user_object.id(如果 id 是 User 中的属性名称)
我是 Spring MVC 和 Hibernate 的新手,我遇到了一个问题。我有 Child table 由 Hibernate 创建,它与用户 table (多对一)连接。当 Hibernate 创建 Child table 时,会自动添加 'user_object_id' - 通过 'user_object' (键入用户)这两个 table 已连接。但是,当我只想向用户显示他的 children 时,出现了问题:
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
我在ChildDaoImpl class的findAllUserChilds方法中从table得到Childobject,然后映射到Child控制器中的 DTO (POJO object)。错误与 Child 模型中的 'user_object_id' 相关,因为该变量在那里不存在,因为它由 Hibernate 自动创建并由 Criteria 在 findAllUserChilds() 中返回。我该如何解决这个问题?
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
...)
}
Child控制器:
@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();
}
}
Child 没有属性 user_object_id 但 user_object。 您必须使用 user_object.id(如果 id 是 User 中的属性名称)