想用<set batch-size="10">的功能去批量实例化集合的实例,但是在控制台输出的SQL代码来看还是没有变化,产生的select语句还是实例化一个TblReply对象执行一条select语句,不知道什么原因batch-size="10"属性不起作用,我看《精通Hibernate》这本书里面的教程都可以,下面是我的代码,代码有点长,麻烦大家帮忙解决,急切学习中:
TblTopic.hbm.xml:
<?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">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.good.hibernate.entity.TblTopic" table="TBL_TOPIC" batch-size="10" schema="dbo" catalog="bbs">
<id name="topicId" type="java.lang.Integer">
<column name="topicId" />
<generator class="increment" />
</id>
<many-to-one name="tblUser" class="com.good.hibernate.entity.TblUser" lazy="false">
<column name="uId" not-null="true" />
</many-to-one>
<many-to-one name="tblBoard" class="com.good.hibernate.entity.TblBoard">
<column name="boardId" not-null="true" />
</many-to-one>
<set name="tblReplys" batch-size="3" inverse="true" lazy="true">
<key column="topicId"></key>
<one-to-many class="com.good.hibernate.entity.TblReply"/>
</set>
<property name="title" type="java.lang.String">
<column name="title" length="50" not-null="true" />
</property>
<property name="content" type="java.lang.String">
<column name="content" length="1000" not-null="true" />
</property>
<property name="publishTime" type="java.util.Date">
<column name="publishTime" length="23" not-null="true" />
</property>
<property name="modifyTime" type="java.util.Date">
<column name="modifyTime" length="23" not-null="true" />
</property>
</class>
<query name="getTopics">
from TblTopic tblTopic
left join fetch tblTopic.tblUser
where tblTopic.tblBoard.boardId=:boardId
</query>
</hibernate-mapping>
===============================================================================
===============================================================================
实例类。
TblTopic.java:public class TblTopic {
private Integer topicId;
private String title;
private String content;
private Date publishTime;
private Date modifyTime;
private TblUser tblUser;
private TblBoard tblBoard;
private Set<TblReply> tblReplys = new HashSet<TblReply>();
private int tblReplysSize;
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Date getModifyTime() {
return modifyTime;
}
public void setModifyTime(Date modifyTime) {
this.modifyTime = modifyTime;
}
public Date getPublishTime() {
return publishTime;
}
public void setPublishTime(Date publishTime) {
this.publishTime = publishTime;
}
public TblBoard getTblBoard() {
return tblBoard;
}
public void setTblBoard(TblBoard tblBoard) {
this.tblBoard = tblBoard;
}
public TblUser getTblUser() {
return tblUser;
}
public void setTblUser(TblUser tblUser) {
this.tblUser = tblUser;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Integer getTopicId() {
return topicId;
}
public void setTopicId(Integer topicId) {
this.topicId = topicId;
}
public Set<TblReply> getTblReplys() {
return tblReplys;
}
public void setTblReplys(Set<TblReply> tblReplys) {
this.tblReplys = tblReplys;
try {
setTblReplysSize(tblReplys.size());
} catch (Exception e) {
// TODO: handle exception
}
}
public int getTblReplysSize() {
return tblReplysSize;
}
public void setTblReplysSize(int tblReplysSize) {
this.tblReplysSize = tblReplysSize;
}
}==========================================================================================
==========================================================================================
调用HQL的代码
Service.java:public class Service {
public List<TblTopic> getTopicList(int boardId) {
Session session = HibernateSessionFactory.getSession();
Transaction tran = null;
List<TblTopic> tblTopics = null;
try {
Query query = session.getNamedQuery("getTopics");
query.setInteger("boardId",boardId);
tblTopics = query.list();
} catch (Exception e) {
tran.rollback();
}finally{
session.close();
}
return tblTopics;
}
}
TblTopic.hbm.xml:
<?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">
<!--
Mapping file autogenerated by MyEclipse Persistence Tools
-->
<hibernate-mapping>
<class name="com.good.hibernate.entity.TblTopic" table="TBL_TOPIC" batch-size="10" schema="dbo" catalog="bbs">
<id name="topicId" type="java.lang.Integer">
<column name="topicId" />
<generator class="increment" />
</id>
<many-to-one name="tblUser" class="com.good.hibernate.entity.TblUser" lazy="false">
<column name="uId" not-null="true" />
</many-to-one>
<many-to-one name="tblBoard" class="com.good.hibernate.entity.TblBoard">
<column name="boardId" not-null="true" />
</many-to-one>
<set name="tblReplys" batch-size="3" inverse="true" lazy="true">
<key column="topicId"></key>
<one-to-many class="com.good.hibernate.entity.TblReply"/>
</set>
<property name="title" type="java.lang.String">
<column name="title" length="50" not-null="true" />
</property>
<property name="content" type="java.lang.String">
<column name="content" length="1000" not-null="true" />
</property>
<property name="publishTime" type="java.util.Date">
<column name="publishTime" length="23" not-null="true" />
</property>
<property name="modifyTime" type="java.util.Date">
<column name="modifyTime" length="23" not-null="true" />
</property>
</class>
<query name="getTopics">
from TblTopic tblTopic
left join fetch tblTopic.tblUser
where tblTopic.tblBoard.boardId=:boardId
</query>
</hibernate-mapping>
===============================================================================
===============================================================================
实例类。
TblTopic.java:public class TblTopic {
private Integer topicId;
private String title;
private String content;
private Date publishTime;
private Date modifyTime;
private TblUser tblUser;
private TblBoard tblBoard;
private Set<TblReply> tblReplys = new HashSet<TblReply>();
private int tblReplysSize;
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Date getModifyTime() {
return modifyTime;
}
public void setModifyTime(Date modifyTime) {
this.modifyTime = modifyTime;
}
public Date getPublishTime() {
return publishTime;
}
public void setPublishTime(Date publishTime) {
this.publishTime = publishTime;
}
public TblBoard getTblBoard() {
return tblBoard;
}
public void setTblBoard(TblBoard tblBoard) {
this.tblBoard = tblBoard;
}
public TblUser getTblUser() {
return tblUser;
}
public void setTblUser(TblUser tblUser) {
this.tblUser = tblUser;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public Integer getTopicId() {
return topicId;
}
public void setTopicId(Integer topicId) {
this.topicId = topicId;
}
public Set<TblReply> getTblReplys() {
return tblReplys;
}
public void setTblReplys(Set<TblReply> tblReplys) {
this.tblReplys = tblReplys;
try {
setTblReplysSize(tblReplys.size());
} catch (Exception e) {
// TODO: handle exception
}
}
public int getTblReplysSize() {
return tblReplysSize;
}
public void setTblReplysSize(int tblReplysSize) {
this.tblReplysSize = tblReplysSize;
}
}==========================================================================================
==========================================================================================
调用HQL的代码
Service.java:public class Service {
public List<TblTopic> getTopicList(int boardId) {
Session session = HibernateSessionFactory.getSession();
Transaction tran = null;
List<TblTopic> tblTopics = null;
try {
Query query = session.getNamedQuery("getTopics");
query.setInteger("boardId",boardId);
tblTopics = query.list();
} catch (Exception e) {
tran.rollback();
}finally{
session.close();
}
return tblTopics;
}
}
解决方案 »
- struts2的小程序求解答
- 怎样将这个超链接变灰,不可用?
- JSP里什么时候需要提交到servlet里
- prototype验证checkbox问题
- javax.servlet.ServletException: No getter method for property Admin_Name of bean org.apache.struts.taglib.html.BEAN
- 请问哪位达淫用jsp做过地图类型的图片放大缩小拖动等功能
- 怎样使用这个Bean!!!代码如下
- 请问大家:为什么有些人在开发数据库时把每一个数据表都单独写在一个类里面?
- 关于jive professional 3.05的安装
- 请问如何部署sip 服务器
- 问个spring 连接oracle数据库问题
- 我受不了,用二进制流写的图片竟然不能显示在<img src="">里
batch-size 好象是针对 批量修改 和删除的要测试查询的话 应该用 :fetch-size `````````````````