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

📄 example_weblog.pot

📁 hibernate-distribution-3.3.1.GA-dist.zip源码
💻 POT
📖 第 1 页 / 共 2 页
字号:
      "\n"      "import java.util.ArrayList;\n"      "import java.util.Calendar;\n"      "import java.util.Iterator;\n"      "import java.util.List;\n"      "\n"      "import org.hibernate.HibernateException;\n"      "import org.hibernate.Query;\n"      "import org.hibernate.Session;\n"      "import org.hibernate.SessionFactory;\n"      "import org.hibernate.Transaction;\n"      "import org.hibernate.cfg.Configuration;\n"      "import org.hibernate.tool.hbm2ddl.SchemaExport;\n"      "\n"      "public class BlogMain {\n"      "    \n"      "    private SessionFactory _sessions;\n"      "    \n"      "    public void configure() throws HibernateException {\n"      "        _sessions = new Configuration()\n"      "            .addClass(Blog.class)\n"      "            .addClass(BlogItem.class)\n"      "            .buildSessionFactory();\n"      "    }\n"      "    \n"      "    public void exportTables() throws HibernateException {\n"      "        Configuration cfg = new Configuration()\n"      "            .addClass(Blog.class)\n"      "            .addClass(BlogItem.class);\n"      "        new SchemaExport(cfg).create(true, true);\n"      "    }\n"      "    \n"      "    public Blog createBlog(String name) throws HibernateException {\n"      "        \n"      "        Blog blog = new Blog();\n"      "        blog.setName(name);\n"      "        blog.setItems( new ArrayList() );\n"      "        \n"      "        Session session = _sessions.openSession();\n"      "        Transaction tx = null;\n"      "        try {\n"      "            tx = session.beginTransaction();\n"      "            session.persist(blog);\n"      "            tx.commit();\n"      "        }\n"      "        catch (HibernateException he) {\n"      "            if (tx!=null) tx.rollback();\n"      "            throw he;\n"      "        }\n"      "        finally {\n"      "            session.close();\n"      "        }\n"      "        return blog;\n"      "    }\n"      "    \n"      "    public BlogItem createBlogItem(Blog blog, String title, String text)\n"      "                        throws HibernateException {\n"      "        \n"      "        BlogItem item = new BlogItem();\n"      "        item.setTitle(title);\n"      "        item.setText(text);\n"      "        item.setBlog(blog);\n"      "        item.setDatetime( Calendar.getInstance() );\n"      "        blog.getItems().add(item);\n"      "        \n"      "        Session session = _sessions.openSession();\n"      "        Transaction tx = null;\n"      "        try {\n"      "            tx = session.beginTransaction();\n"      "            session.update(blog);\n"      "            tx.commit();\n"      "        }\n"      "        catch (HibernateException he) {\n"      "            if (tx!=null) tx.rollback();\n"      "            throw he;\n"      "        }\n"      "        finally {\n"      "            session.close();\n"      "        }\n"      "        return item;\n"      "    }\n"      "    \n"      "    public BlogItem createBlogItem(Long blogid, String title, String text)\n"      "                        throws HibernateException {\n"      "        \n"      "        BlogItem item = new BlogItem();\n"      "        item.setTitle(title);\n"      "        item.setText(text);\n"      "        item.setDatetime( Calendar.getInstance() );\n"      "        \n"      "        Session session = _sessions.openSession();\n"      "        Transaction tx = null;\n"      "        try {\n"      "            tx = session.beginTransaction();\n"      "            Blog blog = (Blog) session.load(Blog.class, blogid);\n"      "            item.setBlog(blog);\n"      "            blog.getItems().add(item);\n"      "            tx.commit();\n"      "        }\n"      "        catch (HibernateException he) {\n"      "            if (tx!=null) tx.rollback();\n"      "            throw he;\n"      "        }\n"      "        finally {\n"      "            session.close();\n"      "        }\n"      "        return item;\n"      "    }\n"      "    \n"      "    public void updateBlogItem(BlogItem item, String text)\n"      "                    throws HibernateException {\n"      "        \n"      "        item.setText(text);\n"      "        \n"      "        Session session = _sessions.openSession();\n"      "        Transaction tx = null;\n"      "        try {\n"      "            tx = session.beginTransaction();\n"      "            session.update(item);\n"      "            tx.commit();\n"      "        }\n"      "        catch (HibernateException he) {\n"      "            if (tx!=null) tx.rollback();\n"      "            throw he;\n"      "        }\n"      "        finally {\n"      "            session.close();\n"      "        }\n"      "    }\n"      "    \n"      "    public void updateBlogItem(Long itemid, String text)\n"      "                    throws HibernateException {\n"      "    \n"      "        Session session = _sessions.openSession();\n"      "        Transaction tx = null;\n"      "        try {\n"      "            tx = session.beginTransaction();\n"      "            BlogItem item = (BlogItem) session.load(BlogItem.class, itemid);\n"      "            item.setText(text);\n"      "            tx.commit();\n"      "        }\n"      "        catch (HibernateException he) {\n"      "            if (tx!=null) tx.rollback();\n"      "            throw he;\n"      "        }\n"      "        finally {\n"      "            session.close();\n"      "        }\n"      "    }\n"      "    \n"      "    public List listAllBlogNamesAndItemCounts(int max)\n"      "                    throws HibernateException {\n"      "        \n"      "        Session session = _sessions.openSession();\n"      "        Transaction tx = null;\n"      "        List result = null;\n"      "        try {\n"      "            tx = session.beginTransaction();\n"      "            Query q = session.createQuery(\n"      "                \"select blog.id, blog.name, count(blogItem) \" +\n"      "                \"from Blog as blog \" +\n"      "                \"left outer join blog.items as blogItem \" +\n"      "                \"group by blog.name, blog.id \" +\n"      "                \"order by max(blogItem.datetime)\"\n"      "            );\n"      "            q.setMaxResults(max);\n"      "            result = q.list();\n"      "            tx.commit();\n"      "        }\n"      "        catch (HibernateException he) {\n"      "            if (tx!=null) tx.rollback();\n"      "            throw he;\n"      "        }\n"      "        finally {\n"      "            session.close();\n"      "        }\n"      "        return result;\n"      "    }\n"      "    \n"      "    public Blog getBlogAndAllItems(Long blogid)\n"      "                    throws HibernateException {\n"      "        \n"      "        Session session = _sessions.openSession();\n"      "        Transaction tx = null;\n"      "        Blog blog = null;\n"      "        try {\n"      "            tx = session.beginTransaction();\n"      "            Query q = session.createQuery(\n"      "                \"from Blog as blog \" +\n"      "                \"left outer join fetch blog.items \" +\n"      "                \"where blog.id = :blogid\"\n"      "            );\n"      "            q.setParameter(\"blogid\", blogid);\n"      "            blog  = (Blog) q.uniqueResult();\n"      "            tx.commit();\n"      "        }\n"      "        catch (HibernateException he) {\n"      "            if (tx!=null) tx.rollback();\n"      "            throw he;\n"      "        }\n"      "        finally {\n"      "            session.close();\n"      "        }\n"      "        return blog;\n"      "    }\n"      "    \n"      "    public List listBlogsAndRecentItems() throws HibernateException {\n"      "        \n"      "        Session session = _sessions.openSession();\n"      "        Transaction tx = null;\n"      "        List result = null;\n"      "        try {\n"      "            tx = session.beginTransaction();\n"      "            Query q = session.createQuery(\n"      "                \"from Blog as blog \" +\n"      "                \"inner join blog.items as blogItem \" +\n"      "                \"where blogItem.datetime > :minDate\"\n"      "            );\n"      "\n"      "            Calendar cal = Calendar.getInstance();\n"      "            cal.roll(Calendar.MONTH, false);\n"      "            q.setCalendar(\"minDate\", cal);\n"      "            \n"      "            result = q.list();\n"      "            tx.commit();\n"      "        }\n"      "        catch (HibernateException he) {\n"      "            if (tx!=null) tx.rollback();\n"      "            throw he;\n"      "        }\n"      "        finally {\n"      "            session.close();\n"      "        }\n"      "        return result;\n"      "    }\n"      "}]]>"msgstr ""

⌨️ 快捷键说明

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