2004-06-03 12:31:32 -04:00
|
|
|
<chapter id="example-weblog">
|
|
|
|
<title>Example: Weblog Application</title>
|
|
|
|
|
|
|
|
<sect1 id="example-weblog-classes">
|
|
|
|
<title>Persistent Classes</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
The persistent classes represent a weblog, and an item posted
|
|
|
|
in a weblog. They are to be modelled as a standard parent/child
|
|
|
|
relationship, but we will use an ordered bag, instead of a set.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<programlisting><![CDATA[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;
|
|
|
|
}
|
|
|
|
}]]></programlisting>
|
|
|
|
|
|
|
|
<programlisting><![CDATA[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;
|
|
|
|
}
|
|
|
|
}]]></programlisting>
|
|
|
|
|
|
|
|
</sect1>
|
|
|
|
|
|
|
|
<sect1 id="example-weblog-mappings">
|
|
|
|
<title>Hibernate Mappings</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
The XML mappings should now be quite straightforward.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<programlisting><![CDATA[<?xml version="1.0"?>
|
2004-08-09 23:16:42 -04:00
|
|
|
<!DOCTYPE hibernate-mapping PUBLIC
|
2005-01-23 20:49:15 -05:00
|
|
|
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
|
|
|
|
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
|
2004-06-03 12:31:32 -04:00
|
|
|
|
|
|
|
<hibernate-mapping package="eg">
|
|
|
|
|
2004-08-09 23:16:42 -04:00
|
|
|
<class
|
|
|
|
name="Blog"
|
2004-09-26 00:21:03 -04:00
|
|
|
table="BLOGS">
|
2004-08-09 23:16:42 -04:00
|
|
|
|
|
|
|
<id
|
|
|
|
name="id"
|
2004-06-03 12:31:32 -04:00
|
|
|
column="BLOG_ID">
|
2004-08-09 23:16:42 -04:00
|
|
|
|
2004-06-03 12:31:32 -04:00
|
|
|
<generator class="native"/>
|
2004-08-09 23:16:42 -04:00
|
|
|
|
2004-06-03 12:31:32 -04:00
|
|
|
</id>
|
2004-08-09 23:16:42 -04:00
|
|
|
|
|
|
|
<property
|
|
|
|
name="name"
|
|
|
|
column="NAME"
|
|
|
|
not-null="true"
|
2004-06-03 12:31:32 -04:00
|
|
|
unique="true"/>
|
2004-08-09 23:16:42 -04:00
|
|
|
|
|
|
|
<bag
|
|
|
|
name="items"
|
|
|
|
inverse="true"
|
|
|
|
order-by="DATE_TIME"
|
2004-06-03 12:31:32 -04:00
|
|
|
cascade="all">
|
2004-08-09 23:16:42 -04:00
|
|
|
|
2004-06-03 12:31:32 -04:00
|
|
|
<key column="BLOG_ID"/>
|
|
|
|
<one-to-many class="BlogItem"/>
|
2004-08-09 23:16:42 -04:00
|
|
|
|
2004-06-03 12:31:32 -04:00
|
|
|
</bag>
|
2004-08-09 23:16:42 -04:00
|
|
|
|
2004-06-03 12:31:32 -04:00
|
|
|
</class>
|
2004-08-09 23:16:42 -04:00
|
|
|
|
2004-06-03 12:31:32 -04:00
|
|
|
</hibernate-mapping>]]></programlisting>
|
|
|
|
|
|
|
|
<programlisting><![CDATA[<?xml version="1.0"?>
|
2004-08-09 23:16:42 -04:00
|
|
|
<!DOCTYPE hibernate-mapping PUBLIC
|
2005-01-23 20:49:15 -05:00
|
|
|
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
|
|
|
|
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
|
2004-06-03 12:31:32 -04:00
|
|
|
|
|
|
|
<hibernate-mapping package="eg">
|
2004-08-09 23:16:42 -04:00
|
|
|
|
|
|
|
<class
|
|
|
|
name="BlogItem"
|
|
|
|
table="BLOG_ITEMS"
|
2004-06-03 12:31:32 -04:00
|
|
|
dynamic-update="true">
|
2004-08-09 23:16:42 -04:00
|
|
|
|
|
|
|
<id
|
|
|
|
name="id"
|
2004-06-03 12:31:32 -04:00
|
|
|
column="BLOG_ITEM_ID">
|
2004-08-09 23:16:42 -04:00
|
|
|
|
2004-06-03 12:31:32 -04:00
|
|
|
<generator class="native"/>
|
2004-08-09 23:16:42 -04:00
|
|
|
|
2004-06-03 12:31:32 -04:00
|
|
|
</id>
|
2004-08-09 23:16:42 -04:00
|
|
|
|
|
|
|
<property
|
|
|
|
name="title"
|
|
|
|
column="TITLE"
|
2004-06-03 12:31:32 -04:00
|
|
|
not-null="true"/>
|
2004-08-09 23:16:42 -04:00
|
|
|
|
|
|
|
<property
|
|
|
|
name="text"
|
|
|
|
column="TEXT"
|
2004-06-03 12:31:32 -04:00
|
|
|
not-null="true"/>
|
2004-08-09 23:16:42 -04:00
|
|
|
|
|
|
|
<property
|
|
|
|
name="datetime"
|
|
|
|
column="DATE_TIME"
|
2004-06-03 12:31:32 -04:00
|
|
|
not-null="true"/>
|
2004-08-09 23:16:42 -04:00
|
|
|
|
|
|
|
<many-to-one
|
|
|
|
name="blog"
|
|
|
|
column="BLOG_ID"
|
2004-06-03 12:31:32 -04:00
|
|
|
not-null="true"/>
|
2004-08-09 23:16:42 -04:00
|
|
|
|
2004-06-03 12:31:32 -04:00
|
|
|
</class>
|
2004-08-09 23:16:42 -04:00
|
|
|
|
2004-06-03 12:31:32 -04:00
|
|
|
</hibernate-mapping>]]></programlisting>
|
|
|
|
|
|
|
|
</sect1>
|
|
|
|
|
|
|
|
<sect1 id="example-weblog-code">
|
|
|
|
<title>Hibernate Code</title>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
The following class demonstrates some of the kinds of things
|
|
|
|
we can do with these classes, using Hibernate.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<programlisting><![CDATA[package eg;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Calendar;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.List;
|
|
|
|
|
2004-08-09 23:16:42 -04:00
|
|
|
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;
|
2004-06-03 12:31:32 -04:00
|
|
|
|
|
|
|
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.save(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.list().get(0);
|
|
|
|
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 > :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;
|
|
|
|
}
|
|
|
|
}]]></programlisting>
|
|
|
|
|
|
|
|
</sect1>
|
|
|
|
|
|
|
|
</chapter>
|
|
|
|
|