java - 在 @CollectionElement hibernate 中使用 createCriteria

标签 java hibernate

我有这个类(从实际代码中简化而来):

@Entity
KeyVal{
@Id private int id;
@ElementCollection private Collection<String> keys;
@ElementCollection private Collection<String> values;
}

我希望能够提取所有持有特定 key 的 KeyVals。 类似于:

List<KeyVal> getByKey(String key) {
    return getSession().createCriteria(KeyVal.class)
                       .add(Restriction.eq("keys",key)).list();
}

但是该代码产生:

    org.hibernate.exception.GenericJDBCException: could not execute query
        at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:140)
        at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:128)
        at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
        at org.hibernate.loader.Loader.doList(Loader.java:2545)
        at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2276)
        at org.hibernate.loader.Loader.list(Loader.java:2271)
        at org.hibernate.loader.criteria.CriteriaLoader.list(CriteriaLoader.java:119)
        at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1716)
        at org.hibernate.impl.CriteriaImpl.list(CriteriaImpl.java:347)
        at lucky.HibernateLuckyDB.getAllSearchesContaining(HibernateLuckyDB.java:53)
        at lucky.HibernateLuckyDBTest.testGetAllSearchesContainingMulty(HibernateLuckyDBTest.java:58)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
        at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
        at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
        at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
        at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
        at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
        at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
        at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
    Caused by: org.h2.jdbc.JdbcSQLException: Parameter "#1" is not set; SQL statement:
    select this_.id as id12_0_ from WorldConSearch this_ where this_.id=? [90012-140]
        at org.h2.message.DbException.getJdbcSQLException(DbException.java:327)
        at org.h2.message.DbException.get(DbException.java:167)
        at org.h2.message.DbException.get(DbException.java:144)
        at org.h2.expression.Parameter.checkSet(Parameter.java:73)
        at org.h2.command.Prepared.checkParameters(Prepared.java:153)
        at org.h2.command.CommandContainer.query(CommandContainer.java:79)
        at org.h2.command.Command.executeQuery(Command.java:132)
        at org.h2.jdbc.JdbcPreparedStatement.executeQuery(JdbcPreparedStatement.java:96)
        at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeQuery(NewProxyPreparedStatement.java:76)
        at org.hibernate.jdbc.AbstractBatcher.getResultSet(AbstractBatcher.java:208)
        at org.hibernate.loader.Loader.getResultSet(Loader.java:1953)
        at org.hibernate.loader.Loader.doQuery(Loader.java:802)
        at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:274)
        at org.hibernate.loader.Loader.doList(Loader.java:2542)
        ... 31 more

奇怪的是,对于复杂类型来说这似乎很容易(来自 Hibernate 文档):

List cats = sess.createCriteria(Cat.class)
                .add( Restrictions.like("name", "F%") )
                .createCriteria("kittens")
                .add( Restrictions.like("name", "F%") )
                .list();

最佳答案

你能试试 createAlias 吗?

List<KeyVal> getByKey(String key)
{
     return getSession().createCriteria(KeyVal.class)
         .createAlias("keys", "key")
         .add(Restriction.eq("key",key))
         .list();
}

关于java - 在 @CollectionElement hibernate 中使用 createCriteria,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9012796/

相关文章:

java - 在使用 Hibernate 4 时, `org.hibernate.cache.EhCacheProvider` 需要什么依赖项?

java - 将多部分文件插入数据库

hibernate - 无法在运行 mysql 的情况下获取 JPA

java - 如何为.net应用程序制作javaservice?

java - java程序在eclipse IDE中编译错误

java - 如何检测JFrame窗口最小化和最大化事件?

java - Spring MVC 与 jdbc 模板

java - org.hibernate.exception.ConstraintViolationException : could not execute statement

java - 将子包移动到新包后@Autowire 不起作用

java - Android 6.0 - 使用 PlaybackParams 设置视频速度