使用带有List <enum> </enum>的Hibernate Restrictions.in

时间:2013-01-10 14:03:48

标签: hibernate

如何将Restrictions.in与列表字段一起使用?

这是我的模特:

@Entity
@Table(name = "W_GROUP", schema = "U_FRONTEND")
public class UserGroup {
@Id
@Column(name = "GROUP_ID")
private Long id;

@Column(name = "NAME", length = 100, nullable = false, unique = true)
private String name;

@ElementCollection(targetClass = Feature.class)
@JoinTable(name = "W_FEATURE", joinColumns = @JoinColumn(name = "GROUP_ID", nullable = false), schema = "U_FRONTEND")
@Column(name = "NAME", nullable = false, length = 50)
@Enumerated(EnumType.STRING)
private List<Feature> features;

@JoinTable(name = "W_USER_GROUP", joinColumns = @JoinColumn(name = "GROUP_ID"), inverseJoinColumns = @JoinColumn(name = "USER_ID"), schema = "U_FRONTEND")
@ManyToMany
private List<User> users;

@Column(name = "UPD_USER", nullable = false, length = 50)
private String updUser;

@Column(name = "UPD_DATE", nullable = false, columnDefinition = "date")
private Calendar updDate;
// Getters and setters
}

我试图只获得具有某些功能的群组:

Criteria criteria = session.createCriteria(UserGroup.class);

            criteria.addOrder(new Sort(Order.ASC, "name").getOrder());

            criteria.add(Restrictions.not(Restrictions.in("features",
                    FeatureUtils.getInvalids())));

但这会返回错误:

  

引起:org.hibernate.exception.GenericJDBCException:缺少IN或   索引:: 1处的OUT参数   org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:54)     在   org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:125)     在   org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:110)     在   org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:129)     在   org.hibernate.engine.jdbc.internal.proxy.AbstractProxyHandler.invoke(AbstractProxyHandler.java:81)     在$ Proxy35.executeQuery(未知来源)at   org.hibernate.loader.Loader.getResultSet(Loader.java:1926)at at   org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1727)     在org.hibernate.loader.Loader.doQuery(Loader.java:852)处   org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:293)     在org.hibernate.loader.Loader.doList(Loader.java:2411)at   org.hibernate.loader.Loader.doList(Loader.java:2397)at   org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2227)at at   org.hibernate.loader.Loader.list(Loader.java:2222)at   org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:122)     在org.hibernate.internal.SessionImpl.list(SessionImpl.java:1621)at   org.hibernate.internal.CriteriaImpl.list(CriteriaImpl.java:374)at at   hamburgsud.frontend.dao.UserGroupDAO.list(UserGroupDAO.java:39)at at   hamburgsud.frontend.controller.UserController.form(UserController.java:38)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)     在java.lang.reflect.Method.invoke(Method.java:597)at   br.com.caelum.vraptor.interceptor.ExecuteMethodInterceptor.intercept(ExecuteMethodInterceptor.java:61)     ... 43更多引起:java.sql.SQLException:缺少IN或OUT   index :: 1处的参数   oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)     在   oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:146)     在   oracle.jdbc.driver.OraclePreparedStatement.processCompletedBindRow(OraclePreparedStatement.java:1681)     在   oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3280)     在   oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:3329)     在   com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeQuery(NewProxyPreparedStatement.java:76)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)     在java.lang.reflect.Method.invoke(Method.java:597)at   org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:122)     ... 63更多

1 个答案:

答案 0 :(得分:0)

我知道它不是很漂亮,但你可以构建一个id列表(List<Long> featureIds),然后做一个:

Restrictions.in("features", featureIds);