2012-01-26 75 views
2

我有這個類(從實際代碼的簡化):在Hibernate中使用個createCriteria上@CollectionElement

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

我希望能夠提取持有特定密鑰的所有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(); 
+0

當你嘗試時會得到什麼錯誤? – AHungerArtist

+0

@AHungerArtist編輯過的例外 – upsidedown

回答

0

可以嗎嘗試createAlias?

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

這會產生org.hibernate.MappingException:集合不是關聯 – upsidedown

相關問題