⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 example-weblog.html

📁 是一个中文的Hibernate库文档
💻 HTML
字号:
<html><head>      <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">   <title>&#31532;&nbsp;23&nbsp;&#31456;&nbsp;&#31034;&#20363;&#65306;Weblog &#24212;&#29992;&#31243;&#24207;</title><link rel="stylesheet" href="../shared/css/html.css" type="text/css"><meta name="generator" content="DocBook XSL Stylesheets V1.65.1"><link rel="home" href="index.html" title="HIBERNATE - &#31526;&#21512;Java&#20064;&#24815;&#30340;&#20851;&#31995;&#25968;&#25454;&#24211;&#25345;&#20037;&#21270;"><link rel="up" href="index.html" title="HIBERNATE - &#31526;&#21512;Java&#20064;&#24815;&#30340;&#20851;&#31995;&#25968;&#25454;&#24211;&#25345;&#20037;&#21270;"><link rel="previous" href="example-parentchild.html" title="&#31532;&nbsp;22&nbsp;&#31456;&nbsp;&#31034;&#20363;&#65306;&#29238;&#23376;&#20851;&#31995;(Parent Child Relationships)"><link rel="next" href="example-mappings.html" title="&#31532;&nbsp;24&nbsp;&#31456;&nbsp;&#31034;&#20363;&#65306;&#22797;&#26434;&#26144;&#23556;&#23454;&#20363;"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="3" align="center">&#31532;&nbsp;23&nbsp;&#31456;&nbsp;&#31034;&#20363;&#65306;Weblog &#24212;&#29992;&#31243;&#24207;</th></tr><tr><td width="20%" align="left"><a accesskey="p" href="example-parentchild.html">&#19978;&#19968;&#39029;</a>&nbsp;</td><th width="60%" align="center">&nbsp;</th><td width="20%" align="right">&nbsp;<a accesskey="n" href="example-mappings.html">&#19979;&#19968;&#39029;</a></td></tr></table><hr></div><div class="chapter" lang="zh-cn"><div class="titlepage"><div><div><h2 class="title"><a name="example-weblog"></a>&#31532;&nbsp;23&nbsp;&#31456;&nbsp;&#31034;&#20363;&#65306;Weblog &#24212;&#29992;&#31243;&#24207;</h2></div></div><div></div></div><div class="sect1" lang="zh-cn"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="example-weblog-classes"></a>23.1.&nbsp;&#25345;&#20037;&#21270;&#31867;</h2></div></div><div></div></div><p>        	&#19979;&#38754;&#30340;&#25345;&#20037;&#21270;&#31867;&#34920;&#31034;&#19968;&#20010;weblog&#21644;&#22312;&#20854;&#20013;&#24352;&#36148;&#30340;&#19968;&#20010;&#36148;&#23376;&#12290;&#20182;&#20204;&#26159;&#26631;&#20934;&#30340;&#29238;/&#23376;&#20851;&#31995;&#27169;&#22411;&#65292;&#20294;&#26159;&#25105;&#20204;&#20250;&#29992;&#19968;&#20010;&#26377;&#24207;&#21253;&#65288;ordered bag)&#32780;&#38750;&#38598;&#21512;(set)&#12290;        </p><pre class="programlisting">package eg;import java.util.List;public class Blog {    private Long _id;    private String _name;    private List _items;    public Long getId() {        return _id;    }    public List getItems() {        return _items;    }    public String getName() {        return _name;    }    public void setId(Long long1) {        _id = long1;    }    public void setItems(List list) {        _items = list;    }    public void setName(String string) {        _name = string;    }}</pre><pre class="programlisting">package eg;import java.text.DateFormat;import java.util.Calendar;public class BlogItem {    private Long _id;    private Calendar _datetime;    private String _text;    private String _title;    private Blog _blog;    public Blog getBlog() {        return _blog;    }    public Calendar getDatetime() {        return _datetime;    }    public Long getId() {        return _id;    }    public String getText() {        return _text;    }    public String getTitle() {        return _title;    }    public void setBlog(Blog blog) {        _blog = blog;    }    public void setDatetime(Calendar calendar) {        _datetime = calendar;    }    public void setId(Long long1) {        _id = long1;    }    public void setText(String string) {        _text = string;    }    public void setTitle(String string) {        _title = string;    }}</pre></div><div class="sect1" lang="zh-cn"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="example-weblog-mappings"></a>23.2.&nbsp;Hibernate &#26144;&#23556;</h2></div></div><div></div></div><p>            &#19979;&#21015;&#30340;XML&#26144;&#23556;&#24212;&#35813;&#26159;&#24456;&#30452;&#30333;&#30340;&#12290;        </p><pre class="programlisting">&lt;?xml version="1.0"?&gt;&lt;!DOCTYPE hibernate-mapping PUBLIC         "-//Hibernate/Hibernate Mapping DTD 3.0//EN"        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"&gt;&lt;hibernate-mapping package="eg"&gt;    &lt;class         name="Blog"         table="BLOGS" &gt;                &lt;id             name="id"             column="BLOG_ID"&gt;                        &lt;generator class="native"/&gt;                    &lt;/id&gt;                &lt;property             name="name"             column="NAME"             not-null="true"             unique="true"/&gt;                    &lt;bag             name="items"             inverse="true"             order-by="DATE_TIME"             cascade="all"&gt;                        &lt;key column="BLOG_ID"/&gt;            &lt;one-to-many class="BlogItem"/&gt;                    &lt;/bag&gt;            &lt;/class&gt;    &lt;/hibernate-mapping&gt;</pre><pre class="programlisting">&lt;?xml version="1.0"?&gt;&lt;!DOCTYPE hibernate-mapping PUBLIC         "-//Hibernate/Hibernate Mapping DTD 3.0//EN"        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd"&gt;&lt;hibernate-mapping package="eg"&gt;        &lt;class         name="BlogItem"         table="BLOG_ITEMS"         dynamic-update="true"&gt;                &lt;id             name="id"             column="BLOG_ITEM_ID"&gt;                        &lt;generator class="native"/&gt;                    &lt;/id&gt;                &lt;property             name="title"             column="TITLE"             not-null="true"/&gt;                    &lt;property             name="text"             column="TEXT"             not-null="true"/&gt;                    &lt;property             name="datetime"             column="DATE_TIME"             not-null="true"/&gt;                    &lt;many-to-one             name="blog"             column="BLOG_ID"             not-null="true"/&gt;                &lt;/class&gt;    &lt;/hibernate-mapping&gt;</pre></div><div class="sect1" lang="zh-cn"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="example-weblog-code"></a>23.3.&nbsp;Hibernate &#20195;&#30721;</h2></div></div><div></div></div><p>            &#19979;&#38754;&#30340;&#31867;&#28436;&#31034;&#20102;&#25105;&#20204;&#21487;&#20197;&#20351;&#29992;Hibernate&#23545;&#36825;&#20123;&#31867;&#36827;&#34892;&#30340;&#19968;&#20123;&#25805;&#20316;&#12290;        </p><pre class="programlisting">package eg;import java.util.ArrayList;import java.util.Calendar;import java.util.Iterator;import java.util.List;import org.hibernate.HibernateException;import org.hibernate.Query;import org.hibernate.Session;import org.hibernate.SessionFactory;import org.hibernate.Transaction;import org.hibernate.cfg.Configuration;import org.hibernate.tool.hbm2ddl.SchemaExport;public class BlogMain {        private SessionFactory _sessions;        public void configure() throws HibernateException {        _sessions = new Configuration()            .addClass(Blog.class)            .addClass(BlogItem.class)            .buildSessionFactory();    }        public void exportTables() throws HibernateException {        Configuration cfg = new Configuration()            .addClass(Blog.class)            .addClass(BlogItem.class);        new SchemaExport(cfg).create(true, true);    }        public Blog createBlog(String name) throws HibernateException {                Blog blog = new Blog();        blog.setName(name);        blog.setItems( new ArrayList() );                Session session = _sessions.openSession();        Transaction tx = null;        try {            tx = session.beginTransaction();            session.persist(blog);            tx.commit();        }        catch (HibernateException he) {            if (tx!=null) tx.rollback();            throw he;        }        finally {            session.close();        }        return blog;    }        public BlogItem createBlogItem(Blog blog, String title, String text)                        throws HibernateException {                BlogItem item = new BlogItem();        item.setTitle(title);        item.setText(text);        item.setBlog(blog);        item.setDatetime( Calendar.getInstance() );        blog.getItems().add(item);                Session session = _sessions.openSession();        Transaction tx = null;        try {            tx = session.beginTransaction();            session.update(blog);            tx.commit();        }        catch (HibernateException he) {            if (tx!=null) tx.rollback();            throw he;        }        finally {            session.close();        }        return item;    }        public BlogItem createBlogItem(Long blogid, String title, String text)                        throws HibernateException {                BlogItem item = new BlogItem();        item.setTitle(title);        item.setText(text);        item.setDatetime( Calendar.getInstance() );                Session session = _sessions.openSession();        Transaction tx = null;        try {            tx = session.beginTransaction();            Blog blog = (Blog) session.load(Blog.class, blogid);            item.setBlog(blog);            blog.getItems().add(item);            tx.commit();        }        catch (HibernateException he) {            if (tx!=null) tx.rollback();            throw he;        }        finally {            session.close();        }        return item;    }        public void updateBlogItem(BlogItem item, String text)                    throws HibernateException {                item.setText(text);                Session session = _sessions.openSession();        Transaction tx = null;        try {            tx = session.beginTransaction();            session.update(item);            tx.commit();        }        catch (HibernateException he) {            if (tx!=null) tx.rollback();            throw he;        }        finally {            session.close();        }    }        public void updateBlogItem(Long itemid, String text)                    throws HibernateException {            Session session = _sessions.openSession();        Transaction tx = null;        try {            tx = session.beginTransaction();            BlogItem item = (BlogItem) session.load(BlogItem.class, itemid);            item.setText(text);            tx.commit();        }        catch (HibernateException he) {            if (tx!=null) tx.rollback();            throw he;        }        finally {            session.close();        }    }        public List listAllBlogNamesAndItemCounts(int max)                    throws HibernateException {                Session session = _sessions.openSession();        Transaction tx = null;        List result = null;        try {            tx = session.beginTransaction();            Query q = session.createQuery(                "select blog.id, blog.name, count(blogItem) " +                "from Blog as blog " +                "left outer join blog.items as blogItem " +                "group by blog.name, blog.id " +                "order by max(blogItem.datetime)"            );            q.setMaxResults(max);            result = q.list();            tx.commit();        }        catch (HibernateException he) {            if (tx!=null) tx.rollback();            throw he;        }        finally {            session.close();        }        return result;    }        public Blog getBlogAndAllItems(Long blogid)                    throws HibernateException {                Session session = _sessions.openSession();        Transaction tx = null;        Blog blog = null;        try {            tx = session.beginTransaction();            Query q = session.createQuery(                "from Blog as blog " +                "left outer join fetch blog.items " +                "where blog.id = :blogid"            );            q.setParameter("blogid", blogid);            blog  = (Blog) q.uniqueResult();            tx.commit();        }        catch (HibernateException he) {            if (tx!=null) tx.rollback();            throw he;        }        finally {            session.close();        }        return blog;    }        public List listBlogsAndRecentItems() throws HibernateException {                Session session = _sessions.openSession();        Transaction tx = null;        List result = null;        try {            tx = session.beginTransaction();            Query q = session.createQuery(                "from Blog as blog " +                "inner join blog.items as blogItem " +                "where blogItem.datetime &gt; :minDate"            );            Calendar cal = Calendar.getInstance();            cal.roll(Calendar.MONTH, false);            q.setCalendar("minDate", cal);                        result = q.list();            tx.commit();        }        catch (HibernateException he) {            if (tx!=null) tx.rollback();            throw he;        }        finally {            session.close();        }        return result;    }}</pre></div></div><div class="navfooter"><hr><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="example-parentchild.html">&#19978;&#19968;&#39029;</a>&nbsp;</td><td width="20%" align="center"><a accesskey="u" href="index.html">&#19978;&#19968;&#32423;</a></td><td width="40%" align="right">&nbsp;<a accesskey="n" href="example-mappings.html">&#19979;&#19968;&#39029;</a></td></tr><tr><td width="40%" align="left" valign="top">&#31532;&nbsp;22&nbsp;&#31456;&nbsp;&#31034;&#20363;&#65306;&#29238;&#23376;&#20851;&#31995;(Parent Child Relationships)&nbsp;</td><td width="20%" align="center"><a accesskey="h" href="index.html">&#36215;&#22987;&#39029;</a></td><td width="40%" align="right" valign="top">&nbsp;&#31532;&nbsp;24&nbsp;&#31456;&nbsp;&#31034;&#20363;&#65306;&#22797;&#26434;&#26144;&#23556;&#23454;&#20363;</td></tr></table></div></body></html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -