Search code examples
javahibernatecriteria

Restrictions to get main Entity based on another entity from a List


I have a relationship between 2 Entities, UserDefinition and Company. Here is how i map my relationship with hibernate :

@OneToMany
@JoinColumn(name="COMPANY_ID",nullable=true,updatable=true)
private List<UserDefinition> users;

I do have a COMPANY_ID column in my table but not in UserDefinition Entity to avoid recursivity. Here is what i am doing for now :

@Transactional
    public Company retrieveByUser(UserDefinition user) {
        return (Company) getCurrentSession().createCriteria(getClazz()).add(Restrictions.eq("users", user)).uniqueResult();
    }

But i get an error, here is a part of the stack trace :

java.sql.SQLException: Missing IN or OUT parameter at index:: 1
    at oracle.jdbc.driver.OraclePreparedStatement.processCompletedBindRow(OraclePreparedStatement.java:2086)
    at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3772)
    at oracle.jdbc.driver.T4CPreparedStatement.executeInternal(T4CPreparedStatement.java:1343)
    at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:3822)
    at oracle.jdbc.driver.OraclePreparedStatementWrapper.executeQuery(OraclePreparedStatementWrapper.java:1165)
    at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeQuery(NewProxyPreparedStatement.java:353)
    at org.hibernate.engine.jdbc.internal.ResultSetReturnImpl.extract(ResultSetReturnImpl.java:70)
    at org.hibernate.loader.Loader.getResultSet(Loader.java:2122)
    at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1905)
    at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1881)
    at org.hibernate.loader.Loader.doQuery(Loader.java:925)
    at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:342)
    at org.hibernate.loader.Loader.doList(Loader.java:2622)
    at org.hibernate.loader.Loader.doList(Loader.java:2605)
    at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2434)
    at org.hibernate.loader.Loader.list(Loader.java:2429)
    at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:109)
    at org.hibernate.internal.SessionImpl.list(SessionImpl.java:1787)
    at org.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:363)
    at org.hibernate.internal.CriteriaImpl.uniqueResult(CriteriaImpl.java:385)
    at XXXX.commons.spring.io.persistence.dao.CompanyDAO.retrieveByUser(CompanyDAO.java:25)

There is probably something wrong with Restrictions.eq("users", user). How can i retrieve the company of one specific user ? After retrieving this company i will use it as a filter by company - e.g. Restrictions.in("userLogin", companyDao.retrieveByUser(user).getUsers()) on another table containing the userLogin. Thanks for your help :)


Solution

  • Restrictions.eq("users", user) would mean that the list of users is equal to user, so that's not correct, you want to test if the list of users contains that user. I think that this should work better (creating an alias for the user):

    Criteria criteria= getSession().createCriteria(getClazz());
    criteria.createAlias("users", "u"); 
    criteria.add(Restrictions.eq("u.id", user.getId()));
    

    Please note that the Criteria API is deprecated so you might want to consider the JPA API or HQL