0

我是 Spring MVC 和 Hibernate 的新手,我遇到了一个问题。我有由 Hibernate 创建的子表,它与用户表(多对一)连接。当 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”有关,因为该变量不存在,因为它是由 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
...)

}

子控制器:

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

}
4

1 回答 1

0

Child 没有属性 user_object_id 而是 user_object。您必须使用 user_object.id (如果 id 是用户中的属性名称)

于 2016-05-22T12:46:07.980 回答