当前位置:编程学习 > JAVA >>

hibernate 多对一 查询生成语句问题

配置如下:



<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.ibokee.weibo.bean">
<class name="UserTopic" table="ibokee_wb_usertopic" dynamic-insert="true" dynamic-update="true" >

<id name="id" column="ID" length="11">
<generator class="native" />
</id>
<property name="type" column="type" />
<many-to-one name="user" column="userID"  fetch="select"/>
<many-to-one name="topic" column="topicID"/>
<property name="createTime" column="createTime" type="timestamp" />
</class>
</hibernate-mapping>


<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.ibokee.weibo.bean">
<class name="Topic" table="ibokee_wb_topic" dynamic-insert="true"  dynamic-update="true">

<id name="id" column="ID" length="11">
<generator class="native" />
</id>

<property name="name" column="name" length="20" />
<property name="userCount" column="userCount" length="11"/>
<property name="postCount" column="postCount" length="11"/>
<property name="isRecommend" column="isRecommend" length="11"/>
<property name="isHot" column="isHot" length="11"/>
<property name="desc" column="desc"/>
<property name="picUrl" column="picUrl"/>
<property name="isLock" column="isLock" length="11"/>
<property name="createTime" column="createTime" type="timestamp" />
<many-to-one class="TopicGroup" name="topicGroup" column="groupID"/>
</class>
</hibernate-mapping>


