Java tutorial
/* * Hibernate, Relational Persistence for Idiomatic Java * * Copyright (c) 2010, Red Hat Inc. or third-party contributors as * indicated by the @author tags or express copyright attribution * statements applied by the authors. All third-party contributions are * distributed under license by Red Hat Inc. * * This copyrighted material is made available to anyone wishing to use, modify, * copy, or redistribute it subject to the terms and conditions of the GNU * Lesser General Public License, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License * for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this distribution; if not, write to: * Free Software Foundation, Inc. * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ package hibernate; import static org.junit.Assert.assertNotNull; import java.sql.Connection; import java.sql.SQLException; import java.sql.Statement; import java.util.Date; import java.util.Iterator; import java.util.List; import java.util.Set; import org.hibernate.Session; import org.hibernate.jdbc.Work; import org.junit.Test; import com.lichhao.blog.model.Article; import com.lichhao.blog.model.Tag; /** * Illustrates the use of Hibernate native APIs. The code here is unchanged from * the {@code basic} example, the only difference being the use of annotations * to supply the metadata instead of Hibernate mapping files. * * @author Steve Ebersole */ public class TagAndArticleTest extends BaseHibernateConfig { @Test public void test() throws Exception { // SessionFactory createAnnotataionSessionFactory(); testTagAndArticle(); closeSessionFactory(); // XmlSessionFactory createXmlSessionFactory(); testTagAndArticle(); closeSessionFactory(); } public void testTagAndArticle() { Session session = sessionFactory.openSession(); session.beginTransaction(); Tag tag = new Tag(); tag.setTagName(""); tag.setCreateDate(new Date()); session.save(tag); assertNotNull("??Tag???", tag.getTagId()); Tag tag1 = new Tag(); tag1.setTagName(""); tag1.setCreateDate(new Date()); session.save(tag1); assertNotNull("??Tag???", tag1.getTagId()); Tag tag2 = new Tag(); tag2.setTagName("?"); tag2.setCreateDate(new Date()); session.save(tag2); assertNotNull("??Tag???", tag2.getTagId()); Tag tag3 = new Tag(); tag3.setTagName(""); tag3.setCreateDate(new Date()); session.save(tag3); assertNotNull("??Tag???", tag3.getTagId()); Article article = new Article(); article.setTitle(""); article.setContent(""); article.setCreateDate(new Date()); article.setModifyDate(new Date()); article.setSummary("?"); article.setVisitCount(0); article.setIsPublished(Boolean.TRUE); article.setType("post"); article.setCommentStatus("open"); article.setArticleStatus("public"); article.setLink("http://localhost:8080/newblog/post/abce"); Set<Tag> ts = article.getTags(); ts.add(tag); ts.add(tag1); ts.add(tag2); ts.add(tag3); session.save(article); assertNotNull("??Article???", article.getArticleId()); article = new Article(); article.setTitle("2"); article.setContent("2"); article.setCreateDate(new Date()); article.setModifyDate(new Date()); article.setSummary("?2"); article.setVisitCount(0); article.setIsPublished(Boolean.TRUE); article.setType("post"); article.setCommentStatus("open"); article.setArticleStatus("public"); article.setLink("http://localhost:8080/newblog/post/abce2"); ts = article.getTags(); ts.add(tag); ts.add(tag1); ts.add(tag2); ts.add(tag3); session.save(article); assertNotNull("??Article???", article.getArticleId()); session.getTransaction().commit(); session.close(); session = sessionFactory.openSession(); session.beginTransaction(); List<Article> articles = session.createQuery( "select p from Article p left join fetch p.tags t where t.tagName = :tagName order by p.createDate desc") .setParameter("tagName", tag.getTagName()).list(); logger.debug("'" + tag.getTagName() + "'?:"); for (Article bean : articles) { session.refresh(bean); logger.debug("------------------------"); logger.debug("" + bean.getArticleId()); logger.debug("" + bean.getTitle()); logger.debug(""); for (Tag t : bean.getTags()) { logger.debug(t.getTagName()); } logger.debug("------------------------"); } // hibernatesql?doReturningWork session.doWork(new Work() { @Override public void execute(Connection connection) throws SQLException { Statement stmt = connection.createStatement(); stmt.executeUpdate("delete from tag_article"); stmt.executeUpdate("delete from article"); stmt.executeUpdate("delete from tag"); stmt.close(); } }); session.getTransaction().commit(); session.close(); } }