org.hibernate.NonUniqueResultException: query did not return a unique result: 68
at org.hibernate.impl.AbstractQueryImpl.uniqueElement(AbstractQueryImpl.java:789)
at org.hibernate.impl.AbstractQueryImpl.uniqueResult(AbstractQueryImpl.java:780)
at com.bjsxt.oa.manager.impl.InitSystemDatasImpl.importRoleAndAcl(InitSystemDatasImpl.java:89)
at com.bjsxt.oa.manager.impl.InitSystemDatasImpl.addOrUpdateInitDatas(InitSystemDatasImpl.java:47)
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.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:318)
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:203)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:162)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:107)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:209)
at $Proxy7.addOrUpdateInitDatas(Unknown Source)
at com.bjsxt.oa.manager.InitSystemDatasTest.testAddOrUpdateInitDatas(InitSystemDatasTest.java:7)
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 junit.framework.TestCase.runTest(TestCase.java:154)
at junit.framework.TestCase.runBare(TestCase.java:127)
at org.springframework.test.ConditionalTestCase.runBare(ConditionalTestCase.java:69)
at junit.framework.TestResult$1.protect(TestResult.java:106)
at junit.framework.TestResult.runProtected(TestResult.java:124)
at junit.framework.TestResult.run(TestResult.java:109)
at junit.framework.TestCase.run(TestCase.java:118)
at junit.framework.TestSuite.runTest(TestSuite.java:208)
at junit.framework.TestSuite.run(TestSuite.java:203)
at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)
☆
有谁知道query did not return a unique result:2的含义? 发表于: 2007-2-14 下午7:01 | |||
public final
的方法。
Query
query = session.cre
ate
Query("from User
as u where u.
age > ?
and u.n
ame like ?");
query.setInteger(0,
25);
query.setString(1, "%
a%");
List list =
query.list();
|
Query
query = session.cre
ate
Query("from User
as u where u.
age > :min
Age
and u.n
ame like likeN
ame");
query.setInteger("min
Age",
25);
query.setString("likeN
ame", "%
a%");
List list =
query.list();
|
......
<
hibernate-mapping
>
<
class
name
=
"org.qiujy.demo.User"
table
=
"user"
>
<
id
name
=
"id"
column
=
"id"
type
=
"java.lang.Integer"
>
<
generator
class
=
"native"
/>
</
id
>
<
property
name
=
"name"
column
=
"name"
type
=
"java.lang.String"
/>
<
property
name
=
"age"
column
=
"age"
type
=
"java.lang.Integer"
/>
</
class
>
<query name=
"queryUserByAgeAndName">
<![CDATA[
from User as u where u.age >:minAge and u.name like :likeName
]]>
</query>
</
hibernate-mapping
>
|
Query
query = session.getN
amed
Query(
"queryUserByAgeAndName");
query.setInteger("min
Age",
25);
query.setString("likeN
ame", "%
a%");
List list =
query.list();
|
Hibernate-QBC中的setFetchMode规则
使用QBC所需要注意的一些重点:HQL总是忽略配置文件中预先抓取策略,即在HQL中使用预先抓取时,必须显示指定fetch关键字(例如:from Team t left join fetch t.students s where t.teamName like '%2')。
而QBC则不会忽略配置文件中的预先抓取策略。如果在配置文件中指定了预先抓取,则QBC会使用outer join fetch语句预先抓取对象的附属物,就好像是使用Session.get()或Session.load()方法一样。对于一个Criteria查询,可以调用criteria.setFetchMode("team",FetchMode.SELECT),以使配置文件的预先抓取策略失效。
另外 createAlias时候也会覆盖 fetchmode 设置,修改关联对象的提取方式。
eg. criteria.createAlias("tags", "tag",CriteriaSpecification.LEFT_JOIN);
http://cid-65ac531dd8831879.spaces.live.com/Blog/cns!65AC531DD8831879!218.entry
Hibernate-column原则
属性 本身类的主键id 关联类的主键id <key column="" />的值 < column="" />的值
一对一 yes yes
一对多 yes yes yes
多对一 yes yes yes
多对多 yes yes yes yes
在我们查询Parent对象的时候,默认只有Parent的内容,并不包含childs的信息,如果在Parent.hbm.xml里设置lazy="false"的话才同时取出关联的所有childs内容. 问题是我既想要hibernate默认的性能又想要临时的灵活性该怎么办? 这就是fetch的功能。我们可以把fetch与lazy="true"的关系类比为事务当中的编程式事务与声明式事务,不太准确,但是大概是这个意思。
总值,fetch就是在代码这一层给你一个主动抓取得机会.
Parent parent = (Parent)hibernateTemplate.execute(new HibernateCallback() {
public Object doInHibernate(Session session) throws HibernateException, SQLException {
Query q = session.createQuery(
"from Parent as parent "+
" left outer join fetch parent.childs " +
" where parent.id = :id"
);
q.setParameter("id",new Long(15));
return (Parent)q.uniqueResult();
}
});
Assert.assertTrue(parent.getChilds().size() > 0);
你可以在lazy="true"的情况下把fetch去掉,就会报异常. 当然,如果lazy="false"就不需要fetch了
☆
最后把MYSQL5·0·18换成MYSQL5·0·22
改my.ini的default-character-set=gb2312有两个地方要改
重启mysql
打开运行
net stop mysql
net start mysql
最终OK。原来是数据库的问题。