Hibernate中HQL语句的left join的例子详解

来源:互联网 发布:linux c 编程 编辑:程序博客网 时间:2024/05/21 21:49
1.com.jcuckoo.entity添加ShowBook类,用来封装界面显示的数据

@Entity
   public class ShowBook {

@Id
private int id;
private String name;
private String author;
private String publisher;
private double price;
private String pic;
private int storageId;
private int amount;
private Date inDate;
……
}
2.在hibernate.cfg.xml配置文件中,添加<mapping class="com.jcuckoo.entity.ShowBook"/>
3.在com.jcuckoo.entity.Book中添加属性及get/set方法
private int id;
private String name;
private String author;
private String publisher;
private double price;
private String pic;
private Set<Storage> storages;
4.在Book.hbm.xml中添加多对一配置
<set name="storages">
    <key column="bookId"></key>
    <one-to-many class="com.jcuckoo.entity.Storage"/>
   </set>

5.在com.jcuckoo.entity.Storage添加属性及get/set方法
private int id;
private int bookId;
private String bookName;
private int amount;
private Date inDate;
private Book book;
6.在Storage.hbm.xml中添加一对多配置
<many-to-one name="book" class="com.jcuckoo.entity.Book" column="bookId" insert="false" update="false"/>
7.com.jcuckoo.dao中添加ShowBookDao,通过左连接完成数据查询from Book b left join fetch b.storages s
public List<ShowBook> findAll() {List<ShowBook> showBookList = new ArrayList<ShowBook>();String hql = "from Book b left join fetch b.storages s";Query query = this.getSession().createQuery(hql);List<Book> list = query.list();for (int i = 0; i < list.size(); i++) {Book book = list.get(i);ShowBook showBook = new ShowBook(book.getId(), book.getName(),book.getAuthor(), book.getPublisher(), book.getPrice(),book.getPic());Iterator it = book.getStorages().iterator();if (it.hasNext()) {Storage storage = (Storage) it.next();showBook.setStorageId(storage.getId());showBook.setAmount(storage.getAmount());showBook.setInDate(storage.getInDate());}showBookList.add(showBook);}return showBookList;}


8.在applicationContext.xml配置ShowBookDao
<bean id="ShowBookDao" class="com.jcuckoo.dao.ShowBookDao">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
9.在com.jcuckoo.service添加ShowBookService类
10.在applicationContext.xml配置ShowBookService
<bean id="ShowBookService" class="com.jcuckoo.service.ShowBookService">
<property name="showBookDao" ref="ShowBookDao"/>
</bean>
11.在com.jcuckoo.action添加BuyBookAction类
private ShowBookService showBookService;//提供get/set方法
public String list() throws Exception {   // 去数据库查数据,并显示处理   List<ShowBook> showBookList = showBookService.findAll();   ServletActionContext.getRequest().setAttribute("showBookList",showBookList);   return "list";}


12.在applicationContext.xml配置
<bean id="BuyBookAction" class="com.jcuckoo.action.BuyBookAction">
<property name="ShowBookService" ref="ShowBookService"/>
</bean>
13. 在显示界面
<s:iterator value="#request.showBookList" id="myBook" status="status">      <s:if test="%{#status.index%5==0}">      <div>      </s:if>    <div class="${status.index%2==0?'div1':'div2' }">        <table width="200" border="1">              <tr>                <td rowspan="4"><img src="${myBook.pic }" width="100px" height="180px"/></td>                <td>作者:${myBook.name }${myBook.author }</td>              </tr>              <tr>                <td>出版社:${myBook.publisher }</td>              </tr>              <tr>                <td>价格:${myBook.price }</td>              </tr>              <tr>                <td>上架时间:<%-- :${myBook.storage.dealInDate==null?'<font color=red>暂未上架</font>':myBook.storage.dealInDate } --%>                <s:if test="%{#myBook.amount>0 }">                <!-- %{#myBook.storage!=null and #myBook.storage.amount>0 }或                %{#myBook.storage!=null && #myBook.storage.amount>0 } -->               ${myBook.dealInDate }               <input type="button" value="加入购入车" class="shopCarButton"/>                </s:if>                <s:else>                <font color=red>暂未上架</font>                </s:else>                </td>              </tr>            </table>      </div>       <s:if test="%{#status.index%5==4}">      </div>      <div class="clearDiv"></div>      </s:if>     </s:iterator>


0 0
原创粉丝点击