我是Spring MVC和Hibernate的新手,我遇到了一个问题。我已经通过Hibernate创建了Child表,它与User表(Many to One)相连。当Hibernate创建Child表时,会自动添加' user_object_id' - 通过' user_object' (类型用户)这两个表是连接的。但是,当我想向用户展示他的孩子时,就会出现问题:
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
我从findAllUserChilds方法中的ChildDaoImpl类中的表中获取Child对象,然后映射到ChildController中的ChildDTO(POJO对象)。错误与' user_object_id'在Child模型中,因为这个变量不存在,因为它由Hibernate自动创建并由Criteria在findAllUserChilds()中返回。我该如何解决这个问题?
子:
@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();
}
}
答案 0 :(得分:0)
Child没有propetry user_object_id但没有user_object。 您必须使用user_object.id(如果id是User中的属性名称)