Spring hibernate标准嵌套对象

时间:2016-11-23 23:30:01

标签: hibernate-criteria detachedcriteria

我的数据正确查询存在问题。

   @Id
@GeneratedValue
private Integer id;

@Size(min = 1, message = "")
@Column(unique = true)
@UniqueUserName(message = "")
private String name;

@Size(min = 1, message = "")
@Email
private String email;

@Size(min = 5, message = "")
private String password;

private boolean enabled;

@ManyToMany
@JoinTable
private List<Role> roles;

@OneToOne
@JoinColumn
private PersonalData personalData;

@OneToMany(cascade = CascadeType.REMOVE)
@JoinColumn(name = "OWNED_USER_ID")
private List<User> userList; 

这是我的班级用户。我希望用户获得所有拥有用户的列表。

这是我的代码:

DetachedCriteria detachedCriteria = DetachedCriteria.forClass(User.class);
detachedCriteria.setProjection(Property.forName("userList.id"));
Session session = entityManager.unwrap(Session.class); 
Criteria criteria = session.createCriteria(OfferStorage.class);
criteria.add(Subqueries.propertyIn("user.id", detachedCriteria));
criteria.list();

它会让我跟随错误 无法解析属性:userList.id

如果您使用userList.user.id,则相同。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

那很快。

如果其他人有问题就有答案

    DetachedCriteria detachedCriteria = DetachedCriteria.forClass(User.class);
    detachedCriteria.createAlias("userList", "userList");
    detachedCriteria.add(Restrictions.eq("id", userId ));
    detachedCriteria.setProjection(Property.forName("userList.id"));

    Session session = entityManager.unwrap(Session.class);
    Criteria criteria = session.createCriteria(OfferStorage.class);
    criteria.add(Subqueries.propertyIn("user.id", detachedCriteria));
    criteria.list();