hibernate-criteriadetachedcriteria

Spring hibernate criteria nested object


I have problem with correct query for my data.

   @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; 

That's my class User. And i want to get list of all owned users by user.

here is my code:

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

And it's return me following error could not resolve property: userList.id

It's same if u use userList.user.id. Any ideas?


Solution

  • Well that was fast.

    If some else would have problem with it there is answer

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