查询用户关注的话题,1-n个查询,而不是一个连接查询,怎么优化成一条连接查询,一条语句!
打印出的sql:
Hibernate: select usertopic0_.ID as ID83_, usertopic0_.type as type83_, usertopic0_.userID as userID83_, usertopic0_.topicID as topicID83_, usertopic0_.createTime as createTime83_ from ibokee_wb_usertopic usertopic0_ where usertopic0_.userID=? and usertopic0_.type=0 order by usertopic0_.createTime desc
Hibernate: select topic0_.ID as ID75_0_, topic0_.name as name75_0_, topic0_.userCount as userCount75_0_, topic0_.postCount as postCount75_0_, topic0_.isRecommend as isRecomm5_75_0_, topic0_.isHot as isHot75_0_, topic0_.desc as desc75_0_, topic0_.picUrl as picUrl75_0_, topic0_.isLock as isLock75_0_, topic0_.createTime as createTime75_0_, topic0_.groupID as groupID75_0_ from ibokee_wb_topic topic0_ where topic0_.ID=?
Hibernate: select topic0_.ID as ID75_0_, topic0_.name as name75_0_, topic0_.userCount as userCount75_0_, topic0_.postCount as postCount75_0_, topic0_.isRecommend as isRecomm5_75_0_, topic0_.isHot as isHot75_0_, topic0_.desc as desc75_0_, topic0_.picUrl as picUrl75_0_, topic0_.isLock as isLock75_0_, topic0_.createTime as createTime75_0_, topic0_.groupID as groupID75_0_ from ibokee_wb_topic topic0_ where topic0_.ID=?
Hibernate: select topic0_.ID as ID75_0_, topic0_.name as name75_0_, topic0_.userCount as userCount75_0_, topic0_.postCount as postCount75_0_, topic0_.isRecommend as isRecomm5_75_0_, topic0_.isHot as isHot75_0_, topic0_.desc as desc75_0_, topic0_.picUrl as picUrl75_0_, topic0_.isLock as isLock75_0_, topic0_.createTime as createTime75_0_, topic0_.groupID as groupID75_0_ from ibokee_wb_topic topic0_ where topic0_.ID=?
Hibernate: select topic0_.ID as ID75_0_, topic0_.name as name75_0_, topic0_.userCount as userCount75_0_, topic0_.postCount as postCount75_0_, topic0_.isRecommend as isRecomm5_75_0_, topic0_.isHot as isHot75_0_, topic0_.desc as desc75_0_, topic0_.picUrl as picUrl75_0_, topic0_.isLock as isLock75_0_, topic0_.createTime as createTime75_0_, topic0_.groupID as groupID75_0_ from ibokee_wb_topic topic0_ where topic0_.ID=?
Hibernate: select topic0_.ID as ID75_0_, topic0_.name as name75_0_, topic0_.userCount as userCount75_0_, topic0_.postCount as postCount75_0_, topic0_.isRecommend as isRecomm5_75_0_, topic0_.isHot as isHot75_0_, topic0_.desc as desc75_0_, topic0_.picUrl as picUrl75_0_, topic0_.isLock as isLock75_0_, topic0_.createTime as createTime75_0_, topic0_.groupID as groupID75_0_ from ibokee_wb_topic topic0_ where topic0_.ID=?
Hibernate: select topic0_.ID as ID75_0_, topic0_.name as name75_0_, topic0_.userCount as userCount75_0_, topic0_.postCount as postCount75_0_, topic0_.isRecommend as isRecomm5_75_0_, topic0_.isHot as isHot75_0_, topic0_.desc as desc75_0_, topic0_.picUrl as picUrl75_0_, topic0_.isLock as isLock75_0_, topic0_.createTime as createTime75_0_, topic0_.groupID as groupID75_0_ from ibokee_wb_topic topic0_ where topic0_.ID=?
Hibernate: select topic0_.ID as ID75_0_, topic0_.name as name75_0_, topic0_.userCount as userCount75_0_, topic0_.postCount as postCount75_0_, topic0_.isRecommend as isRecomm5_75_0_, topic0_.isHot as isHot75_0_, topic0_.desc as desc75_0_, topic0_.picUrl as picUrl75_0_, topic0_.isLock as isLock75_0_, topic0_.createTime as createTime75_0_, topic0_.groupID as groupID75_0_ from ibokee_wb_topic topic0_ where topic0_.ID=?
Hibernate: select topic0_.ID as ID75_0_, topic0_.name as name75_0_, topic0_.userCount as userCount75_0_, topic0_.postCount as postCount75_0_, topic0_.isRecommend as isRecomm5_75_0_, topic0_.isHot as isHot75_0_, topic0_.desc as desc75_0_, topic0_.picUrl as picUrl75_0_, topic0_.isLock as isLock75_0_, topic0_.createTime as createTime75_0_, topic0_.groupID as groupID75_0_ from ibokee_wb_topic topic0_ where topic0_.ID=?
Hibernate: select topic0_.ID as ID75_0_, topic0_.name as name75_0_, topic0_.userCount as userCount75_0_, topic0_.postCount as postCount75_0_, topic0_.isRecommend as isRecomm5_75_0_, topic0_.isHot as isHot75_0_, topic0_.desc as desc75_0_, topic0_.picUrl as picUrl75_0_, topic0_.isLock as isLock75_0_, topic0_.createTime as createTime75_0_, topic0_.groupID as groupID75_0_ from ibokee_wb_topic topic0_ where topic0_.ID=?
Hibernate: select topic0_.ID as ID75_0_, topic0_.name as name75_0_, topic0_.userCount as userCount75_0_, topic0_.postCount as postCount75_0_, topic0_.isRecommend as isRecomm5_75_0_, topic0_.isHot as isHot75_0_, topic0_.desc as desc75_0_, topic0_.picUrl as picUrl75_0_, topic0_.isLock as isLock75_0_, topic0_.createTime as createTime75_0_, topic0_.groupID as groupID75_0_ from ibokee_wb_topic topic0_ where topic0_.ID=?





--------------------编程问答-------------------- 这个需要自己仔细点看了。lz加油! --------------------编程问答--------------------
引用 1 楼 yuejingjiahong 的回复:
这个需要自己仔细点看了。lz加油!


不知道怎么隐射 --------------------编程问答-------------------- 大中午的 看到这些 头晕了 ... --------------------编程问答-------------------- hibernate 多对一查询 当我配置文件lazy=false时,如果我查的是单个实体,出来的语句是表连接式的查询,但如果我是query.list()查集合的时候,查询语句是2条的,请问这是为什么,怎么可以在查询集合的时候也是表连接查询,而非生成2条查询语句
补充:Java ,  Java EE
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,