0

我有两张桌子hesk_usersuser如下所示。

@Entity @Table(name="hesk_users") 公共类 UserHesk {

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

@OneToOne
@JoinColumn(name="user_id")
private User user;

    set.... get..

}

@Entity @Table(name="user") 公共类用户实现可序列化 {

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

@NotEmpty
@Column
private String firstname;

     @OneToOne(mappedBy="user")
private UserHesk userHesk; 

    set ... get ... 

}

我有休眠查询,但它不起作用..

            DetachedCriteria detachedCriteria=DetachedCriteria.forClass(UserHesk.class)
    .setProjection(Projections.property("user_id"));

    Criteria criteria=sessionFactory.getCurrentSession().createCriteria(User.class);
    criteria.add(Property.forName("id").notIn(detachedCriteria));
    User user=(User)criteria.list().get(0);
    System.out.println(user.getFirstname());
    System.out.println("Subquery Size "+criteria.list().size());

user_id归档错误。由于关系无法得到。

4

1 回答 1

0

HQL 和条件查询始终使用实体类和属性。永远不要使用基础表和列的名称。UserHesk 类没有任何user_id属性。它有一个user属性。

所以代码应该是

DetachedCriteria detachedCriteria = 
    DetachedCriteria.forClass(UserHesk.class)
                    .setProjection(Projections.property("user.id"));

甚至

DetachedCriteria detachedCriteria = 
    DetachedCriteria.forClass(UserHesk.class)
                    .createAlias("user", "u")
                    .setProjection(Projections.property("u.id"));
于 2013-03-07T14:29:35.443 回答